Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays and reference

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, another confusing question on Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 11356-11359). Enthuware. Kindle Edition.
Given the following program, which statements are true?

Select 2 options
A. Compile time error at line 3.
B. The program will throw a java.lang.ClassCastException at the line labelled 2 when run.
C. The program will throw a java.lang.ClassCastException at the line labelled 3 when run.
D. The program will compile and run if the (B[ ] ) cast in the line 2 and the whole line 3 is removed.
E. The cast at line 2 is needed.


I got answer E but apparently C is correct as well and I had no idea (other than the exercise telling me that 2 answers were correct)
I spent some time looking at this code and writing the process down on a piece of paper but I still don't fully get it. The explanation says that
at run time, this fails because a1 is not an array of B but is an array of A.
. But how is that possible? With a=b array a becomes effectively b but, I thought, since we said that a1 = a, a1 and a are pointing to the same object and therefore when you say a=b you're effectively saying also a1=b, I would have thought.



 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way there is a straight 1, it's supposed to be //1, sorry I can't edit my own code so I had to repost
 
Henry Wong
author
Marshal
Pie
Posts: 22108
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:But how is that possible? With a=b array a becomes effectively b but, I thought, since we said that a1 = a, a1 and a are pointing to the same object and therefore when you say a=b you're effectively saying also a1=b, I would have thought.


You are correct in that after the "a1 = a" statement, you have two different references pointing to the same object. However, you have to remember that it is still two different references.

So, after the "a=b" statement, the a reference is now pointing to the same object as the b reference. And the a1 reference remains unchanged.

Henry
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jason Attin wrote:But how is that possible? With a=b array a becomes effectively b but, I thought, since we said that a1 = a, a1 and a are pointing to the same object and therefore when you say a=b you're effectively saying also a1=b, I would have thought.
Always remember,java is pass by value not pass by reference.
when you see things like-"a1=a",always do a conversion of "a"(which is a reference) to the value it is pointing at that time.because of the fact(java is pass by value),latter on changing the reference "a"  did not change a1.
let me explain what is happening in your code-
At line 5-"a1=a" causes a1 to point the object a is pointing at that time(a1 has nothing to do with "a" but with the object it is refering),this statement is same as => a=new A[10],a1=new A[10].
At line 7 "a=b" causes "a" to point the object b is pointing at that time(a has nothing to do with b,it just checks what b is referencing during the assignment and then it is assigned the object it found b is referencing at that time),so it is same as=>b=new B[20],a=new B[20]
so finally you have-a=new [B] , b=new[B] , a1 = new A[10].
just see this link it will take your 1 minute and all your doubts will get cleared-Ranch Campfire-Story of javaPassByValue.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[EDIT:for the last 2nd line of my previous post]-so finally you have-a=new B[20] , b=new B[20] , a1 = new A[10]

sorry for the mistake.

Kind Regards!
Praveen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic