File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Autoboxing" Watch "Autoboxing" New topic
Author

Autoboxing

Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
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
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
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

Joined: Oct 13, 2005
Posts: 37891
    
  22
That looks good, but it might be preceded by a test like thisYou may also wish to test for a NumberFormatException.
Vivek Saha
Greenhorn

Joined: Mar 03, 2010
Posts: 4
Can any one please help me to understand the concept of "AUTOBOXING & UNBOXING" .......................?
Vivek Saha
Greenhorn

Joined: Mar 03, 2010
Posts: 4
IS following the SCJP 6 book by--- Kathy & Bert enough for securing 100% marks in da SCJP exam???
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

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.


When you do things right, people won't be sure you've done anything at all.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autoboxing
 
Similar Threads
AutoBoxing
valueOf() vs parseXXX()
Autoboxing / Unboxing doubt
How to find the elements present in the Array