Wrapper Class in Java : – Convert String into Primitive Data Type
Primitive Data Type | Wrapper Class |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
Example 1 : String to Integer
public class A {
public static void main(String[] args) {
String x ="100";
int i = Integer.parseInt(x);
System.out.println(i);
}
}
Output : 100
Example 2 : String to Double
public class A {
public static void main(String[] args) {
String z ="10.3";
double d = Double.parseDouble(z);
System.out.println(d);
}
}
You Also Learn –
- What is inheritance in Java
- What is Abstract Class in Java
- What is Lambda Expression in Java 8
- Types of Exception in Java
- What is Final Keyword in Java
- What is Exception Handling in Java
- What is Interface in Java
Wrapper Class in Java
Output : 10.3
Example 3 : String to Boolean
public class A {
public static void main(String[] args) {
String k = "true";
boolean b = Boolean.parseBoolean(k);
System.out.println(b);
}
}
Output : true
Example 4 : String to Float
public class A {
public static void main(String[] args) {
String o= “20.34”;
float f = Float.parseFloat(o);
System.out.println(f);
}
}
Output : 20.34