NA   2024   Semester -NA    Subject -Java

What is the difference between == and .equals() in Java?


Difference between == and .equals() in Java:

 

== is used to compare the memory addresses of objects, while .equals() compares the contents of objects.

 

Example: 

String str1 = new String("Hello");

String str2 = new String("Hello");

System.out.println(str1 == str2); // false (different memory addresses)

System.out.println(str1.equals(str2)); // true (same content)



Share to whatsapp

More Questions from Java Module 0

Write a function that takes in the radius as input and returns the circumference of a circle.


View

Input an email from the user. You have to create a username from the email by deleting the part that comes after ‘@’. Display that username to the user.

Example : 

email = “mejona@gmail.com” ; username = “mejona” 

email = “helloWorld123@gmail.com”; username = “helloWorld123”


View

Write a function to calculate the product of 2 numbers.


View

Take an array of names as input from the user and print them on the screen.


View

Take an array of Strings input from the user & find the cumulative (combined) length of all those strings.


View
What is the difference between == and .equals() in Java?
View

Write a function that takes in age as input and returns if that person is eligible to vote or not. A person of age > 18 is eligible to vote.


View