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.
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 theIntegerclass returns the int equivalent of anIntegerobject. - The
parseInt()method of theIntegerclass converts aStringto an int. - The
valueOf()method of theIntegerclass converts an int to anIntegerobject. - Unboxing is the process of converting an
Integerobject to an int automatically. - The
NullPointerExceptionexception may occur when using theintValue()method on a nullIntegerobject. - The
NumberFormatExceptionexception may occur when using theparseInt()method on an invalidString. - The
ArrayIndexOutOfBoundsExceptionexception may occur when trying to access an invalid index in theargsarray.



