Preparation for Java SE 11 Developer 1Z0-819

in java •  3 years ago  (edited)

Normally I would begin with buying some book and read it along with practicing the stuff I don't fully understand but this time I am planning to do it the hard way. On official Oracle exam topics list there are 13 main sections and 32 subsections if I'm counting it correctly. So let's begin with the first one:

1. Working with Java data types
1.1. Use primitives and wrapper classes, including, operators, parentheses, type promotion and casting

So it looks like a lot to do at once but is it? Let's recap.

Primitives and wrapper classes

There are eight primitive types and each of them has a wrapper class:
byte, short, int, long, float, double, char, boolean

Primitive wrapper class in Java wikipedia article looks like a good starting point to get overall idea of what are we dealing with. But that is not enough for a Java certification exam or it was not the case beck when I've begun my journey with Java language as my main source of income.

Let's see an example question from some random free Java mock exams out there:

After running following piece of code, what would be the output:

public static void main(String[] args) {
       int i = 127;
       int j = i;
       int x = 128;
       int y = x;
       
       List l = new ArrayList();
       l.add(i);
       l.add(j);
       l.add(x);
       l.add(y);
       
       System.out.println(l.get(0) == l.get(1));
       System.out.println(l.get(2) == l.get(3));
   }
  1. true
    false
  2. true
    true
  3. false
    true
  4. code would not compile

This is the kind of questions you could expect (at least I would) on a Java certification exam and the correct answer is... the 1st one. Why? There are several issues covered in this question actually. First thing is that primitives are impossible to be stored in a List (you could use array for that, thanks God Java is consistent) and because of that, behind the scenes int is automatically converted (or autoboxed to be precise) into Integer as only objects are being able to be stored in a List. Another issue is that, because of performance and optimization purposes,

public static Integer valueOf(int i)

method, according to java doc

will always cache values in the range -128 to 127

and the last piece is the == operator. When two primitive int values are compared everything should be fine and work as you would expect it to. Resulting in true true being printed to the console. But when == is used to compare two objects, the first comparison is done for exactly the same instance of the Integer object. You should know it's the same instance because of the value - 127 - is in the range of cached values. 128 on the other hand is not thus two different instances are created while putting (atoboxing) into the list. This is the madness you should prepare for if you like to become Certified Java Programmer (TM). There is even a question covering that on Stack Overflow.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!