• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refrence

 
Harshal Sood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Team

please help me to resolve the problem.


File x = new File("myFile.txt");
File y = new File("myFile.txt");
File z = x;
System.out.println(x == y); // Outputs false
System.out.println(x == z); // Outputs true
Even though all of the variables point to the same fi le information, only two, x and z,
are equal in terms of ==. In this example, as well as during the OCA exam, you may be presented
with classnames that are unfamiliar, such as File. Many times you can answer questions
about these classes without knowing the specifi c details of these classes. In particular,
you should be able to answer questions that indicate x and y are two separate and distinct
objects, even if you do not know the data types of these objects.


How a ,b ,c references are not equal.please tell me in simple words.
 
Igor Soudakevitch
Author
Greenhorn
Posts: 25
6
Android Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects created with the new keyword are always distinct from each other. This is why the reference variables x and y point to different objects. What those objects contain is irrelevant.
The operator 'double equals' compares references to objects rather than their contents; since x and y point to different objects, the result of x == y is false.
The third statement declares a new reference variable z and assignes to it the value that the reference var x holds; from now on z points to the same object that x points to. That's why the last printing statement outputs true: the object is the same.

On a final note, please do proofread your messages before you post them: the textbook's code snippet does not even contain the reference variables a, b or c...
 
Harshal Sood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor Soudakevitch wrote:Objects created with the new keyword are always distinct from each other. This is why the reference variables x and y point to different objects. What those objects contain is irrelevant.
The operator 'double equals' compares references to objects rather than their contents; since x and y point to different objects, the result of x == y is false.
The third statement declares a new reference variable z and assignes to it the value that the reference var x holds; from now on z points to the same object that x points to. That's why the last printing statement outputs true: the object is the same.

On a final note, please do proofread your messages before you post them: the textbook's code snippet does not even contain the reference variables a, b or c...


Thanks a lot Igor Shostakovitch

and i recheck the variables are x,y,z;
 
Henry Wong
author
Marshal
Pie
Posts: 22086
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor Soudakevitch wrote:
On a final note, please do proofread your messages before you post them: the textbook's code snippet does not even contain the reference variables a, b or c...


This example comes from a book? ... Can you quote the source (QuoteYourSources link) for us?

Thanks,
Henry
 
Igor Soudakevitch
Author
Greenhorn
Posts: 25
6
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That snippet comes, naturally, from B&S, a.k.a. OCA: Oracle® Certified Associate Java® SE 8 Programmer I Study Guide Exam 1Z0-808 by Jeanne Boyarsky and Scott Selikoff.
Page 66.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic