valueOf(all types) JAVA – JAVA 10th Class notes – ICSE JAVA Notes

valueOf(all types) JAVA – JAVA 10th Class notes – ICSE JAVA Notes

valueOf (all types) :

valueOf (all types) function converts from String to the primitive datatype.

 

Note : Conversion from String to primitive datatype is only possible when it contains number (digit). If this function is used with Alphabets or Alpha numeric String then it throws NumberFormatException.

 

String to Integer type Conversion :

Syntax : Integer var = Integer.valueOf(String);

 

Example : String str= “24”;

int n = Integer.valueOf(str);

 

Output : 24

Note : The output 24 is a Integer Value, where as the Input 24 is a String Value.

 

 

/* Program to illustrate the function valueof() */

 

class Value_Of

{

public static void main(String args[])

{

String str = “9″;

String str1=”56″;

int c=Integer.valueOf(str);

double d=Double.valueOf(str1);

System.out.println(” Integer Value : ” + c + “\n Double Value : ” + d);

}

}

 

Output :

Integer Value : 9

Double Value : 56.0

 

Leave a Reply

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

Security Code: