Page 314 - ComputerScience_Class_11
P. 314
5 String str1= "2022";
6 int n=Integer.valueOf(str1);
7 // converts "2022" to 2022.
8 String str2= "20.23";
9 double d=Double.valueOf(str2);
10 // converts "20.23" to 20.23.
11 System.out.println(str1);
12 System.out.println(str2);
13 }
14 }
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
2022
20.23
The toString(datatype value) Method
This method is used to convert a wrapper object or a primitive value to a String. The syntax is:
<String> <variable>= <Wrapper class>.toString(datatype variable);
Let us see the below example:
Write a program to convert an integer and a double to strings using toString() method and
Program 31
print the result.
1 class string
2 {
3 public static void main(String[] args)
4 {
5 int num=2022;
6 String str=Integer.toString(num);
7 //converts 2022 to "2022"
8 double dou=20.22;
9 String str1=Double.toString(dou);
10 //converts "20.22" to 20.22
11 System.out.println(str);
12 System.out.println(str1);
13 }
14 }
312 Touchpad Computer Science (Ver. 3.0)-XI

