DEV Community

karthik alapati
karthik alapati

Posted on

Difference between equals and == in java ?

Difference between equals and == in java

String in Java is not a primitive type and it is a Object type. It means it will allocate the memory in Heap. There are usually two ways of defining an string in Java

1) with Literal : String s = “Hello India”;

2) with new Keyword : String s = new String(“Hello India”);

There is a difference between memory allocation for both the types. For 1st type, it will allocate the memory in String Constant pool which is an integral part of Heap Memory and for 2nd type it will allocate the memory in Heap.

Note: Refer that String Constant pool link what it will do internally

Coming the point of difference between == and equals() method. Both are used for the comparison but one is a operator and another one is method. “==”is for address comparison and equals() method is for content comparison.

Refer : https://academyera.com/difference-between-equals-and-in-java

Top comments (0)