Topics Discussed: Class Wrappers and changing types using toString / parseint / parseDouble
Source code for Lesson 21:
class testing{ public static void main(String[] args){ //double d = null; //Can't do this, double is a primative type Double d = null; int k=5; if(k < 5 ) { d+=10; } else{ k+=10; } if(d==null){ //do Something } System.out.println(d+" - " + k); //double d=4.0 //String str = "" ; //str = appendOhai(Double.toString(d)); } public static String appendOhai(String str){ str +="Ohai"; return str; } }
Class wrappers:
Class wrappers exist to help us manipulate primitives of similar type. Some code that you’ll want to be aware of is
Double d = null; // Class wrappers can accept null values Double.parseInt(d); //Class wrappers can be used to change the variable type as long as the data is the same **NOTE** When changing variable type, there can be a loss of precision. Example double d= 4.779; int i = Double.parseInt(d); // I would now be equal to 4. Everything after the decimal would be dropped.