➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Java Integer decode() Method | This blog explains the decode method in the Java Integer class. This method decodes the string into an integer.
Many times we use an integer as a string like (“ ”) there are scenarios where we have provided the integer value inside the double quotes, and the decode method decodes these strings as an integer. The decode method actually decodes the string data type into an integer data type.
Integer.decode() Java
The syntax for the decode method is as follows:-
public static Integer decode(String str)
The Integer.decode() method of the Java Integer class decodes a String into an Integer. It accepts decimal, hexadecimal, and octal numbers given by the following grammar:-
Parameter: the str which needs to be decoded.
Returns: the decoded integer value.
Exception: NumberFormatException, throws this expectation when the string contains a value that is not parsable.
Integer.decode() Examples
Example-1:
String1 = “1223”
Output: 1223
Example-2:
String2: “Apple”
Output:- NumberFormatException.
public class Main {
public static void main(String[] args) {
String str = "65";
System.out.println("Afer decoding the string: " + Integer.decode(str));
}
}
Output:-
Afer decoding the string: 65
public class Main {
public static void main(String[] args) {
String str = "100";
Integer num = Integer.decode(str);
System.out.println("String after decoding: " + num);
System.out.println("Type of str: " + str.getClass().getName());
System.out.println("Type of num: " + num.getClass().getName());
}
}
Output:-
String after decoding: 100
Type of str: java.lang.String
Type of num: java.lang.Integer
public class Main {
public static void main(String[] args) {
String str = "0X12";
Integer num = Integer.decode(str);
System.out.println("String after decoding: " + num);
}
}
Output:-
String after decoding: 18
The 0x and 0X represent hexadecimal numbers. The string contains hexadecimal 12 which is decoded in decimal value. And the decimal value for it is 18 as 16 * 1 + 1 * 2 = 18.
Exception Thrown by Java Integer decode() Method
The below program shows the expectation that can be thrown by the decode method when it finds the integer that cannot be parsed. We have passed an actual string “KnowProgram” as str and then decode the str, as this is not the integer that can be parsed hence the decode method throws an error.
public class Main {
public static void main(String[] args) {
String str = "KnowProgram";
System.out.println("String after decoding: ");
System.out.println(Integer.decode(str));
}
}
Output:-
String after decoding:
Exception in thread “main” java.lang.NumberFormatException: For input string: “KnowProgram”
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!