Created
November 29, 2017 17:41
-
-
Save ShmuelMofrad/4b4f0b1d7f159b2455e8117b46ba553d to your computer and use it in GitHub Desktop.
Integer.getInteger and Integer.valueOf in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]"); | |
} | |
} |
Author
ShmuelMofrad
commented
Nov 29, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment