Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing

 
Ian Lubelsky
Ranch Hand
Posts: 49
  • 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: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • 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: 50217
79
  • 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
  • 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
  • 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: 1809
12
Eclipse IDE Java Windows
  • 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: 20667
65
Chrome Eclipse IDE Java Windows
  • 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