my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why doesn't this work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't this work?

A. Fernandez
Greenhorn

Joined: Nov 24, 2003
Posts: 9
Ok,
So I have this simple code:
String s = "9";
int i = 9;
int k = i + Integer.intValue(s);
========
Now shouldn't Integer.intValue convert String s to a primitive so it can add it to i?
Instead Java finds java.lang.String instead of int.
What's the logic behind this?


Lovin' Life.....
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
intValue is an instance method not static.
Doing this should work:


SCJP 1.4<br />SCWCD 1.4
Don Wood
Ranch Hand

Joined: Dec 05, 2003
Posts: 65
Another possibility is Integer.parseInt which avoids creating the extra Integer object:

[ December 13, 2003: Message edited by: Don Wood ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why doesn't this work?
 
Similar Threads
Boxing
I am not getting forwarded to another page where i want to go
How to convert an Object to integer?
Casting to and from Stacks
Need suggestion on implementation