conversion methods charts

 

  To convert from primitives to String:

 

 

from
byte b

from
short s

from
char c

from
int i

from
long l

from
float f

from
double d

to
String

s = String.
valueOf(b)

s = String.
valueOf(s)

s = String.
valueOf(c)

s = String.
valueOf(i)

s = String.
valueOf(l)

s = String.
valueOf(f)

s = String.
valueOf(d)

 

 

  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
String s

from
Byte B

from
Short S

from
Character C

from
Integer I

from
Long L

from
Float F

from
Double D

from
Boolean B            

to
String

s.toString()

returns s

s = B.
toString()

s = S.
toString()

s = C.
toString()

s = I.
toString()

s = L.
toString()

s = F.
toString()

s = D.
toString()

s = B.
toString()

to
byte b

b = Byte.
parseByte(s)

b = B.byte
Value()

b = S.
byteValue()

 

b = I.byte
Value()

b = L.byte
Value()

b = F.byte
Value()

b = D.byte
Value()

 

to
short sh

sh = Short.
parseShort(s)

s = B.short
Value()

s = S.sho
rtValue()

 

s = I.short
Value()

s = L.short
Value()

s = F.short
Value()

s = D.short
Value()

 

to
char[ ]

c[ ] = s.toChar
Array()

 

 

c = C.char
Value()

 

 

 

 

 

to
int i

 i = Integer.
parseInt(s)

i = B.int
Value()

i = S.
intValue()

i=Char.getNu
mericValue()

i = I.int
Value()

i = L.int
Value()

i = F.int
Value()

i = D.int
Value()

 

to
long l

l = Long.pa
rseLong(s)

l = B.long
Value()

l = S.
longValue()

 

l = I.long
Value()

l = L.long
Value()

l = F.long
Value()

l = D.long
Value()

 

to
float f

f = Float.
parseFloat(s)

f = B.float
Value()

f = S.
floatValue()

 

f = I.float
Value()

f = L.float
Value()

f = F.float
Value()

f = D.float
Value()

 

to
double d

d = Double.pa
rseDouble(s)

d = B.dou
bleValue()

d = S.dou
bleValue()

 

d=I.double
Value()

d=L.double
Value()

d=F.double
Value()

d=D.double
Value()

 

to
boolean

 

 

 

 

 

 

 

 

b = B.bool
eanValue()

 

  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
byte  b

from
short  s

from
char  c

from
int  i

from
long  l

from
float  f

from
double  d

from
boolean  b            

to
Byte  B *

Byte B = new
Byte ( b );

 

 

 

 

 

 

 

to
Short  S *

Short S =  new
Short ( b );

Short S = new
Short ( s );

 

 

 

 

 

 

to Char-
acter  C

 

 

Character C = new Character
( c );

 

 

 

 

 

to
Integer  I *

Integer I = new
Integer (  b );

Integer I = new
Integer (  s );

Integer I = new
Integer (  c );

Integer I = new
Integer (  i );

 

 

 

 

to
Long  L *

Long L = new
Long ( b );

Long L = new
Long ( s );

Long L = new
Long ( c );

Long L = new
Long ( i );

Long L = new
Long ( l );

 

 

 

to
Float  F *

Float F = new
Float ( b );

Float F = new
Float ( s );

Float F = new
Float ( c );

Float F = new
Float ( i );

Float F = new
Float ( l );

Float F = new
Float ( f );

Float F = new
Float ( d );

 

to
Double  D *

Double D = new
Double ( b );

Double D = new
Double ( s );

Double D = new
Double ( c );

Double D = new
Double ( i );

Double D = new
Double ( l );

Double D = new
Double ( f );

Double D = new
Double ( d );

 

to
Boolean B

 

 

 

 

 

 

 

Boolean B=new Boolean ( b );

 

 

  Converting byte and char arrays to Strings.  To convert between primitive arrays and Strings: 

 

 

from
String s

from
byte[  ]  b

from
char[ ] c

to String s

 

String s = new String( b );

String s = new String ( c );

or

s = String.valueOf( c );

to
byte[ ] b

byte[ ] b = s.getBytes( );

 

byte[ ] b = new  byte[ c.length ];

for ( int x = 0; x < b.length; x++ )
b[ x ] = (byte) c[ x ];

to
char[ ] c

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++)
c[ x ] = (char)  b[ 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" ):

}