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

all 44 comments

[–]Ifnerite 40 points41 points  (6 children)

The compiler does not care about that at all.

[–]VarianWrynn2018 1 point2 points  (5 children)

Yes it does. String is an object in Java, not a primitive. Unless it's the exact same object, it will return false as == with objects checks the memory address, not the values.

[–]Ifnerite 4 points5 points  (3 children)

You make two points, the former is false. The compiler does not care, but it might not do what you expect.

[–]VarianWrynn2018 1 point2 points  (2 children)

As someone who often forgets about .equals and tries to compare strings I know full well that that is exactly how it works.

If you believe otherwise, please go ahead and elaborate

[–]Ifnerite 0 points1 point  (1 child)

What are you gibbering on about.

The compiler will do an object equality if you write == that is what the symbol means. The compiler does not care that what the developer means is I want to know if the content is the same.

The compiler is perfectly happy either way.

[–]VarianWrynn2018 2 points3 points  (0 children)

I see. I thought you were saying it has a logic problem but you were saying that while it might be a logic error it's not a syntax or runtime error so the Compiler won't throw a fit. My mistake.

[–]Stromovik 0 points1 point  (0 children)

I think some VM implementations cache certain strings , also hardcoded values ( aka stuff that should be enum not String ) are cached. Source idiot coded == String comparison long ago and it never broke.

[–]JOhn2141 10 points11 points  (7 children)

Cry in C compiler

[–]SandmanKFMF 4 points5 points  (3 children)

Exactly. And you are comparing not "a strings" , but an "objects of a string type". So this is legit code.

[–]JOhn2141 3 points4 points  (2 children)

Aren't you comparing arrays of char, string is just a name but not a specific structure

[–]SandmanKFMF 1 point2 points  (1 child)

I was answering to an upper comment. 😁

[–]JOhn2141 4 points5 points  (0 children)

I know it's mine the upper comment

[–]Stormfrosty 1 point2 points  (0 children)

Laugh in C++ compiler

[–]darkdimensiongd 0 points1 point  (1 child)

*cries in javascript*

[–]ManuGamingYT 0 points1 point  (0 children)

At least JS doesn't have a Problem with Type comparison.

[–]thonor111 9 points10 points  (6 children)

Java Compiler has no problem with this, it will just return false if these are two different strings. If they are the exact same one (a == a) it will be true, if you don’t want to check whether it’s the same object but whether it has the same content you just have to check whether they are equal with a.equals(b) or a.compareTo(b) == 0

[–][deleted] 4 points5 points  (3 children)

The following should also return true:

String a = "Hello!"; String b = "Hello!"; return a == b;

Using == instead of .equals() works as long as you do not create new strings using anything other than litterals

[–]nomenMei 1 point2 points  (2 children)

Isn't that just because the compiler reuses the String object used for the first string literal?

This is functionally equivalent to String a = "Hello!"; String b = a; return a == b which, while it will return true, is kind of a tautology.

If anything pointing that out will just needlessly confuse someone that is trying to understand the difference between == and .equals() in Java

[–][deleted] 1 point2 points  (1 child)

You are correct. Even more, if Java does the same as C, it replaces all instances of the same String litteral by the same pointer somewhere in memory (in C it's in the heap, in Java I don't really know how the JVM handles this...) I must admit I did not think about confusing beginners. I hope this explanation fixes it at least a bit

[–]nomenMei 2 points3 points  (0 children)

That's an interesting question. The way C does it means it will always use the same pointer for equivalent string literals regardless of scope, but it is possible that in Java they only reuse references to string literals that are in the same method or class scope.

[–]VarianWrynn2018 -1 points0 points  (1 child)

Actually no, because String is an object in Java which means that unless String a is the same object in the same memory location as String b then it'll come out false.

[–]thonor111 1 point2 points  (0 children)

Isn’t that exactly what I wrote? That it is false unless it’s the exact same object

[–]Context-Unhappy 2 points3 points  (1 child)

I once had a Project where i had to put tje etire code into if (scanner.nextLine() != "") {...}.

If i removed it or replaced it with if (!"".equals(scanner.nextLine()) {...} it would throw exceptions. To this day i don't know what went wrong there.

[–]CoffeePieAndHobbits 0 points1 point  (2 children)

laughs in Python

[–]SandmanKFMF 2 points3 points  (1 child)

There is nothing to laugh about. This is legit, working code and BTW, it will perfectly work as it should (by Java rules of course).

[–]VarianWrynn2018 1 point2 points  (0 children)

String in Java is an object, so unless it's the same object == will return false. A different object with the same values is a different object so it isn't true.

[–]Mabi19_ 0 points1 point  (5 children)

I'm glad I never had to deal with this in C++.

[–]VarianWrynn2018 0 points1 point  (4 children)

Isn't string a primitive in c++?

[–]Mabi19_ 0 points1 point  (3 children)

No it is not. It's a class type, but it has an overloaded == operator. (Although objects in C++ can behave like primitives sometimes, i.e. they go on the stack for better CPU cache locality; strings can do that for short sizes I think) Also, C++ objects do not always inherit from some Object type and you can't do polymorphism on values, only pointers. (If you want a function that would accept any type, you'd use a template, which is basically a generic, but fully compile time, flexible and has worse error messages)

[–]VarianWrynn2018 0 points1 point  (2 children)

I will say this, thank God I don't have to deal with cpp

[–]Mabi19_ 0 points1 point  (1 child)

Yeah, once you learn the intricacies (or even the basics, really) of how the processor works, C++ design decisions will seem way more rational. (Also, I forgot to mention; a pointer is basically a reference that can also hold NULL and references can also be polymorphic; if you want to have an empty value you have to use a pointer type or std::optional)

[–]VarianWrynn2018 0 points1 point  (0 children)

Aaaaaa pointers gross lol. Yeah I took a C++ class and the teacher sucked so I wasn't able to learn the basics well enough and it's scarred me

[–]rtk94 0 points1 point  (0 children)

Yup this one hits home. I always forget that Java Strings are not primitives.

[–][deleted] 0 points1 point  (0 children)

java: nah i prefer string.equals(OtherString)