wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes valueOf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "valueOf" Watch "valueOf" New topic
Author

valueOf

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131


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]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Vijay Haah
Greenhorn

Joined: Oct 20, 2004
Posts: 3
Hey Netty,

See the return type for the intValue() method in the Integer class and you will be able to understand why the compile error occurred in lines 2 and 4.

Have fun,
Vijay
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Marc, Vijay

Thanks for dissecting the issue and laying it's organs out.
My own issue was, I was completely missing the 'outside' "Integer.parseInt" on line 3 and 4, when reading the question.

.......
I live to fight another day.

Thx!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: valueOf