valueOf( ) Methods in java

In Java, the valueOf() method is a static method defined in many wrapper classes, such as Integer, Double, Boolean, etc. This method is used to create an instance of the corresponding wrapper class from a specified primitive value or a string.

Here is the general syntax for the valueOf() method:

static WrapperClass valueOf(primitiveType value);
static WrapperClass valueOf(String s);

Examples:

1. Using valueOf() with Primitive Values:

int primitiveInt = 42;

// Using valueOf to create an Integer object from a primitive int
Integer intWrapper = Integer.valueOf(primitiveInt);

double primitiveDouble = 3.14;

// Using valueOf to create a Double object from a primitive double
Double doubleWrapper = Double.valueOf(primitiveDouble);

2. Using valueOf() with Strings:

String numericString = "123";

// Using valueOf to create an Integer object from a numeric string
Integer intWrapperFromString = Integer.valueOf(numericString);

String boolString = "true";

// Using valueOf to create a Boolean object from a string representing a boolean value
Boolean boolWrapperFromString = Boolean.valueOf(boolString);

Notes:

  1. Optimization:
  • The valueOf() method is often implemented to cache and reuse instances for certain values, providing better memory efficiency. For example, Integer.valueOf() caches integers in the range -128 to 127.
Integer a = Integer.valueOf(42);
Integer b = Integer.valueOf(42);

System.out.println(a == b); // Outputs true (same object reference)
  1. Autoboxing:
  • The valueOf() method is frequently used in autoboxing, where primitive values are automatically converted to their corresponding wrapper objects.
int primitiveInt = 42;

// Autoboxing: primitive int to Integer wrapper class
Integer intWrapper = Integer.valueOf(primitiveInt);
  1. Compatibility:
  • The valueOf() method provides a convenient way to work with primitive types and their corresponding wrapper classes, making code more readable and consistent.
  1. String Representation:
  • When valueOf() is used with a String, it parses the string and creates an object representing the corresponding primitive value. For example, Integer.valueOf("123") creates an Integer object with a value of 123.
String numericString = "123";
Integer intWrapperFromString = Integer.valueOf(numericString);

Using valueOf() is a common practice when working with wrapper classes, especially when you need to convert primitive types or strings to their corresponding wrapper objects. It simplifies the process of creating wrapper objects and is widely used in various scenarios, including collection manipulation and handling user inputs.

The valueOf( ) method returns the enumeration constant whose value corresponds to the string passed in str

Example of valuesof() method

An enumeration of apple varieties

enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}

class EnumDemo2 {
public static void main(String args[])
{
Apple ap;
System.out.println(“Here are all Apple constants:”);
// use valueOf()
ap = Apple.valueOf(“Winesap”);
System.out.println(“ap contains ” + ap);
}
}

Output of valueof() method

ap contains Winesap

Leave a Comment