• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

4 Out of 5 Java Developers Failed to Solve This Problem

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They say 4 Out of 5 Java Developers Failed to Solve This Problem (one of them)
A. m1 & new name
B. None of the above
C. m1 & m1
D. new name & new name

At first I confused then I saw that toString() method didn’t override so it was printed hashcode. Therefore the correct answer is option B.

Then I think that what would be result if we override toString() method?

I thougt the correct answer would be option D - new name & new name. Because we assign m2 to m1 in line 1 and I think m1 and m2 refer to the same object. But my answer was wrong. The correct answer would be option A - m1 & new name. I think my mistake is regarding pass-by-value. If line 1 would be
m[0] = new MyClass(); m[0] = "new name";
I wouldn’t confused.
I understand if there is any variable assignment inside method it doesn’t affect the caller because we pass the copy of reference. Do I think correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Please QuoteYourSources.

Henry


Sorry, I thought it might be advertisement if I wrote the website name so I didn't it. It is source.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:If line 1 would be
m[0] = new MyClass(); m[0] = "new name";
I wouldn’t confused.

Then the answer would still be B, because the code doesn't compile

Mushfiq Mammadov wrote:I understand if there is any variable assignment inside method it doesn’t affect the caller because we pass the copy of reference. Do I think correct?

The callMe method has a var-args parameter and is invoked by passing two objects to this method. So behind the scenes a temporary array is created which is only known to the callMe method. So after line1 is executed, both elements in the array will refer to the same MyClass instance (to which m2 is refering as well). And then the name of this instance is changed to "new name". The name of the other MyClass instance will never be changed as it can no longer be refered to after line1 is executed.

What do you think about this code?

Hope it helps!
Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a way it's a trick question because it leads you down the path to thinking it's going to print m1.name and m2.name, but it really prints m1 and m2. It's testing how much attention you're paying to the question.

Mushfiq Mammadov wrote:
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Mushfiq Mammadov wrote:If line 1 would be
m[0] = new MyClass(); m[0] = "new name";
I wouldn’t confused.

Then the answer would still be B, because the code doesn't compile

Oh, sorry, you are right, Roel, m[0] = "new name"; should be m[0].name = "new name";


Roel De Nijs wrote:What do you think about this code?

I make mistake again But then I understand what is wrong in my answer. I remember that I have learned pass by value regarding reference type as this:

According the above rule I think the value of m would be changed and the result would be new name & new name. But changing value affect only array object (which m is referring) not m1 and m2. If we write System.out.println(m[0] + " & " + m[1]); the result would be new name & new name.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:In a way it's a trick question because it leads you down the path to thinking it's going to print m1.name and m2.name, but it really prints m1 and m2. It's testing how much attention you're paying to the question.

You are right, I also confused this point at first glance)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:But changing value affect only array object (which m is referring) not m1 and m2.

Exactly!

Mushfiq Mammadov wrote:If we write System.out.println(m[0] + " & " + m[1]); the result would be new name & new name.

That would have been my next code example (with the same question), but you already solved it flawlessly. Well done!
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:That would have been my next code example (with the same question), but you already solved it flawlessly. Well done!

Thanks for your interesting code example
 
Gordon Brown
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it fair too see this as a language design flaw? Q: To what does m[1] refer, the container of the contents? A: Well, that depends on the context in which you use it. Ripe source of confusion I reckon.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it a language design flaw? Each element of the array holds the value of a reference. If you assign a new value then you change the reference, but that doesn't alter the objects that are being referenced.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gordy Bone wrote:Is it fair too see this as a language design flaw? Q: To what does m[1] refer, the container of the contents? A: Well, that depends on the context in which you use it. Ripe source of confusion I reckon.

That's not a design flaw at all!

m is an array of reference variables of type MyClass, so each array element (like m[0]) refers to a MyClass instance (or is null). And that doesn't depend on the context at all. And if you execute m[1].name = "new name";, you are changing the name of the MyClass instance m[1] is refering to. And if you execute m[0] = m[1]; (or m[0] = new MyClass();), you are changing the instance to which reference variable m[0] is refering to. That's all straightforward, not context dependent and crystal clear!
 
Gordon Brown
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right... it operates like any reference variable. Not sure how I managed to confuse myself on that one. Long day.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic