Java .

Java Casting

Getting a String out of an ArrayList

String StringName = (String)arrayListName.get(n);

Getting a double out of an ArrayList (Stored in the ArrayList as a Double)

double doubName = ((Double)arrayListName.get(n)).doubleValue();

Getting a String out of a Vector

String StringName = (String)vectName.get(n);

Getting a double out of a Vector (Stored in the Vector as a Double)

double doubName = ((Double)vectName.get(n)).doubleValue();

String to Double to double (using Double constructor)

double doubName = new Double(stringName).doubleValue;

String to Double to double

double doubName = Double.valueOf(stringName).doubleValue;

String to double (using static Double method - Java 1.2 & later)

double doubName = Double.parseDouble(stringName);

double to a new String (using the String constructor)

String stringName = new String(doubleName);

double to existing String

stringName = String.valueOf(doubleName);

double to int

double doubleName = 3;
int intName = (int)doubleName;  
  // intName becomes == 3
double anotherDoubleName = 3.3;
int anotherIntName = (int)anotherDoubleName;  
  //anotherIntName becomes == 3, not 3.3

String to char

char charName = stringName.charAt(2); 
  //must specify offset in string to get char from
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by brknt on 2013-10-11 Rate this Comment

Thank you for this useful sharing, , but you wrote lines from "Getting a String out of an ArrayList" to "double to int" two times.

 
Sign in to comment on Java Casting.