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

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


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

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


View

Write a function to multiply 2 numbers.


View

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


View

Take an array of numbers as input and check if it is an array sorted in ascending order.

Eg : { 1, 2, 4, 7 } is sorted in ascending order.

       {3, 4, 6, 2} is not sorted in ascending order.


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