P
P
Polina2020-02-28 20:46:18
Java
Polina, 2020-02-28 20:46:18

Why Object.toString(obj) != String.ValueOf(obj)?

Google writes that these methods are equivalent, except that when null ValueOf throws a NullPointerException, while toString does not.

But if you run

System.out.ptintln(Object.toString(obj) ==  String.ValueOf(obj))
java will print "false" to the console.
Please explain why this is so, ideally an explanation based on the documentation. I don’t understand on my own (I

also wonder if it’s correct to say that when writing String str = Object.toString(obj) we put a reference to the structure of the obj object in the String variable?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-02-28
Mezenina @medigi27

Because in Java, the equality operator is not applicable for checking the equality of objects. Canonical answer to this thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question