Hi everyone,
I'm a mid-level engineer who recently transitioned from C# to Java.
I just migrated our entire tech stack from C# to the Java ecosystem.
We also welcomed a new colleague who previously used PHP.
During my guidance on Java, we had the following conversation:
New colleague: Why do we use equals()
for string comparison instead of ==
or ===
?
Me: Because String
is an object.
New colleague: Then why can we concatenate strings with the +
operator?
Me: The +
operator is actually shorthand for StringBuilder.append()
. From an object-oriented perspective (OOP), you can also use string1.concat(string2)
.
New colleague: Why isn't the +
operator used for BigDecimal
addition here?
Me: Because BigDecimal is also an object...
New colleague: Following that logic, if strings are objects, shouldn't we create them with String string1 = new String()
and then string1.setValue(new char[] { 's', 't', 'r', 'i', 'n', 'g' })
?
Me: That would be too verbose. By the way, how did you compare strings in PHP?
New colleague: We used ===
. strcmp()
was an option too, but less common.
After many more questions...
Me: Don't overthink it, just write code and get used to it.
Do you have any better explanations for Java's design choices that are easier for beginners to understand?
P.S. I previously asked a similar question on the amber-dev mailing list,
but I don't know how to explain the "blessed type".