BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

ValueOf() Method in Java

Last Updated: June 2, 2024 by Chaitanya Singh | Filed Under: java

In this guide, we will discuss one of the most commonly used method in Java. The valueOf() method is available in several wrapper classes and other classes such as String class. This method is mostly used in the conversion of types, for example, converting int to Integer, double to Double etc.

ValueOf() method usage In Wrapper Classes

In wrapper classes, the valueOf() method is used for converting primitive types such as int, float double to Wrapper class objects such as Integer, Float, Double etc.

Integer.valueOf()

Converts a primitive int or a String to an Integer object.

public class ValueOfExample {
public static void main(String[] args) {
// From int to Integer
Integer intObj1 = Integer.valueOf(101);

// From String to Integer
Integer intObj2 = Integer.valueOf("200");

System.out.println(intObj1); // Output: 101
System.out.println(intObj2); // Output: 200
}
}

Double.valueOf()

Converts a primitive double or a String to a Double object.

public class ValueOfExample {
public static void main(String[] args) {
// From double to Double
Double doubleObj1 = Double.valueOf(10.55);

// From String to Double
Double doubleObj2 = Double.valueOf("20.55");

System.out.println(doubleObj1); // Output: 10.55
System.out.println(doubleObj2); // Output: 20.55
}
}

Boolean.valueOf()

Converts a primitive boolean or a String to a Boolean object.

public class ValueOfExample {
public static void main(String[] args) {
// From boolean to Boolean
Boolean boolObj1 = Boolean.valueOf(true);

// From String to Boolean
Boolean boolObj2 = Boolean.valueOf("false");

System.out.println(boolObj1); // Output: true
System.out.println(boolObj2); // Output: false
}
}

ValueOf() method In the String Class

In the String class, the valueOf() method is used to convert various data types into their string representation.

Example of conversion from different data type to String

public class ValueOfStringExample {
public static void main(String[] args) {
// int to String Conversion
String str1 = String.valueOf(100);
System.out.println(str1); // Output: "100"

// double to String Conversion
String str2 = String.valueOf(10.5);
System.out.println(str2); // Output: "10.5"

// boolean to String Conversion
String str3 = String.valueOf(true);
System.out.println(str3); // Output: "true"

// char array to String Conversion
char[] charArray = {'J', 'a', 'v', 'a'};
String str4 = String.valueOf(charArray);
System.out.println(str4); // Output: "Java"

// Object to String Conversion
Object obj = new Object();
String str5 = String.valueOf(obj);
System.out.println(str5);
}
}

Points to Note:

  1. Overloading of valueOf() Method: The valueOf() method is overloaded in wrapper classes and String class to handle different input types (e.g., primitive types and strings).
  2. Used for Conversion: It provides an easy way to convert primitive data types to Wrapper objects as well as other data types to Strings.
  3. Handles Null Input: In case of wrapper classes, if the input is null, it throws NumberFormatException. In case of String class, String.valueOf(null) returns the string “null” if the input data is null.

Top Related Articles:

  1. Passing a List to a Varargs method
  2. Difference between Static and Dynamic Dispatch in Java
  3. Java Integer byteValue() Method
  4. Java Annotations tutorial with examples
  5. Java String to int Conversion

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap