Java Conversion Technique

16:19:00

In real scenario we need conversion from one type to other type, so with help of wrapper class mechanism , can achieve conversion from one method to other and vice versa..

 Native Method

  1. int
  2. float
  3. char
  4. double
  5. long

 Wrapper Class Metghod

  1. Integer
  2. Float
  3. Char
  4. Double
  5. Long



Conversion Types:

  1.  int to String.
  2. float to String
  3. long to String
  4. double to String
  5. char to String

  1.  String to int
  2. String to float
  3. String to long
  4. String to double
  5. String to char



Using Methods to conversion:


  1. valueof()
  2. Stringto()
  3. parseXxx()
we will discuss Detail:


Java String to int

We can convert String to int in java using Integer.parseInt() method..

Scenario
It is generally used if we have to perform mathematical operations on the string that contains number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is integer, we need to convert string to int. To do so, we use Integer.parseInt() method.

Signature

The parseInt() is the static method of Integer class. The signature of parseInt() method is given below:


public static int parseInt(String s)  

Java String to int Example
Let's see the simple code to convert string to int in java.

int i=Integer.parseInt("200");  


Let's see the simple example of converting String to int in java.

public class StringToIntExample{  
public static void main(String args[]){  
String s="200";  
int i=Integer.parseInt(s);  
System.out.println(s+100);//200100 because + is string concatenation operator  
System.out.println(i+100);//300 because + is binary plus operator  
}}  


Output:

200100
300



Java int to String

We can convert int to String in java using String.valueOf() and Integer.toString() methods.

Scenario

It is generally used if we have to display number in textfield because everything is displayed as a string in form.
1) String.valueOf()
The String.valueOf() method converts int to String. The valueOf() is the static method of String class. The signature of valueOf() method is given below:


1.         public static String valueOf(int i)  


Java int to String Example using String.valueOf()
Let's see the simple code to convert int to String in java.
1.         int i=10;  
2.         String s=String.valueOf(i);//Now it will return "10"  


Let's see the simple example of converting String to int in java.

1.         public class IntToStringExample1{  
2.         public static void main(String args[]){  
3.         int i=200;  
4.         String s=String.valueOf(i);  
5.         System.out.println(i+100);//300 because + is binary plus operator  
6.         System.out.println(s+100);//200100 because + is string concatenation operator  
7.         }}  


Output:
300


Java String to long

We can convert String to long in java using Long.parseLong() method.

Scenario

It is generally used if we have to perform mathematical operations on the string that contains long number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is long, we need to convert string to long. To do so, we use Long.parseLong() method.

Signature

The parseLong() is the static method of Long class. The signature of parseLong() method is given below:
1.         public static long parseLong(String s)  


Java String to long Example
Let's see the simple code to convert string to long in java.
1.         long l=Long.parseLong("200");  


Let's see the simple example of converting String to long in java.
1.         public class StringToLongExample{  
2.         public static void main(String args[]){  
3.         String s="9990449935";  
4.         long l=Long.parseLong(s);  
5.         System.out.println(l);  
6.         }} 


Output:
9990449935


Java long to String

We can convert long to String in java using String.valueOf() and Long.toString() methods.

Scenario

It is generally used if we have to display long number in textfield because everything is displayed as a string in form.
1) String.valueOf()
The String.valueOf() is an overloaded method. It can be used to convert long to String. The valueOf() is the static method of String class. The signature of valueOf() method is given below:
1.         public static String valueOf(long i)  


Java long to String Example using String.valueOf()
Let's see the simple code to convert int to String in java.
1.         long i=9993939399L;//L is the suffix for long  
2.         String s=String.valueOf(i);//Now it will return "9993939399"  


Let's see the simple example of converting String to int in java.
1.         public class LongToStringExample1{  
2.         public static void main(String args[]){  
3.         long i=9993939399L;  
4.         String s=String.valueOf(i);  
5.         System.out.println(s);  
6.         }}  


Output:
9993939399



Java String to float

We can convert String to float in java using Float.parseFloat() method.

Scenario

It is generally used if we have to perform mathematical operations on the string that contains float number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is float, we need to convert string to float. To do so, we use Float.parseFloat() method.

Signature

The parseFloat() is the static method of Float class. The signature of parseFloat() method is given below:
1.         public static int parseFloat(String s)  


Java String to float Example
Let's see the simple code to convert string to float in java.
1.         float f=Float.parseFloat("23.6");  


Let's see the simple example of converting String to float in java.
1.         public class StringToFloatExample{  
2.         public static void main(String args[]){  
3.         String s="23.6";  
4.         float f=Float.parseFloat("23.6");  
5.         System.out.println(f);  
6.         }}  


Output:
23.6


Java String to double

We can convert String to double in java using Double.parseDouble() method.

Scenario

It is generally used if we have to perform mathematical operations on the string that contains double number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is double, we need to convert string to double. To do so, we use Double.parseDouble() method.

Signature

The parseDouble() is the static method of Double class. The signature of parseDouble() method is given below:
1.         public static double parseDouble(String s)  


Java String to double Example
Let's see the simple code to convert string to double in java.

1.         double d=Double.parseDouble("23.6");  







Let's see the simple example of converting String to double in java.
1.         public class StringToDoubleExample{  
2.         public static void main(String args[]){  
3.         String s="23.6";  
4.         double d=Double.parseDouble("23.6");  
5.         System.out.println(d);  
6.         }}  


Output:
23.6


Java String to Date

We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes. To learn this concept well, you should visit DateFormat and SimpleDateFormat classes.

Java String to Date Example

Let's see the simple code to convert String to Date in java.
1.         import java.text.SimpleDateFormat;  
2.         import java.util.Date;  
3.         public class StringToDateExample1 {  
4.         public static void main(String[] args)throws Exception {  
5.             String sDate1="31/12/1998";  
6.             Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);  
7.             System.out.println(sDate1+"\t"+date1);  
8.         }  
9.         }  


Output:
31/12/1998        Thu Dec 31 00:00:00 IST 1998


Let's see another code to convert different types of string to Date in java.
1.         import java.text.SimpleDateFormat;  
2.         import java.util.Date;  
3.         public class StringToDateExample1 {  
4.         public static void main(String[] args)throws Exception {  
5.             String sDate1="31/12/1998";  
6.             String sDate2 = "31-Dec-1998";  
7.             String sDate3 = "12 31, 1998";  
8.             String sDate4 = "Thu, Dec 31 1998";  
9.             String sDate5 = "Thu, Dec 31 1998 23:37:50";  
10.          String sDate6 = "31-Dec-1998 23:37:50";  
11.          SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");  
12.          SimpleDateFormat formatter2=new SimpleDateFormat("dd-MMM-yyyy");  
13.          SimpleDateFormat formatter3=new SimpleDateFormat("MM dd, yyyy");  
14.          SimpleDateFormat formatter4=new SimpleDateFormat("E, MMM dd yyyy");  
15.          SimpleDateFormat formatter5=new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");  
16.          SimpleDateFormat formatter6=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");  
17.          Date date1=formatter1.parse(sDate1);  
18.          Date date2=formatter2.parse(sDate2);  
19.          Date date3=formatter3.parse(sDate3);  
20.          Date date4=formatter4.parse(sDate4);  
21.          Date date5=formatter5.parse(sDate5);  
22.          Date date6=formatter6.parse(sDate6);  
23.          System.out.println(sDate1+"\t"+date1);  
24.          System.out.println(sDate2+"\t"+date2);  
25.          System.out.println(sDate3+"\t"+date3);  
26.          System.out.println(sDate4+"\t"+date4);  
27.          System.out.println(sDate5+"\t"+date5);  
28.          System.out.println(sDate6+"\t"+date6);  
29.      }  
30.      }  


Output:
31/12/1998        Thu Dec 31 00:00:00 IST 1998
31-Dec-1998     Thu Dec 31 00:00:00 IST 1998
12 31, 1998        Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998                           Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998 23:37:50        Thu Dec 31 23:37:50 IST 1998
31-Dec-1998 23:37:50                 Thu Dec 31 23:37:50 IST 1998


You Might Also Like

0 comments

Popular Posts

Like us on Facebook