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

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 to print the sum of all odd numbers from 1 to n.


View

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


View

Find the maximum & minimum number in an array of integers.

[HINT : Read about Integer.MIN_VALUE & Integer.MAX_VALUE in Java]


View

Input a string from the user. Create a new string called ‘result’ in which you will replace the letter ‘e’ in the original string with letter ‘i’. 

Example : 

original = “eabcdef’ ; result = “iabcdif”

Original = “xyz” ; result = “xyz”


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