whats the difference between == and .equal ?

Answer Posted / haneef

see, every object created by the new operator, it has it own
hash code.

so when you compare with equals(), checks only content. But
with ==, it also checks the hashcode.

try this example

package app;

public class Test {
public static void main(String[] args) {

String str2 = new String("Haneef");
String str3 = new String("Haneef");

if (str2.equals(str3))
System.out.println("ok");
else
System.out.println("Not OK");

if(str2==str3)
System.out.println("ok");
else
System.out.println("Not ok");
}
}

u get

OK
NOT OK

Is This Answer Correct ?    7 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain all java features with real time examples

1210


What is method with example?

582


What are the different data types in java?

534


how to convert Java project into Maven ?

585


What is a literal coding?

513






What is a jagged array in java?

536


What about static nested classes in java?

592


Can we use both this () and super () in a constructor?

556


Does java support function overloading, pointers, structures, unions or linked lists?

607


Can we declare array without size in java?

552


When is the finalize() called?

701


Explain reverse a linked list iterative solution in java?

500


What is the different types of functions?

561


Difference between static binding and dynamic binding?

568


how to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? Or extending thread class or implementing runnable interface. Which is better? : Java thread

598