This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
answer given = compile error on line 2 and 4 for me 'line 2' is fine and no issues here. let's come to line 3 and 4. 'valueOf' returns a wrapper 'Integer'. [so far so good] i2 = primitive & i4=wrapper [line 0] so line 3[primitive = wrapper] should give a compile error and not line 4[wrapper=wrapper]
First, look at line 1... Integer.parseInt("1") returns an int based on the String argument. Passing this int to the Integer constructor creates a new Integer. Then calling intValue() on this Integer instance returns an int. This int is what's assigned to int i1.
Now, reconsider line 2... This is exactly the same as line 1 except that it trys to assign the int to an Integer instance, i3, resulting in a compile-time error.
On to line 3... Integer.valueOf("1") returns an instance of Integer based on the String argument. Then calling toString returns it back to a String. This String is then used as an argument in Integer.parseInt, which returns an int. It's this int that's assigned to int i2.
And line 4... As on line 3, Integer.valueOf("1") returns an instance of Integer based on the String argument. But here, calling intValue on this Integer instance returns an int, which is passed to Integer.parseInt. Unfortunately, parseInt does not take an int argument, so we get a compile-time error. [ October 20, 2004: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org