java == vs equals
Java
In general both equals() and == operator in Java are used to compare
objects to check equality but here are some of the differences between the two:
1) .equals() and == is that one is a method and other is operator.
2) We can use == operator for reference comparison (address comparison)
and .equals() method for content comparison.
-> == checks if both objects point to the same memory location
-> .equals() evaluates to the comparison of values in the objects.
3) If a class does not override the equals method, then by default it
uses equals(Object o) method of the closest parent class
that has overridden this method.
// Java program to understand
// the concept of == operator
public class Test {
public static void main(String[] args)
{
String s1 = new String("HELLO");
String s2 = new String("HELLO");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
Output:
false
true
Explanation: Here we are creating two (String) objects namely s1 and s2.
Both s1 and s2 refers to different objects.
-> When we use == operator for s1 and s2 comparison then the result is false
as both have different addresses in memory.
-> Using equals, the result is true because its only comparing the
values given in s1 and s2.
Also in Java:
- Title
- get number of items in arraylist java
- Category
- Java
- Title
- java log base 2
- Category
- Java
- Title
- set iteration java
- Category
- Java
- Title
- iterate through an arraylist java
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- enum with numbers java
- Category
- Java
- Title
- java remove double spaces
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- how to create an array of arraylist in java
- Category
- Java
- Title
- how to make a minecraft hacked client drawing the hud malicious intent
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- how to append to an array in java
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- java stack pop
- Category
- Java
- Title
- create color object java
- Category
- Java
- Title
- java processbuilder example
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- java hahmap
- Category
- Java
- Title
- java string to int
- Category
- Java
- Title
- Java copy file
- Category
- Java
- Title
- java create txt file
- Category
- Java
- Title
- java put a char array into a string
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- comparable on a generic class java
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- java - get open ports
- Category
- Java
- Title
- convert localdatetime to localdate
- Category
- Java
- Title
- java create directory
- Category
- Java
- Title
- how to send http post create request using curl command
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- java variable declaration
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- java script print date in YYYY-MM-DD HH:MM:SS format
- Category
- Java
- Title
- how to create a 2d arraylist java
- Category
- Java
- Title
- editer un label java
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- even or odd in java
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- add in list java
- Category
- Java
- Title
- binary numbers java
- Category
- Java
- Title
- java not instanceof
- Category
- Java
- Title
- java hashmap size
- Category
- Java
- Title
- how to create a circle in java
- Category
- Java
- Title
- java script dynamic varibale creation
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- loop collection android
- Category
- Java
- Title
- java double format
- Category
- Java
- Title
- java program to display characters from a to z using loop
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java
- Title
- arraylist set method
- Category
- Java
- Title
- java replace element in list
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- how to echo java_home in windows cmd
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- how to copy list item to another list in java
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java
- Title
- java random seed
- Category
- Java
- Title
- rgb to hex java
- Category
- Java
- Title
- java output length of each line in a text document
- Category
- Java
- Title
- declare String in java
- Category
- Java
- Title
- how to split string with dolor sign in java
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- java implement interface
- Category
- Java
- Title
- java check if a line is enclosed in quotation marks
- Category
- Java
- Title
- java calendar add minutes
- Category
- Java
- Title
- JavaFX mouse over effe
- Category
- Java
- Title
- java while loop break
- Category
- Java
- Title
- java pause program until key pressed
- Category
- Java
- Title
- Don't use a line-beased input after a token-based input.
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- android dynamically create layer-list with item and shape site:stackoverflow.com
- Category
- Java
- Title
- string length in java
- Category
- Java
- Title
- if else program in java
- Category
- Java
- Title
- random boolean java
- Category
- Java
- Title
- IN APPLet how to disable a button in java
- Category
- Java
- Title
- how to check internet is working or not in java
- Category
- Java
- Title
- java split into arraylist
- Category
- Java
- Title
- what is a variable in java
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- how to detect if someone clicks on a jpanel in java
- Category
- Java
- Title
- how to print an array in java
- Category
- Java
- Title
- regex get string between quotes java
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- bubble sort in java
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- math minimum java
- Category
- Java
- Title
- java random number
- Category
- Java