Difference between integer.tostring and string.valueof

  • string.valueof () method return “null” String if  pass value is null.
  • integer.tostring() method will be throw NullPointerException  if  pass value is null.

Example:-

public class Test {
public static void main(String args[]) {  
    String str = null;
    System.out.println(str.toString()); // This will throw a NullPointerException
    System.out.println(String.valueOf(str));  // This will print "null"        
} 
}

Leave a comment