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.?
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.
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.
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).
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 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.
Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.