Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Integer Class

The Integer class in Java is a wrapper class for the primitive data type int. It is part of the java.lang package and provides several utility methods for working with integers. In this tutorial, we'll discuss the basics of the Integer class, including its constructors, methods, and use cases.

  • Creating Integer Objects

There are two ways to create an Integer object:

  • Using the constructor:
Integer number1 = new Integer(10);
  • Using the valueOf method:
Integer number2 = Integer.valueOf(10);

Note: It is generally recommended to use the valueOf method as it is more memory-efficient due to caching of frequently used integer values.

  • Converting an Integer to a Primitive int

To convert an Integer object to a primitive int, use the intValue method:

Integer number = Integer.valueOf(10);
int primitiveInt = number.intValue();
  • Parsing Strings to Integers

The Integer class provides the parseInt and valueOf methods to convert a string containing an integer value to an int or Integer object, respectively.

String numberStr = "42";
int parsedInt = Integer.parseInt(numberStr); // 42
Integer parsedInteger = Integer.valueOf(numberStr); // Integer object with value 42
  • Converting Integers to Strings

To convert an Integer object or a primitive int to a String, use the toString method:

Integer number = Integer.valueOf(10);
String numberStr = number.toString(); // "10"

int primitiveInt = 10;
String primitiveIntStr = Integer.toString(primitiveInt); // "10"
  • Useful Constants and Methods

The Integer class provides several useful constants and methods for working with integers:

  • Integer.MAX_VALUE and Integer.MIN_VALUE: These constants represent the maximum and minimum values of an int in Java, respectively.
System.out.println("Max value: " + Integer.MAX_VALUE); // 2147483647
System.out.println("Min value: " + Integer.MIN_VALUE); // -2147483648
  • compare(int x, int y): Compares two integers, returning a negative value if x is less than y, 0 if x is equal to y, or a positive value if x is greater than y.
int result = Integer.compare(10, 20); // -1
  • bitCount(int i): Returns the number of one-bits in the two's complement binary representation of the specified int value.
int bitCount = Integer.bitCount(42); // 3 (42 in binary is 101010)

In conclusion, the Integer class in Java provides a convenient wrapper for the primitive data type int, along with several utility methods for working with integers. By understanding the basics of the Integer class and its methods, you can write more efficient and readable code when working with integer values.

  1. Converting string to integer in Java

    // Converting string to integer
    String strNumber = "123";
    int number = Integer.parseInt(strNumber);
    
  2. Java Integer class methods and examples

    // Integer class methods
    int value = 42;
    Integer integerObj = Integer.valueOf(value); // Using valueOf
    int intValue = integerObj.intValue(); // Getting primitive int value
    
  3. Parsing strings to integers in Java

    // Parsing strings to integers
    String strNumber = "456";
    int parsedNumber = Integer.parseInt(strNumber);
    
  4. Handling null values with Integer class in Java

    // Handling null values with Integer class
    Integer nullableInteger = null;
    int result = (nullableInteger != null) ? nullableInteger : 0;
    
  5. Integer.valueOf() vs. new Integer() in Java

    // Using valueOf
    Integer valueOfInteger = Integer.valueOf(123);
    
    // Using new Integer (deprecated in Java 9+)
    Integer newInteger = new Integer(456);
    
  6. Formatting integer values in Java

    // Formatting integer values
    int number = 789;
    String formattedNumber = String.format("Formatted Number: %d", number);
    
  7. Comparing integer values in Java with Integer.compare()

    // Comparing integer values
    int num1 = 123;
    int num2 = 456;
    int comparisonResult = Integer.compare(num1, num2);