This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing

 
Ian Lubelsky
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to test a piece of code which converts a string to an integer through autoboxing.

This works:


But this doesn't


From what I read and understood, is the second method can now be written, even though the compiler will interpret it as the first example I gave. However, when I compile the second example, I get an Incompatible Types error. It says Found java.lang.String, but Required java.lang.Integer.

Would I just be better off doing a..... Integer value = Integer.parseInt(args[0]); instead?
 
Henry Wong
author
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autoboxing is for the conversion of a primative type to its wrapper, and vice versa. It has nothing to do with String to Integer conversion.

Henry
 
Ian Lubelsky
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After re-reading the section, that is what I figured. So I'm just going to use: Integer value = Integer.parseInt(args[0]);
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks good, but it might be preceded by a test like thisYou may also wish to test for a NumberFormatException.
 
Vivek Saha
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one please help me to understand the concept of "AUTOBOXING & UNBOXING" .......................?
 
Vivek Saha
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IS following the SCJP 6 book by--- Kathy & Bert enough for securing 100% marks in da SCJP exam???
 
Janeice DelVecchio
Saloon Keeper
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek, please start a new thread for your questions. Asking questions in someone else's thread is hijacking and not very nice to the original poster.

SCJP exam questions should be asked in the SCJP forum.

A simple google search of autoboxing and Java should get you started.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And while we're making remarks on your posts:
Vivek Saha wrote:IS following the SCJP 6 book by--- Kathy & Bert enough for securing 100% marks in da SCJP exam???

1) UseRealWords. Not "da" but "the".
2) Nothing can guarantee you a 100% score in any exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic