• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

significance of parseInt method

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks yes sir here should use parseInt.thanks you both.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic