Monday, September 16, 2013

Data conversions among Primitive, Wrapper, String..

Data type conversions:

Generally, We have huge usage of Primitives, Wrappers and Strings usage in any JAVA project.

So, In this post I am writing most important data conversion techniques which are used mostly in all projects.
And most of the people have confusion on these conversions that what API they have to use for what data type .


Our Requirement:  (String, Wrapper, Primitive)

1) String to Wrapper
2) String to Primitive
3) Wrapper to String
4) Wrapper to Primitive
5) Primitive to String
6) Primitive to Wrapper

The above conversions used extensively.


---------------------------------------------------------------------------------------------------------------

1) String to Wrapper conversion:

-For this we have Wrapper class API, valueOf() method in each and every Wrapper class which takes String as parameter and converts into Wrapper ( Integer, Float,...etc)

static IntegervalueOf(String s)
          Returns an Integer object holding the value of the specified String.

like wise we have methods for all primitives

example usage:
                Integer iStr33=Integer.valueOf("33");
System.out.println(iStr33);

---------------------------------------------------------------------------------------------------------------

2) String to Primitive conversion: 
-For this, we have Wrapper API, parseXXX() method in each and every Wrapper class which takes String as parameter and converts into primitives ( int, float,...etc).

static intparseInt(String s)
          Parses the string argument as a signed decimal integer.

like wise we have methods in all Wrapper classes.

example usage:
                int i=Integer.parseInt("33");
System.out.println(i);

---------------------------------------------------------------------------------------------------------------

3) Wrapper to String conversion:
-For this we have toString() method in each and every Wrapper class.

static StringtoString(int i)
          Returns a String object representing the specified integer.

like wise we have this method in all Wrapper classes.

example usage:
                        String strI=Integer.toString(10);
System.out.println(strI);

---------------------------------------------------------------------------------------------------------------

4) Wrapper to Primitive conversion:
- For this we have xxxValue() method in each and every wrapper class.

 intintValue()
          Returns the value of this Integer as an int.

like wise we have floatValue, doubleValue....etc in all wrapper classes.

example usage:
                        Integer iW=new Integer(22);
int i=iW.intValue();
System.out.println(i);

---------------------------------------------------------------------------------------------------------------

5) Primitive to String conversion:  

- For this conversion we have String API, And this String class has many Overloaded methods.
- And also we have Wrapper class API,

Below table shows you how to convert any primitive to String.

String API:
static StringvalueOf(boolean b)
          Returns the string representation of the boolean argument.
static StringvalueOf(char c)
          Returns the string representation of the char argument.
static StringvalueOf(char[] data)
          Returns the string representation of the char array argument.
static StringvalueOf(char[] data, int offset, int count)
          Returns the string representation of a specific subarray of the char array argument.
static StringvalueOf(double d)
          Returns the string representation of the double argument.
static StringvalueOf(float f)
          Returns the string representation of the float argument.
static StringvalueOf(int i)
          Returns the string representation of the int argument.
static StringvalueOf(long l)
          Returns the string representation of the long argument.
static StringvalueOf(Object obj)
          Returns the string representation of the Object argument.

Wrapper API Integer class:
static StringtoString(int i)
          Returns a String object representing the specified integer.

example usage:

           1)  int x=10;
String strX=String.valueOf(x);
System.out.println(strX); 
               
           2)  String iI = Integer.toString(22);
System.out.println(iI);

---------------------------------------------------------------------------------------------------------------

6) Primitive to Wrapper conversion:
-For this we have valuOf() method in all wrapper classes.
-And also we can create directly using constructor.

type1:
static FloatvalueOf(float f)
          Returns a Float instance representing the specified float value.

type2:
Float(float value) 
          Constructs a newly allocated Float object that represents the primitive float argument.


like wise we have this type of method in all wrapper classes.

example usage:
                        Integer i=new Integer(34);
Integer j=Integer.valueOf(44);

---------------------------------------------------------------------------------------------------------------



The above are some of the techniques we use them mostly in every java project.

Please let me know any mistakes are in this post, Send your feedback at nagarjuna.lingala@gmail.com  or javaojavablog@googlegroups.com

sources :
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html

No comments:

Post a Comment