Integer.Tostring(Int I) VS String.Valueof(Int I)

Integer.toString(int i) vs String.valueOf(int i)

Just two different ways of doing the same thing. It may be a historical reason (can't remember if one came before the other).

String.valueOf() vs. Object.toString()

According to the Java documentation, String.valueOf() returns:

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

So there shouldn't really be a difference except for an additional method invocation.

Also, in the case of Object#toString, if the instance is null, a NullPointerException will be thrown, so arguably, it's less safe.

public static void main(String args[]) {  
String str = null;
System.out.println(String.valueOf(str)); // This will print a String equal to "null"
System.out.println(str.toString()); // This will throw a NullPointerException
}

difference between String.valueOf(int) , `+` string operator and Integer.toString(int)

For the example you have given, the answer will really depend on the type of 'integer' you are using.

loadCity(countryName , countryId + "");

For an Integer object this is equivelent to :

loadCity(countryName, countryId.toString() + "");

Whereas for an int primitive, this code is equivelent to :

loadCity(countryName, String.valueOf(countryId) + "");

In either case, as ArjunShankar pointed out there is a good chance that the compiler has optimised your code anyway. So if your question is 'do I go back and refactor all my code?', then I would say 'don't sweat the small stuff'. But in the future use a more conventional approach to avoid the down votes.

Integer.toString(int a) v. int a + v. String.valueOf()

String.valueOf calls Integer.toString: http://www.mavenjava.com/sun/jdk/1.6.0/src/java/lang/String.java.html#String.valueOf%28int%29

public static String valueOf(int i) { 
return Integer.toString(i, 10);
}

String number = 6 + ""; uses a StringBuilder and appends:

String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

(which is inherently slower than the other two ways). The best bet is to directly use Integer.toString

Java int to String - Integer.toString(i) vs new Integer(i).toString()

Integer.toString calls the static method in the class Integer. It does not need an instance of Integer.

If you call new Integer(i) you create an instance of type Integer, which is a full Java object encapsulating the value of your int. Then you call the toString method on it to ask it to return a string representation of itself.

If all you want is to print an int, you'd use the first one because it's lighter, faster and doesn't use extra memory (aside from the returned string).

If you want an object representing an integer value—to put it inside a collection for example—you'd use the second one, since it gives you a full-fledged object to do all sort of things that you cannot do with a bare int.

Difference between String.valueOf(int i) and printing only i

This is well explained in the JLS - 15.18.1. String Concatenation Operator +:

If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.

You should note the following:

The + operator is syntactically left-associative, no matter whether it
is determined by type analysis to represent string concatenation or
numeric addition. In some cases care is required to get the desired
result.

If you write 1 + 2 + " fiddlers" the result will be

3 fiddlers

However, writing "fiddlers " + 1 + 2 yields:

fiddlers 12

Why is String.valueOf faster than String Concatenation for converting an Integer to a String?

String addition results in the compiler creating a StringBuilder instance, followed by append calls for each added element, followed by a call to StringBuilder.toString() to create the resulting concatenated String instance.

So, ""+number creates a StringBuilder, appends a number using the same conversion as String.valueOf, and then creates a String instance from the StringBuilder with StringBuilder.toString.

String.valueOf(number) avoids the StringBuilder, just using the value from String.valueOf.

The answer might not be the same when the compiler can avoid all of this, if it can discern the final String result because the elements appended are all constants. In that case, the compiler just puts the final String into the compiled code.

toString vs toString(parameter) vs valueOf(parameter) in java

what is the exact difference between this three methods?

Pretty much none.

via JDK8's String.java

When you have a primitive double it can't be null and is directly forwarded to Double.

3125  public static String valueOf(double d) {
3126 return Double.toString(d);
3127 }

When you have a boxed Double value that can be null, this either prints "null" or forwards to the objects toString() method.

2978  public static String valueOf(Object obj) {
2979 return (obj == null) ? "null" : obj.toString();
2980 }

JDK 8's Double.java

To avoid code duplication this simply forwards the internal value to the static method

643  public String toString() {
644 return toString(value);
645 }

And the static method does the "actual work" by delegating to some "internal" implementation.

203  public static String toString(double d) {
204 return FloatingDecimal.toJavaFormatString(d);
205 }

Regardless of what you do. If there is a value to print it will be Double#toString(double d) that does the job. The same applies to Integer, ..

Using String.valueOf(Object) is also what happens when you do println("Value is: " + value). It prints "null" instead of trying to call the toString method on a non existing object.



Related Topics



Leave a reply



Submit