Jump to content

Oka Java Interview Question (Mostly Asked)


BENDU_APPARAO

Recommended Posts

difference between == and equals()

first do not confuse with [b]= (assignment operator) [/b] and [b]== (comparison operator)[/b]


[u][b]== operator[/b][/u]

Java lo eppudinaa object ni create chesthy (using new operator or class.forname ila ) ... java aa object ni JAVA HEAP meomory lo create chesthadi ....

aa memory ni access chesyadaniki manaki oka reference isthudni

[b]Employee solluEmployee = new Employee();[/b]

ikkada new Employee() ki heap memory lo space create ayyi dhaniki oka reference create ayyi adi solluEmployee anee reference lo store avuthundi (reference is always left side of =)


now == antee adi reference of the objects are pointing to same object in heap memory ani check chesthundi .......

now if I say [b]Employee[/b] [b]wasteEmployee = solluEmployee ; (pointing to old object) ... and if(solluEmployee == wasteEmployee) this is true......[/b]
now if I say [b]Employee[/b] [b]wasteEmployee = new Employee(); (kotha object creating)[/b][b]... and if(solluEmployee == wasteEmployee) this is false......[/b]

[u][b]equals() method....[/b][/u]


equals() method anedhi Super class Object class lo untundi so by default anni classes ki vosthudi (Y it is in Object class???) ....

idhi chala simple content of the object check chesthundi ......(general ga manam override cheyali to check our objects are equal) ......
so ikkada oka employee id [b]1001[/b] anukooo now if I am comparing this employee object with another employee object to see both object are related to same employeee ...... now i can say [b]employee.id.equals(anotheremployee.id); [/b]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...