parseInt or valueOf ?
hi
just a quick question I have seen parseInt and valueOf used but I do not understand the difference betweeen the 2 or when one should be used rather than the other. I read somewhere that parseInt is used for debugging purposes.
can someone please shed some light on this for me
thank you
[335 byte] By [
mmartinh] at [2007-9-26 5:45:32]

parseInt() is a method of the Integer class which is used to convert a String to an integer.
Example:
String s1 = "50";
int num1;
num1 = Integer.parseInt( s1 );
valueOf() is a method of the String class that returns the String representation of the argument. There are several versions of the valueOf() method. All of them receive different argument types ( boolean, char, int, long, float, etc).
Example:
char c = "Z";
system.println.out( "print Z as a string:" + String.valueOf( c ) );
In Java you can not mix values of basic types
(int, double,...) with objects. Sometimes when an
object is needed, e.g. package Hashtable, but you
will give this object a value of type int you put the
value in a so called Wrapper class and in doing it
you create an object of this int.
The lang-Package has a class for each type.
These classes are Boolean, Integer, Double and so on.
Some methods of the Integer class
to convert Integer and int
are as follows:
// Constructor
Integer ( int value);
// Class methods
static Integer valueOf (String s); // !!!
static String to String (int i );
static int parseInt (String s);// !!!
// Instance method
intintValue ();
This will make it possible to toggle between values
and objects.
If i is an int you make an object of it with
Integer Iobj = new Integer (i);
If you have a String s of values type int
you get the int-value by
valueOf(String s)
.
Reverse use
parseInt (String s);