Skip to content

Java Integer Class | Methods | Integer Wrapper Class Example

  • by

Java integer class is a Wrapper class and comes Under the java.lang.Number package. This class has wraps a value of the primitive type int in an object. Only single fields can contain Integer object with type int.

Integer Compatibility Version: JDK 1.0

Java Integer Class Methods Integer Wrapper Class Example

 Instantiate Integer Wrapper Class

Here is the simple syntax of use Integer in Java. Using a new keyword with Integer class is no longer with Java 11 Version.

Integer integerValue = 100;

Java integer class example

Here is a code of integer wrapper class in java example. Creating and printing the value and type of.

class Hello {
    public static void main(String args[]) {

        Integer integerValue = 100;
        System.out.println(integerValue.getClass());
        System.out.println(integerValue);
    }
}

Output: class java.lang.Integer
100

Field of an Integer Object

Here are basic and important fields of Integer class Object.

ModifierTypeFieldDescription
staticintMAX_VALUEA constant – the maximum value of int data type and can be equal to 2^31-1.
staticintMIN_VALUEA constant – the minimum value of int data type and can be equal to -2^31.
staticintSIZEThe number of bits used to represent an int value in two’s complement binary form.
staticClass<Integer>TYPEThe Class object representing the primitive type int.

Java Integer Methods

Here we are mentioning some important methods of use in Integer, for a complete list do follow below official Java document site.

compare(int x, int y): This method simply returns -1 and 0, the result of comparing the two int method argument. Where -1 not equivalent and 0 for equal.

class Hello {
    public static void main(String args[]) {

        System.out.println(Integer.compare(10,59));
        System.out.println(Integer.compare(10,10));
    }
}

Output: -1
0

compareTo(Integer another integer): A compare the Integer value to passed int value and return an int, if equal then 0 otherwise 1.

class Hello {
    public static void main(String args[]) {

        Integer a = 100;
        System.out.println(a.compareTo(100));
        System.out.println(a.compareTo(3));
    }
}

Output: 0
1

reverse(int i): Returns the reverse value the order of the bits in the two\’s complement binary representation of the specified int value.

class Hello {
    public static void main(String args[]) {

        Integer a = 546;
        System.out.println(Integer.reverse(a));
    }
}

Output: 1145044992

For converting an Integer to Sring and other converting methods we will learn later tutorials.

Reference: https://docs.oracle.com/javase/9/docs/api/?java/lang/Integer.html (Official Site)

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Data Types Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *