Skip to content

Instantly share code, notes, and snippets.

@ShmuelMofrad
Created November 29, 2017 17:41
Show Gist options
  • Save ShmuelMofrad/4b4f0b1d7f159b2455e8117b46ba553d to your computer and use it in GitHub Desktop.
Save ShmuelMofrad/4b4f0b1d7f159b2455e8117b46ba553d to your computer and use it in GitHub Desktop.
Integer.getInteger and Integer.valueOf in java
public class IntegerOrString {
public static void main(String[] args) {
String object1 = "S124";
String object2 = "124-ab45";
String object3 = "124";
int result1, result2, result3;
/* public static Integer getInteger(String nm) */
/* Determines the integer value of the system property with the specified name. */
result1 = Integer.getInteger(object1); // NullPointerException
result2 = Integer.getInteger(object2); // NullPointerException
result3 = Integer.getInteger(object3); // NullPointerException
System.out.println("result1" + result1);
System.out.println("result2" + result2);
System.out.println("result3" + result3);
System.out.println("object1: " + Integer.getInteger(object1));
System.out.println("object2: " + Integer.getInteger(object2));
System.out.println("object3: " + Integer.getInteger(object3));
/* public static Integer valueOf(String s) throws NumberFormatException */
/* Returns an Integer object holding the value of the specified String. */
result1 = Integer.valueOf(object1); // NumberFormatException
result2 = Integer.valueOf(object2); // NumberFormatException
result3 = Integer.valueOf(object3);
System.out.println("r3: " + result3);
System.out.println("\n[ READ MORE ]\n\n[ https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html ]");
}
}
@ShmuelMofrad
Copy link
Author

ShmuelMofrad commented Nov 29, 2017

Please comment on lines 13 - 19. and 28 + 29
Otherwise, throw exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment