Last Updated:

Valueof () method in Java

Simple JavaString example of valueOf()


Let's take a simple example to understand the use of this method. In this example, we associate double nines with the end of this value. This value is an integer, and in order to add 99 at the end of an integer, we must first convert that integer to a string. We use the valueOf() method to convert the number to the equivalent string str, and then combine the 99 at the end of the converted string.

valueOf() method



public class JavaExample{
public static void main(String args[]){
char vowel[] = {‘B’, ‘G’, ‘H’, ‘P’, ‘Q’};
String str = String.valueOf(vowel);




Autoboxing and Unboxing

The last thing we need to understand before we can learn about equality and comparison is boxing.
Although primitives and object references have different semantics, they can be used interchangeably, thanks to the Java compiler.

Autoboxing is the automatic conversion of primitive types into their corresponding wrapper class, and unpacking is another direction. This allows us to use both types without discrimination:

List<Integer>values = newArrayList<>();

for (int i = 0; i < 50; i++>) {


Our list uses the Integer shell type, but our code compiles even if we add int. This is possible thanks to a compiler that modifies our code by autoboxing i:

List<Integer> values = new ArrayList<>();
for (int i = 0; i < 50; i++>) {


The same is true for the other way:

intsumEven(List<Integer> values) {
intf= 0;
for (Integer summand: values) {
if (summand % 2 == 0) {
f += summand;
return f;


Although we use operators such as % and + that are not available to the Integer object type, the code compiles normally. Because the compiler decompresses the shell type. The actual compiled code looks something like this:

intsumEven(List<Integer> values) {
intresult = 0;
for {
if (i.intValue() % 2 == 0) {
result += i.intValue();
the returned result;




If we look at other programming languages, the most logical conclusion for comparing values may be the operator == and its antagonist !=. Yes, we can use them to check for equality, and they compare the values to each other, but that might not be the value you expect.

Let's try to write an object equality program for two integer references to objects i1 and i2. Even if they do not point to the same object (i1 != i2), i1.equals(i2) will be true as long as they represent the same integer.

Run the following code. This will clarify your doubts about == and equals for integer objects:

public static void main(String[] args) {
integerq1 = new integer(5);
integerq2 = new Integer(5);

System.out.println(q1 == q2); false
System.out.println(i1.equals(q2)); /true
q1 = 10;
q2 = 10;

System.out.println(q1 == q2); true

q1 = Integer.valueOf(7);
q2 = Integer.valueOf(7);

System.out.println(q1 == q2);//true

q1 = Integer.parseInt(«9»);
q2 = Integer.parseInt(«9»);

System.out.println(q1 == q2);//true



Primitives are literals, fixed values in memory that can be checked for equality with ==.
Except when they can't. Unlike other primitive data types, floating-point data types float and double cannot be reliably tested for equality with = = because of their way of being stored in memory. These are not exact values:

floatvalue = 1.0q;
value += 0.1q; 1.1q
value += 0.1q; 1.2q
value += 0.1q; 1.3000001q

boolean is Equal = (value == 1.3q) // false

We have two options. Either using java.util.BigDecimal, which is accurate. Or using threshold comparisons:

7 float value
= 1.0 f;
value += 0.1 f; 1.1 f
value += 0.1 f; 1.2 f
value += 0.1 f; 1.3000001 f

FLOAT THRESHOLD = 0.00001 f;
booleanisEqual = Math.abs(value — 1.3 f) < THRESHOLD); true