2

I am still confused what is the exact difference between this three methods

For example:

Double d = 19.23456;
System.out.println(d.toString());
System.out.println(Double.toString(d));
System.out.println(String.valueOf(d));

All the above methods print the value, but I would like to know more of a logical meaning and differences between these methods. Which method is suitable for which scenarios?

I have gone through the below link where the accepted answer states toString(parameter) and valueOf(parameter) are the same. Then what about toString? May be I am missing a significant point or something. If so please turn my attention towards it.

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

I have read the documentation but I couldn't make out the differences after reading it.

According to the official docs:

toString

 Returns a string containing a concise, human-readable description of this object.
 Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. 
The default implementation is equivalent to the following expression: 

           getClass().getName() + '@' + Integer.toHexString(hashCode())

toString(parameter)

Returns a string containing a concise, human-readable description of the specified double value

valueOf(parameter)

Converts the specified object to its string representation. If the object is null return the string "null", otherwise use toString() to get the string representation.

Thank you in advance

EDIT:

Well I am satisfied by both the answers provided by Eran and zapl. Each of the answers clears the concepts of this method within a specific perspective. For the sake of accepting an answer I would be accepting one. Thank You for clearing my doubt.

Community
  • 1
  • 1
sanedroid
  • 1,036
  • 4
  • 16
  • 27

2 Answers2

3

The difference between Integer.toString() and Integer.toString(int) is that the latter is a static method, and therefore can be called for a primitive variable. The former is an instance method, and can only be called on a reference type.

String.valueOf(Object) is different than the toString() instance methods, since String.valueOf(null) would return "null", while null.toString() would throw a NullPointerException. Integer.toString(int) will also throw a NullPointerException when pass a null Integer reference, since the attempt to unbox that Integer would result in that exception.

Therefore, if you have an Integer i variable, String.valueOf(i) will behave differently than i.toString() and Integer.toString(i) when i is null.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • `Integer.toString(i)` doesn't (or at least *shouldn't*) autobox `i` to a primitive. It *should* give a compilation error – blgt Nov 14 '14 at 12:47
  • 1
    @blgt I just tried `Integer i= null;String s = Integer.toString(i);` and got `NullPointerException`. What are you basing your claim on? – Eran Nov 14 '14 at 12:51
  • @blgt Based on the [JLS 5.3](https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.3), `Method invocation contexts allow ... an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion`. – Eran Nov 14 '14 at 12:54
  • @Eran -- Thank you for clearing much of my confusion.. so I can use toString and valueOf when I am working with objects and primitive types both? But toString(parameter) is only for primitive types.. Also both toString methods will throw nullpointer exception while valueOf won't and just return string representation of null? Have I understood it correctly?? – sanedroid Nov 14 '14 at 13:25
  • 1
    @pooh1527 Almost. You got it mixed up. `toString()` is only for objects. toString(parameter) can be used for both objects and primitives (since objects can be unboxed into primitives). – Eran Nov 14 '14 at 13:32
  • @Eran -- and what about custom objects?? objects of my custom class? – sanedroid Nov 14 '14 at 13:41
  • @pooh1527 In your own classes it's up to you whether to override toString() or not. – Eran Nov 14 '14 at 13:52
  • @Eran You're right, no error and no warning. I'm apparently basing my claim on misremembering stuff – blgt Nov 14 '14 at 14:13
2

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.

zapl
  • 63,179
  • 10
  • 123
  • 154
  • You mean when I am using valueOf(parameter) or toString, toString(parameter) method is called for both.. So if I am aware of the parameter type.. I should go for toString(parameter) assuming that would be more direct implementation?? – sanedroid Nov 14 '14 at 12:52
  • @pooh1527 Most of the time, you would simply concatenate via `"stuff" + value` and it does the right thing for you. I would consider that the best because it's the simplest approach. When you need an explicit `String` value, I'd suggest using `String.valueOf(value)` because it does the right thing with `null` too and you don't have to hardcode a specific class and `toString(value)` method. How exactly the implementation looks should not concern you, so you should not start using `toString(param)` just because it is more direct - maybe thats the case in this Java version only. – zapl Nov 14 '14 at 14:19