Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Question related to array.

 
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question II: (Question from Enthuware)


What will it print when compiled and run?

Enthuware says that, it prints 1.

And here is the explanation:

General Comments: In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.
So, in the expression a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3]; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated. This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a. So, it's actually a[0] = 1.
Note that, if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.


Imagine how would it be if the array object that was initially referred by the reference a is GCed after a=b?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, we can access the previous array which we referred by a earlier if we fetched the array before we attempt to re-manipulate it (like in the above example).
 
author
Posts: 23853
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hrishikesh Yeshwant Alshi wrote:Imagine how would it be if the array object that was initially referred by the reference a is GCed after a=b?



In order for an object to be GC'ed, it must be unreachable. This not only includes all references, but from temporary references (used to evaluate expressions) as well.

IOWs, the object originally referenced by "a" isn't eligible for GC, until the expression is done with it.

Henry
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! I missed that....
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much, Henry. But I am still not clear.

I still fell that once a=b is done 'a' will start referring to { 2, 3, 1, 0 } and the code will loose all the references to { 1, 2, 3, 4 }. Isn't it?

Did you mean that, a [ (a = b)[3] ] ---- the 'a' typed in bold is referring to { 1, 2, 3, 4 } and

a [ (a = b)[3] ] ---- the 'a' typed in the bold is referring to { 2, 3, 1, 0 }.


If so, could you please elaborate a bit? How can same reference 'a' refer to two different array objects?


I am sorry if that is a dumb question.

Many thanks in advance.
 
Henry Wong
author
Posts: 23853
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hrishikesh Yeshwant Alshi wrote:
If so, could you please elaborate a bit? How can same reference 'a' refer to two different array objects?



Basically, it can't. One reference can't refer to two array objects at the same time. Yet, in order for java to process the expression, it seems to need for it to do so. So, how is it done? Simply, with temporary variables. While the JVM is processing the expression, it has temporary variables -- which hold interim values while the processing is happening.

And these temporary variables are uses, just like any other variables, for the GC to determine reachability. In other words, the GC can't collect the object originally referenced by "a" because there is a temporary variable that reaches it. This temporary variable will, of course, go out of scope once the expression is complete.

Henry
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry for that nice explanation. BTW, Can't we access those temporary variables?
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

Great explanation.


 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!