Saturday, May 2, 2020

java puzzlers from oca part 4

In the fourth part of Java Puzzlers, we have something related to char type.


public class Puzzler {

    public static void main(String[] args){
        char myChar = 'a';
        myChar++;

        System.out.println(myChar);
    }
}

You may have guessed it. It will print "b" and the reason for it is that char type is unsigned numeric primitive in the disguise of a character. So if I add one then I'll get the next character in unicode representation.

Then let's take a look at that one


public class Puzzler {

    public static void main(String[] args){
        char myChar = 'a';

        System.out.println(myChar + myChar);
    }
}
Will this print "aa"? Or  which's 97 + 97 = 194 (where 97 is value of 'a'). I don't know if you guessed it right but the result is neither. It's "194". When Java sees plus it tells "hmm that's an addition not a concat" and adds myChars up and returns the int value for it.

java puzzlers from oca part 3

In this third part of Java puzzlers, we will see a surprise in variable naming restrictions.


If I show you this, I'm sure you won't be surprised that this does not compile. static is one of the reserved keywords so why should it work?
public class Puzzler {

    public static void main(String[] args){

        int static = 2;
    }

}
Now I'll ask you a more difficult one. What you think about the below code. Will this compile?
public class Puzzler {

    public static void main(String[] args){
        int bool = 0;
        int integer = 1;
        int const = 2;
        int goto = 3;
    }
}

None of these should be reserved keyword. This is not C right? If you thought that it will compile, you're wrong. const and goto are reserved keywords, but bool and integer are fine.

Sunday, April 19, 2020

java puzzlers from oca part 2

Welcome to the second part of Java Puzzlers from OCA. In this part we will see an interesting case about the underscore separator in numeric literals which came with Java 7.


In the below class you can see the separator underscore in the decimal literal. Also notice the class compiles now without a problem. Octal is the octal representation, binary is the binary and I'm sure you can't guess hex.

public class Puzzler {

    public static void main(String[] args){

        int decimal = 12_345;
        int octal = 04321;
        int binary = 0B1010;
        int hex = 0X4321A;
    }
} 

Octal literal is defined with 0, binary with 0b/0B and hex with 0x/0X. Ok then, let's begin putting _ for a better readability after them.
public class Puzzler {

    public static void main(String[] args){

        int decimal = 12_345;
        int octal = 0_4321;
        int binary = 0B1010;
        int hex = 0X4321A;
    }
} 
Neat. It compiles without a problem. Lets move to binary and hex.
public class Puzzler {

    public static void main(String[] args){

        int decimal = 12_345;
        int octal = 0_4321;
        int binary = 0B_1010;
        int hex = 0X_4321A;
    }
} 
Nope. You'll get "Illegal Underscore" there. I'm sure this is designed that way with something in mind, but it sure is a surprising behavior.

Saturday, April 18, 2020

java puzzlers from oca part 1

I'm reading Oracle Certified Associate Java SE Programmer book from Mala Gupta in my spare time and I'm surprised with some of the new things I learn. Some of the time they really don't make sense, some of the time they make sense but really surprising to see. So in this article series, I wanted to share them as "Java Puzzlers" which sounded much cooler than "Java Surprises".


Lets check the below code and see what happens when we call an empty object reference's static method or field.

public class Puzzler {

    public static int field = 1;

    public static void printField() {
        System.out.println(field);
    }

    public static void main(String[] args){
        /*
        * Lets see what happens when the
        * reference is null.
        * */

        Puzzler puzzler = null;
        puzzler.printField(); // prints 1
        System.out.println(puzzler.field); // prints 1
    }

}

When you try to guess what will happen, you can think that we will get NullPointerException while doing the method and field calls as the reference does not have an object attached to it. But remember that static methods and fields belong to the class itself and not to the instance. So without the need of an associated object you can use them and won't get an exception for doing that. An also the way we call the static method are usually in Puzzler.printField() form which tells more.

Thursday, March 5, 2020

scripting with painless language

Recently I've been working on a complex scoring function in Elasticsearch and stumbled upon this really cool article for painless language. Very well prepared and easy to understand.