This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

valueOf

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic