What are Utility methods in Java?

In Java Utility methods perform common, often re-used functions that are helpful for accomplishing routine programming tasks.

What is a utility method?

Utility method which contains reusable code. we can call these methods from any public class. This helps to reduce code within the trigger and make it more structured.

What is meant by the utility method in java?

The methods in java classes like Arrays, Objects and Math, and wrapper classes are called utility methods in java. This method contains reusable code.

Table of Content :

There are many utility methods in java. we see them one by one.

1. valueOf() method.

 2. XXXValue() method. 

3. parseXxx() method.

 4. toString() method.

valueOf() method

We can use the valueOf() method to create a wrapper object for the given primitive or use this method is an alternative for the constructor.

Case:1

Every wrapper class except the Character class contains a static valueOf() method to create a wrapper object for String. public static wrapper valueOf(String s); Example:

 
class WrapperClassDemo {
public static void main(String[] args)throws Exception
Integer i=Integer.valueOf("10");
Double d=Double.valueOf("10.5");
Boolean b=Boolean.valueOf("ashok");
System.out.println(i);//10
System.out.println(d);//10.5
System.out.println(b);//false
}
} 

Case:2

Every integral type wrapper class (Byte, Short, Integer, and Long) contains the following valueOf() method specified radix string to wrapper object.

public static wrapper valueOf(String s , int radix ) ; //radix means base Note: the allowed radix range is 2 to 36.

 
class WrapperClassDemo {
public static void main(String[] args)
Integer i=Integer.valueOf("100",2);
System.out.println(i);//4
}
} 

Every wrapper class including the Character class defines the valueOf() method to convert primitive to the wrapper.

public static wrapper valueOf(primitive p);

Example:

  
class WrapperClassDemo {
public static void main(String[] args)throws Exception
Integer i=Integer.valueOf(10);
Double d=Double.valueOf(10.5);
Boolean b=Boolean.valueOf(true);
Character ch=Character.valueOf('a');
System.out.println(ch); //a
System.out.println(i);//10
System.out.println(d);//10.5
System.out.println(b);//true
}
} 


2. xxxValue() method

We can use xxxValue() methods to convert wrapper objects to primitive.

Every number type wrapper class (Byte, Short, Integer, Long, Float, Double) contains the following 6 xxx methods to convert wrapper objects to primitives.

1). public short shortValue() 
2). public int intValue() 
3). public long longValue() 
4). public float floatValue() 
5). public double doubleValue(); 
6). public byte byteValue()

Example:

 

class WrapperClassDemo {
public static void main(String[] args)throws Exception
Integer i=new Integer(130);
System.out.println(i.byteValue());//-126
System.out.println(i.shortValue());//130
System.out.println(i.intValue());//130
System.out.println(i.longValue());//130
System.out.println(i.floatValue());//130.0
System.out.println(i.doubleValue());//130.0
}
} 

charValue() method

Character class contains charValue() method to convert Character object to char primitive.

public char charValue(); 

Example:

  
class WrapperClassDemo {
public static void main(String[] args)
Character ch=new Character('a');
char c=ch.charValue();
System.out.println(c);//a
}
} 

booleanValue() method

The Boolean class contains booleanValue() method to convert the Boolean object to a boolean primitive. 

public boolean booleanValue( ); 

Example:

  
class WrapperClassDemo {
public static void main(String[] args)
Boolean b=new Boolean("ashok");
boolean b1=b.booleanValue();
System.out.println(b1);//false
}
} 

In total there are 38(= 6*6+1+1) xxxValue() methods are possible.

3. parseXxx() method

We can use this method to convert String to the corresponding primitive

Case:1 

Every wrapper class except the Character class contains a static parseXxx() method to convert String to the corresponding primitive. public static primitive parseXxx(String s); 

Example:

   
class WrapperClassDemo {
public static void main(String[] args) {
int i=Integer.parseInt("10");
boolean b=Boolean.parseBoolean("ashok");
double d=Double.parseDouble("10.5");
System.out.println(i);//10
System.out.println(b);//false
System.out.println(d);//10.5
}
} 

Case:2

The integral type wrapper classes(Byte, Short, Integer, Long) contain the following parseXxx() method to con specified radix String form to corresponding primitive.

public static primitive parseXxx(String s, int radix); The allowed range of redix is: 2 to 36 Example:


        
class WrapperClassDemo {
public static void main(String[] args)
int i=Integer.parseInt("100",2);
System.out.println(i);//4
}
}
 


4. toString() method

We can use the toString() method to convert wrapper object (or) primitive to String. Form 1 :

public String toString();

Example:

 
class WrapperClassDemo {
public static void main(String[] args) {
Integer i=Integer.valueOf("10");
System.out.println(i);//10
System.out.println(i.toString());//10
}
} 

Case:2

Every wrapper class contains a static toString() method to convert primitive to String. public static String toString(primitive p); Example:

     
class WrapperClassDemo {
public static void main(String[] args)
String s1=Integer.toString(10);
String s2=Boolean.toString(true);
String s3=Character.toString('a');
System.out.println(s1); //10
System.out.println(s2); //true
System.out.println(s3); //a
}
} 

Case:3

Integer and Long classes contain the following static toString() method to convert the primitive to a specific String form.

public static String toString(primitive p, int radix);

 Example:

           
class WrapperClassDemo {
public static void main(String[] args)
String s1=Integer.toString(7,2);
String s2=Integer.toString(17,2);
System.out.println(s1);//111
System.out.println(s2);//10001
}
} 

Case:4

 

Integer and Long classes contains the following toXxxString() methods.
public static String toBinaryString(primitive p);
public static String toOctalString(primitive p);
public static String toHexString(primitive p); 

Example:
 
class WrapperClassDemo {
public static void main(String[] args)
String s1=Integer.toBinaryString(7);
String s2=Integer.toOctalString(10);
String s3=Integer.toHexString(20);
String s4=Integer.toHexString(10);
System.out.println(s1);//111
System.out.println(s2);//12
System.out.println(s3);//14
System.out.println(s4);//a
}
} 

Dancing between String, wrapper object, and primitive

Dancing between String, wrapper object and primitive

Articles/Questiones related to Utility methods in Java

Java toString() method with Examples

In this article, we have seen what Utility methods in Java with examples.