Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| |||||||||||
Core Java - Interview Questions and Answers for 'Object equality' - 7 question(s) found - Order By Newest | |||||||||||
Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems | |||||||||||
| |||||||||||
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory. x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object. Sample code: String x = new String("str"); String y = new String("str"); System.out.println(x == y); // prints false System.out.println(x.equals(y)); // prints true | |||||||||||
Sample Code for equals | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   string comparison   string   object class   ==   equals   object equality  operator   == vs equals   equals vs == Asked in 294 Companies basic   frequent | |||||||||||
Try 6 Question(s) Test | |||||||||||
| |||||||||||
Ans. truefalse | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   coding   equality   object equality | |||||||||||
| |||||||||||
Ans. false false | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  object equality  code  coding   frequent | |||||||||||
| |||||||||||
Ans. = is the assignment operator that assigns the result of the expression on the right to the variable on the left, whereas == is the operator to check object equality to see if the reference on left and right are pointing to the same object. For primitive types, its used to check if both variables holds the same value. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  =  ==  assignment operator  object equality  difference between Basic | |||||||||||
| |||||||||||
Ans. For objects or references, == operator check if the reference on left and right points to the same object. For primitive types or variables, == operator check if the variable on left and right holds the same value. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  ==  object equality  operator Basic | |||||||||||
| |||||||||||
Ans. It will print "true" with integers as well as strings. The reason is "Integer constant pool" and "String pool" String pool maintains pool of string literals. When a string literal is used for the first time, a new string object is created and is added to the pool. Upon it's subsequent usage , the reference for the same object is returned. Similarly java uses integer constant pool. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  string pool  object equality  == | |||||||||||
| |||||||||||
Ans. false false | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  object equality  ==  coding  code Asked in 1 Companies | |||||||||||
| |||||||||||
| |||||||||||