Java Integer intValue Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the Java intValue() method of the Integer class. This method returns the int equivalent of the specified number and is generally used for unboxing.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117716{{"`Java Integer intValue Method`"}} java/classes_objects -.-> lab-117716{{"`Java Integer intValue Method`"}} java/class_methods -.-> lab-117716{{"`Java Integer intValue Method`"}} java/exceptions -.-> lab-117716{{"`Java Integer intValue Method`"}} java/modifiers -.-> lab-117716{{"`Java Integer intValue Method`"}} java/oop -.-> lab-117716{{"`Java Integer intValue Method`"}} java/packages_api -.-> lab-117716{{"`Java Integer intValue Method`"}} java/user_input -.-> lab-117716{{"`Java Integer intValue Method`"}} java/wrapper_classes -.-> lab-117716{{"`Java Integer intValue Method`"}} java/identifier -.-> lab-117716{{"`Java Integer intValue Method`"}} java/arrays -.-> lab-117716{{"`Java Integer intValue Method`"}} java/data_types -.-> lab-117716{{"`Java Integer intValue Method`"}} java/operators -.-> lab-117716{{"`Java Integer intValue Method`"}} java/output -.-> lab-117716{{"`Java Integer intValue Method`"}} java/strings -.-> lab-117716{{"`Java Integer intValue Method`"}} java/variables -.-> lab-117716{{"`Java Integer intValue Method`"}} java/string_methods -.-> lab-117716{{"`Java Integer intValue Method`"}} java/system_methods -.-> lab-117716{{"`Java Integer intValue Method`"}} end

Setting up the Project

Before we get started, we need to create a new Java file in the ~/project directory. Open your terminal and execute the following command:

touch ~/project/IntegerValueDemo.java

This will create a new Java file called IntegerValueDemo.java in the ~/project directory.

Creating an Integer Object

In this step, we will create an instance of the Integer class and store a value in it.

public class IntegerValueDemo {
    public static void main(String[] args) {
        Integer num = 10;
    }
}

Converting Integer to int using intValue()

Now, we will use the intValue() method to get the int equivalent of the Integer object we just created.

public class IntegerValueDemo {
    public static void main(String[] args) {
        Integer num = 10;
        int intValue = num.intValue();
        System.out.println("Integer value of " + num + " is " + intValue);
    }
}

In the above code, we first created a variable intValue and assigned the value returned by the intValue() method. We then printed out the result using the System.out.println() method.

To run the code above, go to the ~/project directory in the terminal and execute the following commands:

javac IntegerValueDemo.java
java IntegerValueDemo

Converting String to int using parseInt()

In this step, we will see an example of converting a String to an int using the parseInt() method.

public class IntegerValueDemo {
    public static void main(String[] args) {
        String number = "15";
        int intValue = Integer.parseInt(number);
        System.out.println("Integer value of " + number + " is " + intValue);
    }
}

In the above code, we first created a String variable number and assigned it a value of "15". We then called the parseInt() method on this String variable to get the equivalent int value. We then printed out the result using the System.out.println() method.

To run the code above, go to the ~/project directory in the terminal and execute the following commands:

javac IntegerValueDemo.java
java IntegerValueDemo

Converting int to Integer using valueOf()

In this step, we will see an example of converting an int to an Integer object using the valueOf() method.

public class IntegerValueDemo {
    public static void main(String[] args) {
        int intValue = 20;
        Integer num = Integer.valueOf(intValue);
        System.out.println("Integer value of " + intValue + " is " + num);
    }
}

In the above code, we first created an int variable intValue and assigned it a value of 20. We then called the valueOf() method on this int variable to get the equivalent Integer object. We then printed out the result using the System.out.println() method.

To run the code above, go to the ~/project directory in the terminal and execute the following commands:

javac IntegerValueDemo.java
java IntegerValueDemo

Unboxing using intValue()

In this step, we will see an example of unboxing using the intValue() method.

public class IntegerValueDemo {
    public static void main(String[] args) {
        Integer num = 30;
        int intValue = num;
        System.out.println("Integer value of " + num + " is " + intValue);
    }
}

In the above code, we created an Integer object num and assigned it a value of 30. We then assigned this Integer object to an int variable intValue, using the auto-unboxing feature introduced in Java 5. We then printed out the result using the System.out.println() method.

To run the code above, go to the ~/project directory in the terminal and execute the following commands:

javac IntegerValueDemo.java
java IntegerValueDemo

Handling NullPointerException

In this step, we will see an example of handling a NullPointerException exception that may occur when using the intValue() method.

public class IntegerValueDemo {
    public static void main(String[] args) {
        Integer num = null;
        try {
            int intValue = num.intValue();
            System.out.println("Integer value of " + num + " is " + intValue);
        } catch (NullPointerException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

In the above code, we created an Integer object num and assigned it a value of null. This will cause a NullPointerException to be thrown when we try to call the intValue() method on it. We are catching this exception using a try-catch block and printing out the error message using the System.out.println() method.

To run the code above, go to the ~/project directory in the terminal and execute the following commands:

javac IntegerValueDemo.java
java IntegerValueDemo

Using User Input

In this step, we will see an example of using user input to get a value and converting it to an int using the parseInt() method.

import java.util.Scanner;

public class IntegerValueDemo {
    public static void main(String[] args) {
        try {
            System.out.print("Enter an integer value: ");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            int intValue = Integer.parseInt(input);
            System.out.println("Integer value of " + input + " is " + intValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input. Please enter a valid integer value.");
        }
    }
}

In the above code, we are using the Scanner class to get input from the user. We are then calling the parseInt() method on this input to get the equivalent int value. We are also handling the NumberFormatException exception that may occur if the user inputs an invalid value.

To run the code above, go to the ~/project directory in the terminal and execute the following commands:

javac IntegerValueDemo.java
java IntegerValueDemo

Using Command Line Arguments

In this step, we will see an example of using command line arguments to pass a value to the program.

public class IntegerValueDemo {
    public static void main(String[] args) {
        try {
            String input = args[0];
            int intValue = Integer.parseInt(input);
            System.out.println("Integer value of " + input + " is " + intValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input. Please enter a valid integer value.");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error: No value provided. Please provide an integer value.");
        }
    }
}

In the above code, we are using the args array to get the value passed as a command line argument. We are then calling the parseInt() method on this value to get the equivalent int value. We are also handling the NumberFormatException exception that may occur if the command line argument is invalid, and the ArrayIndexOutOfBoundsException exception that may occur if no value is provided.

To run the code above, go to the ~/project directory in the terminal and execute the following commands:

javac IntegerValueDemo.java
java IntegerValueDemo 25

Summary

  • The intValue() method of the Integer class returns the int equivalent of an Integer object.
  • The parseInt() method of the Integer class converts a String to an int.
  • The valueOf() method of the Integer class converts an int to an Integer object.
  • Unboxing is the process of converting an Integer object to an int automatically.
  • The NullPointerException exception may occur when using the intValue() method on a null Integer object.
  • The NumberFormatException exception may occur when using the parseInt() method on an invalid String.
  • The ArrayIndexOutOfBoundsException exception may occur when trying to access an invalid index in the args array.

Other Java Tutorials you may like