Java Integer: The meaning and difference between int and integer, converting one to another and need of integer when int already existing etc. are discussed clearly with example in Java int vs Integer. It is advised to read this before going further.
Converting String to int (parsing using parseInt())is given in String to int Conversion in Java.
Example on Java Integer. Some more concepts of Integer are discussed here under.
public class Demo
{
public static void main(String args[])
{
System.out.println("Minimum value Integer: " + Integer.MIN_VALUE); //prints -2147483648
System.out.println("Maximum value Integer: " + Integer.MAX_VALUE); //prints 2147483647
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
Integer i3 = new Integer(20);
System.out.println(i1 == i2); // prints false
System.out.println(i1 == i3); // prints false
System.out.println(i1.equals(i2)); // prints true
System.out.println(i1.equals(i3)); // prints false
System.out.println(i1*i1); // prints 100
String str1 = i1.toString();
String str2 = String.valueOf(i1);
System.out.println(str1); // prints 10
System.out.println(str2); // prints 10
}
}
MIN_VALUE and MAX_VALUE are static final variables declared in Java Integer class that prints the minimum and maximum values an Integer object can take.
We cannot do any arithmetic operations on any Java object. But here, i1*i1 prints 100. The reason is i1 is converted back to int with the concept of autoboxing. This works from JDK 1.5 only; earlier versions of JDK up to 1.4 raises compilation error.
For comparison between Java Integer objects (with == and equals()), strings immutability nature concept is used.
String str1 = i1.toString();
String str2 = String.valueOf(i1);
Any object of Java, to convert into String either toString() of Object class or static method valueOf() can be used.
Just like any object of Java, the Java Integer object i1 takes 4 bytes of memory.
Java Integer is a wrapper class.
============================================================
Your one stop destination for all data type conversions
byte TO | short | int | long | float | double | char | boolean |
short TO | byte | int | long | float | double | char | boolean |
int TO | byte | short | long | float | double | char | boolean |
float TO | byte | short | int | long | double | char | boolean |
double TO | byte | short | int | long | float | char | boolean |
char TO | byte | short | int | long | float | double | boolean |
boolean TO | byte | short | int | long | float | double | char |
String and data type conversions
String TO | byte | short | int | long | float | double | char | boolean |
byte | short | int | long | float | double | char | boolean | TO String |