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 to multiply 2 numbers.


View

Two numbers are entered by the user, x and n. Write a function to find the value of one number raised to the power of another i.e. x^n.


View

Print the spiral order matrix as output for a given matrix of numbers.


View

Write a function to calculate the product of 2 numbers.


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

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