Page 331 - CA_Blue( J )_Class10
P. 331

Let us take an example:
                    float fn = 12.4f;
                    String sl = Float.toString(fn);
                    System.out.println("Result: " + sl);
                    Output:
                    Result: 12.4f

                 13.5.2 Methods to Convert String to Primitive Data Types
                 Every wrapper class contains methods to convert a string value to primitive data types. Some of them are:
                 •  Integer.parseInt(String): The Integer.parseInt(String) method is used to convert a string value into integer type
                    value. It takes a string value as parameter and returns after converting it into int. It converts a string value into
                    integer if and only if the passed value does not contain a letter or symbol. Syntax is:

                    int variable = Integer.parseInt(String);
                    Let us take an example:
                    String s= "120";
                    int n;
                    n=Integer.parseInt(s);
                    System.out.println("Result: " + n);
                    Output:
                    Result: 120
                    The Integer.valueOf(String) method of the Integer class can also be used to convert a string value into integer.
                    Syntax is:
                    int variable = Integer.valueOf(String);
                    Let us take an example:

                    String s= "120";
                    int n;
                    n=Integer.valueOf(s);
                    System.out.println("Result :" + n);
                 •  Long.parseLong(String): The Long.parseLong(String) method is used to convert a string value into long type value.
                    It takes a string value as parameter and returns after converting it into long. It converts a string value into long if
                    and only if the passed value does not contain a letter or symbol. Syntax is:

                    long variable = Long.parseLong(String);
                    Let us take an example:
                    String s= "34123455";
                    long ln;
                    ln=Long.parseLong(s);
                    System.out.println("Result: " + ln);
                    Output:
                    Result: 34123455
                    The Long.valueOf(String) method of the Integer class can also be used to convert a string value into integer. Syntax
                    is:
                    long variable = Long.valueOf(String);
                    Let us take an example:

                    String s= "34123455";
                    long ln;
                    ln=Long.valueOf(s);
                    System.out.println("Result: " + ln);
                    Output:
                    Result: 34123455



                                                                                                                       329
                                                                                                        Library Classes    329
   326   327   328   329   330   331   332   333   334   335   336