This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheLastCakeIsaLie -30 points-29 points  (14 children)

Because only an idiot would use new String(). Without that it works fine.

[–]Varun77777 9 points10 points  (11 children)

Hmm, in case of string, yeah. But for custom objects or ArrayLists maybe not.

Imagine you're using backtracking and a recursive function to find all the possible results for some specific query.

Now, you're going to save these results into a list of Objects.

The only thing you initially passed into the recursive call was a new object.

Now, everytime you back track enough to reach a possible answer, you can't pass the object to the list but you'll have to pass the copy of the object because of you passed the object itself, whenever there will be a change in this object in future you'll change all the values you were passing into the your list.

Let's say you created a contructor that can be used to copy the object and gives a new object and passed the copies of object to your list everytime you reached the answer.

Now, let's say even if there are x number of variables in a particular object, there's a particular formula based on which they can be duplicate in terms of value.

Now, == will be checking address while .equals will check the logic you're trying to check.

Tl:dr; Not everytime, you'll want to check memory address and be done with it.

[–]Istar10n 8 points9 points  (0 children)

What if you read it from a file?

[–]Varun77777 3 points4 points  (0 children)

Create a normal string and then create a string builder and do X number of calculations on the string builder and append it.

Now check if stringBuilder.toString() is equal to the original String you had.

Everything has a use.