Skip to main content Skip to docs navigation
TechSpiderTutorials

java.lang.Integer class in Java

java.lang.Integer

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.

Creating Objects

Integer(int value):
Creates an Integer object initialized with the provided int value.

Integer(String s):
Parses a string representation of an integer and creates an Integer object with the corresponding value.

Methods

public byte byteValue()
Returns the value of this Integer as a byte after a narrowing primitive conversion.


public short shortValue()
Returns the value of this Integer as a short after a narrowing primitive conversion.


public int intValue()
Returns the value of this Integer as an int.


public long longValue()
Returns the value of this Integer as a long after a widening primitive conversion.


public static int max(int a,int b)
Returns the greater of two int values as if by calling Math.max.


public static int min(int a,int b)
Returns the smaller of two int values as if by calling Math.min.


public static int parseInt(String s)throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value


java.lang.Integer Class Example

import java.lang.Integer;

public class IntegerExample {
    public static void main(String[] args) {
        // Creating Integer objects
        Integer int1 = new Integer(42);
        Integer int2 = new Integer("123");

        // Displaying the values
        System.out.println("Integer 1: " + int1);
        System.out.println("Integer 2: " + int2);

        // Using Integer methods
        int intValue1 = int1.intValue();
        int intValue2 = int2.intValue();

        System.out.println("Integer 1 as int: " + intValue1);
        System.out.println("Integer 2 as int: " + intValue2);

        // Parsing an integer from a string
        String intString = "456";
        int parsedInt = Integer.parseInt(intString);
        System.out.println("Parsed integer: " + parsedInt);

        // Converting an integer to String representation
        String intAsString = Integer.toString(789);
        System.out.println("Integer as string: " + intAsString);

        // Getting the hash code of an Integer object
        int hashCode = int1.hashCode();
        System.out.println("Hash code of Integer 1: " + hashCode);

        // Comparing Integer objects
        int comparison = Integer.compare(int1, int2);
        System.out.println("Comparison result: " + comparison);

        // Min and Max values of integers
        System.out.println("Minimum value of an integer: " + Integer.MIN_VALUE);
        System.out.println("Maximum value of an integer: " + Integer.MAX_VALUE);

        // Converting integer to binary, octal, and hexadecimal strings
        int num = 255;
        System.out.println("Binary string of " + num + ": " + Integer.toBinaryString(num));
        System.out.println("Octal string of " + num + ": " + Integer.toOctalString(num));
        System.out.println("Hexadecimal string of " + num + ": " + Integer.toHexString(num));
    }
}

Output

Integer 1: 42
Integer 2: 123
Integer 1 as int: 42
Integer 2 as int: 123
Parsed integer: 456
Integer as string: 789
Hash code of Integer 1: 42
Comparison result: -1
Minimum value of an integer: -2147483648
Maximum value of an integer: 2147483647
Binary string of 255: 11111111
Octal string of 255: 377
Hexadecimal string of 255: ff