conversion methods
charts
■ To convert from primitives to String:
|
|
from |
from |
from |
from |
from |
from |
from |
|
to |
s
= String. |
s
= String. |
s
= String. |
s
= String. |
s
= String. |
s
= String. |
s
= String. |
■ To convert from String to primitives or wrappers: see also String conversions
|
|
from
String s |
|
to
byte b |
byte
b = (byte) s.charAt(0); // for a
single character |
|
to
char c |
char
c = s.charAt(0); // for a single
character |
|
to
short sh |
sh
= Short.parseShort(s) |
|
to
int i |
i = Integer.parseInt(s) |
|
to
long l |
l
= Long.parseLong(s) |
|
to
float f |
f
= Float.parseFloat(s) |
|
to
double d |
d
= Double.parseDouble(s) |
|
to
Byte B |
B
= Byte.valueOf(s) |
|
to
Short S |
S
= Short.valueOf(s) |
|
to
Integer I |
I
= Integer.valueOf(s) |
|
to
Long L |
L = Long.valueOf(s); |
|
to
Float F |
F
= Float.valueOf(s); |
|
to
Double D |
D
= Double.valueOf(s) |
|
to
String |
s.toString() returns s |
■ Converting
wrappers. To convert from
wrappers to primitives and to String:
|
|
from |
from |
from |
from |
from |
from |
from |
from |
from |
|
|
to |
s.toString() returns
s |
s
= B. |
s
= S. |
s
= C. |
s
= I. |
s
= L. |
s
= F. |
s
= D. |
s
= B. |
|
|
to |
b
= Byte. |
b
= B.byte |
b
= S. |
|
b
= I.byte |
b
= L.byte |
b
= F.byte |
b
= D.byte |
|
|
|
to |
sh
= Short. |
s
= B.short |
s
= S.sho |
|
s
= I.short |
s
= L.short |
s
= F.short |
s
= D.short |
|
|
|
to |
c[
] = s.toChar |
|
|
c
= C.char |
|
|
|
|
|
|
|
to |
i = Integer. |
i
= B.int |
i
= S. |
i=Char.getNu |
i
= I.int |
i
= L.int |
i
= F.int |
i
= D.int |
|
|
|
to |
l
= Long.pa |
l
= B.long |
l
= S. |
|
l
= I.long |
l
= L.long |
l
= F.long |
l
= D.long |
|
|
|
to |
f
= Float. |
f
= B.float |
f
= S. |
|
f
= I.float |
f
= L.float |
f
= F.float |
f
= D.float |
|
|
|
to |
d
= Double.pa |
d
= B.dou |
d
= S.dou |
|
d=I.double |
d=L.double |
d=F.double |
d=D.double |
|
|
|
to |
|
|
|
|
|
|
|
|
b
= B.bool |
|
■ Converting primitives to wrappers. To convert from primitives to wrappers:
This chart shows allowable parameters in wrapper constructors.
An asterisk in the left hand column indicates these constructors will also take a double-quote-enclosed String argument of a suitable number.
Float and Double each accept such String numbers with or without imbedded decimal points.
For Boolean conversions see also Boolean class
|
|
from |
from |
from |
from |
from |
from |
from |
from |
|
to |
Byte
B = new |
|
|
|
|
|
|
|
|
to |
Short
S = new |
Short
S = new |
|
|
|
|
|
|
|
to
Char- |
|
|
Character
C = new Character |
|
|
|
|
|
|
to |
Integer
I = new |
Integer
I = new |
Integer
I = new |
Integer
I = new |
|
|
|
|
|
to |
Long
L = new |
Long
L = new |
Long
L = new |
Long
L = new |
Long
L = new |
|
|
|
|
to |
Float
F = new |
Float
F = new |
Float
F = new |
Float
F = new |
Float
F = new |
Float
F = new |
Float
F = new |
|
|
to |
Double
D = new |
Double
D = new |
Double
D = new |
Double
D = new |
Double
D = new |
Double
D = new |
Double
D = new |
|
|
to |
|
|
|
|
|
|
|
Boolean
B=new Boolean ( b ); |
■ Converting byte and
char arrays to Strings. To
convert between primitive arrays and Strings:
|
|
from |
from |
from |
|
to
String s |
|
String
s = new String( b ); |
String
s = new String ( c ); or s
= String.valueOf( c ); |
|
to |
byte[
] b = s.getBytes( ); |
|
byte[
] b = new byte[ c.length ]; for
( int x = 0; x < b.length; x++ ) |
|
to |
char[
] c = s.toCharArray( ) or char[
] c = new char[ s.length( ) ]; s.getChars( 0, s.length( ), c, 0 ); |
char[
] c = new char[ b.length ]; for
( int x = 0; x < b.length; x++) |
|
■ The various parse...(...) methods can be used for validity checking. For instance, the parseInt(...) method can be used to determine if a number is an int. i.e. Assume s is a String number which just came in from the console:
int
x;
try
{
x = Integer.parseInt( s );
System.out.println( s + " is indeed an int" );
}
catch
(NumberFormatException e) {
System.out.println( s +
" was not an int" ):
}
■ Similarly, parseDouble(
..) can be used to determine if a number is a double:
double
d;
try
{
d = Integer.parseDouble( s );
System.out.println( s + " is indeed a double" );
}
catch
(NumberFormatException e) {
System.out.println( s +
" was not a double" ):
}