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

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 that takes in the radius as input and returns the circumference of a circle.


View

Enter 3 numbers from the user & make a function to print their average.


View

Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered. 


View

For a given matrix of N x M, print its transpose in java.


View

Write a function to multiply 2 numbers.


View

Write a function that calculates the Greatest Common Divisor of 2 numbers.


View