• Post Reply Bookmark Topic Watch Topic
  • New Topic

significance of parseInt method  RSS feed

 
manish awara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i unable to find the use of parseInt() method to convert the String to int. i asked to my teacher but he told me that it has use in higher level.
can anyone describe it at the level of core java.?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose that you ask the user to enter a number, for example in a text box in a GUI. The content of the text box is a string. To be able to do computations with the number that is entered, you'll want to convert that string into an int.

Or, suppose that you are reading data from a text file. Maybe the text file contains numbers, that you need to do calculations with. The text in the file is read into a string, and then you convert that string into an int.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jesper has hinted, that method is in regular use and is used frequently by beginners.
 
manish awara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks yes sir here should use parseInt.thanks you both.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:As Jesper has hinted, that method is in regular use and is used frequently by beginners.


That sounds like experts don't use it frequently. Am I interpreting this wrong, or is there a better way to convert String to Integer?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're interpreting it wrong. Just use Integer.parseInt(). There's no special expert version that does things differently or that is better in some way.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, everybody uses parseXXX methods. I was replying to the comment about “use in higher level.”
It does not convert a String to an Integer, but to an int. To convert a String to an Integer, you should use Integer#valueOf(java.lang.String).
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan

From a String to Integer, the valueOf method is also used.

Integer.valueOf(s) is equivalent to Integer.valueOf(parseInt(s, 10)). The valueOf(int) method has the added benefit of using cached numeric values between -128 and 127.

[Edit: As Campbell rightly points out, the maximum value of the cache range could be higher than 127. Thanks CR!]

 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: . . . There's no special expert version . . .
Java® is not a “beginner's language” as BASIC was (nearly 50 years ago). The beginners use the expert version from day 1!
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, everybody uses parseXXX methods. I was replying to the comment about “use in higher level.”
It does not convert a String to an Integer, but to an int. To convert a String to an Integer, you should use Integer#valueOf(java.lang.String).


Ah, thought so. Thanks.
And whoops, I meant int.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote: . . . The valueOf(int) method has the added benefit of using cached numeric values between -128 and 127.
…and maybe more cached values.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!