🎓 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
String.valueOf() method in Java is used to convert different types of data into their string representation. This guide will cover the various overloads of the valueOf method, demonstrate how to use them, and explain their usage with examples.Table of Contents
- Introduction
valueOfMethod Overloads- Examples
- Converting Primitive Data Types
- Converting Object Types
- Converting Character Arrays
- Conclusion
Introduction
The String.valueOf() method is a static method in the String class that converts various types of data into their string representation. This method is particularly useful for converting primitive data types, objects, and character arrays to strings.
valueOf Method Overloads
The String.valueOf() method has several overloads to handle different data types:
String.valueOf(boolean b)String.valueOf(char c)String.valueOf(char[] data)String.valueOf(char[] data, int offset, int count)String.valueOf(double d)String.valueOf(float f)String.valueOf(int i)String.valueOf(long l)String.valueOf(Object obj)
Examples
Converting Primitive Data Types
The valueOf method can be used to convert primitive data types such as boolean, char, int, long, float, and double to their string representation.
Example
public class ValueOfExample {
public static void main(String[] args) {
boolean boolValue = true;
char charValue = 'A';
int intValue = 123;
long longValue = 456L;
float floatValue = 7.89f;
double doubleValue = 10.11;
String boolStr = String.valueOf(boolValue);
String charStr = String.valueOf(charValue);
String intStr = String.valueOf(intValue);
String longStr = String.valueOf(longValue);
String floatStr = String.valueOf(floatValue);
String doubleStr = String.valueOf(doubleValue);
System.out.println("Boolean to String: " + boolStr);
System.out.println("Char to String: " + charStr);
System.out.println("Int to String: " + intStr);
System.out.println("Long to String: " + longStr);
System.out.println("Float to String: " + floatStr);
System.out.println("Double to String: " + doubleStr);
}
}
Output:
Boolean to String: true
Char to String: A
Int to String: 123
Long to String: 456
Float to String: 7.89
Double to String: 10.11
Converting Object Types
The valueOf method can also be used to convert objects to their string representation by calling the object's toString method.
Example
public class ValueOfExample {
public static void main(String[] args) {
Object objValue = new Person("John", 30);
String objStr = String.valueOf(objValue);
System.out.println("Object to String: " + objStr);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
}
Output:
Object to String: Person{name='John', age=30}
Converting Character Arrays
The valueOf method can be used to convert character arrays to their string representation. There are two overloads: one for converting the entire array and another for converting a specific portion of the array.
Example (Converting Entire Array)
public class ValueOfExample {
public static void main(String[] args) {
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String charArrayStr = String.valueOf(charArray);
System.out.println("Char Array to String: " + charArrayStr);
}
}
Output:
Char Array to String: Hello
Example (Converting Portion of Array)
public class ValueOfExample {
public static void main(String[] args) {
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String partialCharArrayStr = String.valueOf(charArray, 1, 3);
System.out.println("Partial Char Array to String: " + partialCharArrayStr);
}
}
Output:
Partial Char Array to String: ell
Conclusion
The String.valueOf() method in Java is an important method for converting different types of data into their string representation. It can handle primitive data types, objects, and character arrays, making it a useful method for various conversion needs in Java applications. By understanding and using the different overloads of the valueOf method, you can efficiently convert data types to strings, enhancing the readability and maintainability of your code.
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