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.
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.
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.
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;
}
}
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
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
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
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
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
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
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
intValue()
method of the Integer
class returns the int equivalent of an Integer
object.parseInt()
method of the Integer
class converts a String
to an int.valueOf()
method of the Integer
class converts an int to an Integer
object.Integer
object to an int automatically.NullPointerException
exception may occur when using the intValue()
method on a null Integer
object.NumberFormatException
exception may occur when using the parseInt()
method on an invalid String
.ArrayIndexOutOfBoundsException
exception may occur when trying to access an invalid index in the args
array.