Basically there are three way to convert an primitive value to string so that if we have requirement to use that primitive as a string then we can you this concept and way of conversion but now the point is out of these three way which one is the best and take less time and doesn't have any performance issue.

The three ways of converting primitive into string are :

1. Using Empty string
2. Using valueOf() with String class
3. Using toString() with Wrapper class.

Using Empty String

With the use of an empty string we are creating an new string in the constant string pool and if that string literal is already present in the pool then it will create the object in the heap which points to the pool literal object and return the heap memory address to its original object. For ex-

public class prog {

    public static void main(String[] args) {

 String s=""+10;
 String x1="java10";
 String x2="java"+10;
 System.out.println(x1.equals(x2)); // true
 System.out.println(x1==x2);    // false
 System.out.println(x1.hashCode()==x2.hashCode());//true

    }

}

In the above example ( x2== x1 ) will give false that means s2 and s1 both are different object and which proves that x1 literal get stored in string pool first and then while storing s2 literal it will first check the pool, if present then it will create an new object in the heap memory if we are using new operator that means internally  String is created with the use of ' new ' operator. 

Using valueOf() with String class

Another option for making a primitive value to string value is the use of valueOf( int i ) defined static in the String class, which return string value and take integer value from its integer type parameter. Here i have used only int type but valueOf() can be used with any primitive type. For ex- 

public class prog {

    public static void main(String[] args) {

 int x=10;
 String s1=String.valueOf(x);
 System.out.println(s1); 
 
     }

}

using valueOf() which it self used Integer.toString( int, int ) internally to convert it into string type value.

Using toString() with Wrapper class

Every wrapper class is having overridden toString() method with parameter as any primitive type. So we have one more option to make a primitive data as string type by using toString( int i ) . Here also i have used int type but toString() is available with any primitive type as argument. For ex-

public class prog {

    public static void main(String[] args) {

 int x=10;
 String s1=Integer.toString(x);
 System.out.println(s1); 
 
     }

}

toString() method internally uses character array concept to create an String. So this is the most efficient way of converting into string value.

So it is recommended to use any of the either valueOf() or toString().

Note: It is not recommended to convert with the use of empty string . 

Related Posts:

  • " new " and " instanceof " Operator in Java new operator refers to the object creation where as instanceof used with the reference variable. Details of which we are going to discuss late in this topic.  What is " new " Operator and its uses in Java ? … Read More
  • Control statement in Java Control statement in any programming language is used to control the flow of the statement in the application. That is why it is termed as " control ". There are three types of control statement i.e 1> Conditional control… Read More
  • Type Casting in Java Type casting in java allow the users to convert one form of data type into another but it should be between same " type " that means we cannot type primitive type to String or any other reference type and vic… Read More
  • Method overloading in java Method overloading in java is the facility which allow us to write same name method with different arguments and hence different implementation.So now with the help of method overloading we can use same method name with diff… Read More
  • Object in Java In java we can define object as the instance of class. Object is used to instantiate all the non-static members. In java everything is based on Object, it means memory are allocating for the objects only. Everything we are … Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic