Saturday, May 9, 2020

java puzzlers from oca part 6


Even for new Java developers, constructors are probably no big mystery. In essence, when you create an instance of a class, the constructor of this class is started. In the 6th part of Java Puzzlers series, we will see a case related to constructors.
public class Puzzler {

    public Puzzler(){
        System.out.println("Puzzler no arg constructor");
    }

    public static void main(String[] args){
        Puzzler puzzler = new Puzzler();
    }
}
In the example above Puzzler() constructor will start and "Puzzler no arg constructor" will be printed to the screen. Now lets see a new example.
public class Puzzler {

    public void Puzzler(){
        System.out.println("Puzzler no arg constructor?");
    }

    public static void main(String[] args){
        Puzzler puzzler = new Puzzler();
    }
}
As you can see we added a return value to the constructor of Puzzler and you may expect that "Puzzler no arg constructor?" will get printed but this is not right. When we add a return value to the constuctor, it stops being a constructor. So it won't get started when a new instance is created.

java puzzlers from oca part 5

In the fifth part of the Java Puzzlers series, we will see something related to X.parseX(String s) methods.


You can see what we expect from X.parseX() methods.
public class Puzzler {

    public static void main(String[] args){
        int i = Integer.parseInt("2"); 
        System.out.println(i); // prints 2
    }
}

We give the methods a String that can be converted to the primitive representation and hope for the best. Now lets check another example which will give us a NumberFormatException.
public class Puzzler {

    public static void main(String[] args){
        int i = Integer.parseInt("integer"); // java.lang.NumberFormatException: For input string: "integer"
    }
}

As the input is a word and not something that can be parsed to an integer, we get NumberFormatException. What happens above is consistent for each number type. So Integer, Byte, Short, Long, Double, Float won't surprise you when you call their parse methods with some random String. You'll get a NumberFormatException.
Now lets check what happens with boolean.
public class Puzzler {

    public static void main(String[] args){
        final boolean b1 = Boolean.parseBoolean("boolean?");
        System.out.println(b1);
    }
}
Can you guess what happens? The parse call will probably throw java.lang.BooleanFormatException, right? Not really. If you run that, it'll print "false" to the screen. The reason is Boolean.parseBoolean() just accepts anything and if it can't parse it, it just returns "false" value. Now lets see the other example.
public class Puzzler {

    public static void main(String[] args){
        final boolean b2 = Boolean.parseBoolean("TrUe");
        System.out.println(b2);
    }
}
You probably expect false again? That's not the case because parseBoolean is case insensitive and will return "true" in this case.