/**
 * Converting from all primitive types to Strings
 * Converting from Strings to numeric and char
*/

public class stringConvert 
{
    public static void main(String[] args) 
    {        
        int     i = -1;
        byte    b =  1;
        short   s =  2;
        long    l =  3;
        float   f =  4.1f;
        double  d = -5.2;
        boolean o =  true;
        char    c = 'h';

        String I;
        String B;
        String S;
        String L;
        String F;
        String D;
        String O;
        String C;

        System.out.println("Converting to strings using valueOf()");

        I  =  String.valueOf(i);        //convert to String
        System.out.println(I);
        B  =  String.valueOf(b);
        System.out.println(B);
        S  =  String.valueOf(s);
        System.out.println(S);
        L  =  String.valueOf(l);
        System.out.println(L);
        F  =  String.valueOf(f);
        System.out.println(F);
        D  =  String.valueOf(d);
        System.out.println(D);
        O  =  String.valueOf(o);
        System.out.println(O);
        C  =  String.valueOf(c);
        System.out.println(C);

        System.out.println("Converting to strings using toString()");

        I  =  Integer.toString(i);      //another way
        System.out.println(I);
        B  =  Byte.toString(b);
        System.out.println(B);
        S  =  Short.toString(s);
        System.out.println(S);
        L  =  Long.toString(l);
        System.out.println(L);
        F  =  Float.toString(f);
        System.out.println(F);
        D  =  Double.toString(d);
        System.out.println(D);
        O  =  Boolean.toString(o);
        System.out.println(O);
        C  =  Character.toString(c);
        System.out.println(C);

        System.out.println("Converting to strings using + \"\" ");

        I = i + "";                     //yet a third way
        System.out.println(I);
        B = b + "";                     
        System.out.println(B);
        S = s + "";                     
        System.out.println(S);
        L = l + "";                     
        System.out.println(F);
        F = f + "";                     
        System.out.println(L);
        D = d + "";                     
        System.out.println(D);
        O = o + "";                     
        System.out.println(O);
        C = c + "";                     
        System.out.println(C);
                
        System.out.println("Converting from string to numeric");

        String text   = "-53.26";
        String text2  = "ABCD";
        int     num;
        long    num1;
        float   num2;
        double  num3;
        char    ch;
                
        try {
            num  =  Integer.parseInt(text);     //convert to integer
            System.out.println(num);
            num1 =  Long.parseLong(text);       //convert to long
            System.out.println(num1);
        } 
        catch (Exception e) {
            System.out.println("not an integer");
        }

        try {
            num2 =  Float.parseFloat(text);     //convert to float
            System.out.println(num2);
            num3 =  Double.parseDouble(text);   //convert to double
            System.out.println(num3);
        } 
        catch (Exception e) {
            System.out.println("not a decimal");
        }

        ch = text2.charAt(2);                   //convert to character 
                
        System.out.println(ch);
    }
}