Why does == work on integers but not on strings?


This question already has an answer here:


Because the value of an object is the memory location which holds the object values, and the value of primitive types is its value itself. If you're using == on objects of type String for example, you are comparing their memory locations.

== compares references because string is an Reference type while int is a value type.

That's because in Java strings are objects, and objects will only be equal if their reference is. (They are the same object)

For string, you have to use equals() instead of == because it is an object.

Need Your Help

Custom values in Google Cloud Logging / Metrics

google-kubernetes-engine google-cloud-logging

Can Google Cloud Logging / Metrics support custom values? E.g. can a log line contain a key-value pair, such as its value is included in metrics as value, as opposed to having to print the key mult...

How to quickly calculate what points are within a given distance of my point

java math geometry coordinates

I have a large number of Longitude and Latitudes and I want to quickly find out which ones are within say a 5km radius of a certain Longitude Latitude.