File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't this work?

 
A. Fernandez
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
dennis zined
Ranch Hand
Posts: 330
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
intValue is an instance method not static.
Doing this should work:
 
Don Wood
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility is Integer.parseInt which avoids creating the extra Integer object:

[ December 13, 2003: Message edited by: Don Wood ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic