File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valueOf

 
Netty poestel
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vijay Haah
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic