Introduction
In this lab, we will learn about the toString()
method of the Integer
class in Java. We will see how to use this method to get the String representation of an Integer object. We will also see some examples to understand its usage.
In this lab, we will learn about the toString()
method of the Integer
class in Java. We will see how to use this method to get the String representation of an Integer object. We will also see some examples to understand its usage.
Create a new Java file called IntegerToString.java
.
public class IntegerToString {
public static void main(String[] args) {
// code to be added
}
}
toString()
In this step, we will create an integer object and then use the toString()
method to convert that integer object into string.
public class IntegerToString {
public static void main(String[] args) {
// create an Integer object
Integer num = 35;
// convert integer to string
String str = num.toString();
// print the string
System.out.println("String representation of the Integer: " + str);
}
}
Output:
String representation of the Integer: 35
toString()
The toString()
method also works for negative integers. In this step, we will convert a negative integer to its String representation.
public class IntegerToString {
public static void main(String[] args) {
// create a negative Integer object
Integer num = -18;
// convert negative integer to string
String str = num.toString();
// print the string
System.out.println("String representation of the Integer: " + str);
}
}
Output:
String representation of the Integer: -18
toString()
In this step, we will create an input integer using Scanner
and then use the toString()
method to get its String representation.
import java.util.Scanner;
public class IntegerToString {
public static void main(String[] args) {
// create scanner object
Scanner sc = new Scanner(System.in);
// take input integer from user
System.out.print("Enter an integer: ");
int num = sc.nextInt();
// convert integer to string
String str = Integer.toString(num);
// print the string
System.out.println("String representation of the entered integer: " + str);
}
}
Output:
Enter an integer: 56
String representation of the entered integer: 56
In step 4, we did not handle the case when the input is not an integer. In this step, we will handle the exception when the input is not a valid integer.
import java.util.Scanner;
public class IntegerToString {
public static void main(String[] args) {
// create scanner object
Scanner sc = new Scanner(System.in);
try {
// take input integer from user
System.out.print("Enter an integer: ");
int num = sc.nextInt();
// convert integer to string
String str = Integer.toString(num);
// print the string
System.out.println("String representation of the entered integer: " + str);
} catch(Exception e) {
System.out.println("Invalid input");
}
}
}
Output:
Enter an integer: 34
String representation of the entered integer: 34
toBinaryString()
In Java, we can use toBinaryString()
method of Integer
class to convert an integer to its binary string representation.
public class IntegerToString {
public static void main(String[] args) {
// create an integer
int num = 10;
// convert integer to binary string
String binaryString = Integer.toBinaryString(num);
// print binary string
System.out.println("Binary string representation of the Integer: " + binaryString);
}
}
Output:
Binary string representation of the Integer: 1010
toHexString()
In Java, we can use toHexString()
method of Integer
class to convert an integer to its hexadecimal string representation.
public class IntegerToString {
public static void main(String[] args) {
// create an integer
int num = 255;
// convert integer to hex string
String hexString = Integer.toHexString(num);
// print hex string
System.out.println("Hexadecimal string representation of the Integer: " + hexString);
}
}
Output:
Hexadecimal string representation of the Integer: ff
In this lab, we learned how to use the toString()
method of the Integer
class to convert an integer to a string representation. We also learned how to handle invalid input and convert an integer to a binary or hexadecimal string representation. This method is useful when we need to convert an integer into a string for use in different contexts.