🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Integer valueOf() method in Java programming and how to use it with an example.
1. Integer valueOf() Method Overview
Definition:
The valueOf() method of the Java Integer class returns an Integer object holding the value of the specified int or string.
Syntax:
1. Integer.valueOf(int i)
2. Integer.valueOf(String s)
3. Integer.valueOf(String s, int radix)
Parameters:
int i: The primitive integer value to be converted to an Integer object.
String s: The string to be parsed into an Integer object.
int radix: The base of the numeral system used in the string (only relevant for the third syntax, default is 10).
Key Points:
- The method provides a way to obtain Integer objects without using the new keyword.
- The valueOf() method is often used for boxing, i.e., converting a primitive int to an Integer object.
- If the string does not contain a parsable integer, a NumberFormatException will be thrown.
- The method is static, which means it's called on the class (Integer) rather than on an instance of the class.
2. Integer valueOf() Method Example
public class ValueOfExample {
public static void main(String[] args) {
// Convert primitive int to Integer object
int num = 100;
Integer intObject = Integer.valueOf(num);
System.out.println("Integer object from int: " + intObject);
// Convert String to Integer object
String numberStr = "200";
Integer intFromString = Integer.valueOf(numberStr);
System.out.println("Integer object from string: " + intFromString);
// Convert String in hexadecimal (base 16) to Integer object
String hexStr = "1a";
Integer intFromHex = Integer.valueOf(hexStr, 16);
System.out.println("Integer object from hex string: " + intFromHex);
}
}
Output:
Integer object from int: 100 Integer object from string: 200 Integer object from hex string: 26
Explanation:
In the example, we started by converting a primitive integer value (100) to an Integer object using the valueOf() method.
We then parsed a string "200" to get its Integer representation.
Lastly, we demonstrated the use of the radix parameter by parsing a hexadecimal string "1a" (which is 26 in base 10) into an Integer object.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment