BLUEJ THEORY 2

In this article, we will look at how to convert a String to the primitive data types – int, long, float and double.
Before going into this topic, we need to know a few details about wrapper classes. There are eight wrapper classes, one for each of the eight primitive data types. These wrapper classes are Byte, Short, Integer, Long, Float, Double, Character and Boolean. Each of these wrapper classes contains a static method (which you can call without creating an object of that class type) of the form parseDATATYPE() where data type should be replaced with the corresponding data type. These methods accept a String, convert it to the corresponding data type and return it.
For example, in the Integer class, the method is parseInt(). It can be used in the following way.
int result = Integer.parseInt(“34″);
The variable result will hold the int value 34. Note that there is a difference between “34″ and 34. “34″ is a String while 34 is an int.
The methods for int, long, float and double along with their usage is shown in the table below.
Wrapper ClassMethodUsageResult
IntegerparseInt()int intVal = Integer.parseInt(“34″);34
LongparseLong()long longVal = Long.parseLong(“34″);34
FloatparseFloat()float floatVal = Float.parseFloat(“3.4″);3.4
DoubleparseDouble()double doubleVal = Double.parseDouble(“3.4″);3.4
Following is a program illustrating the usage of the above methods:
1public class Conversion {
2 
3    public static voidmain(String[] a) {
4        int intVal = Integer.parseInt("34");
5        long longVal = Long.parseLong("34");
6        float floatVal = Float.parseFloat("3.4");
7        double doubleVal = Double.parseDouble("3.4");
8        System.out.println(intVal);
9        System.out.println(longVal);
10        System.out.println(floatVal);
11        System.out.println(doubleVal);
12    }
13 
14}
The output will be
134
234
33.4
43.4

valueOf() method

Each of the four wrapper classes discussed above also have another method by name valueOf() which takes a String converts it to an Integer, Long, Float or Double. Note that long is a primitive data type while Long is a class type used to wrap a long.
It’s usage is shown below for long type.
Long longObject = Long.valueOf(“34”);
longObject will hold an object of type Long which wraps the primitive long value 34.
There is a concept called as auto unboxing where you can assign a wrapper class object to a primitive type variable. By doing so, the wrapper class object will be automatically converted to a primitive type.
Consider the Long.valueOf() method.
long longVal = Long.valueOf(“34”);
longVal will hold 34.
The other methods are shown in the table below.
MethodReturn TypeCan be assigned toUsageResult
Integer.valueOf(String)Integerintint intVal = Integer.valueOf(“34″);34
Long.valueOf(String)Longlonglong longVal = Long.valueOf(“34″);34
Float.valueOf(String)Floatfloatfloat floatVal = Float.valueOf(“3.4″);3.4
Double.valueOf(String)Doubledoubledouble doubleVal = Double.valueOf(“3.4″);3.4

Given below is a program illustrating the usage of the above methods.
1public class Conversion {
2 
3    public static voidmain(String[] a) {
4        int intVal = Integer.valueOf("34");
5        long longVal = Long.valueOf("34");
6        float floatVal = Float.valueOf("3.4");
7        double doubleVal = Double.valueOf("3.4");
8        System.out.println(intVal);
9        System.out.println(longVal);
10        System.out.println(floatVal);
11        System.out.println(doubleVal);
12    }
13 
14}
The output will be
134
234
33.4
43.4

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...