Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scope and array questions

 
Enrique Arguelles
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I am new here, and I hope to enjoy the company of others and learn a lot and help when I can.

Now on with the questions, this are part of the certification exam which hard to admit I failed, so I am trying to see where are my mistakes and correct them.

First scope

public static void main(String ... args){
ClassA a = new ClassA();
a = null;

class ClassA{

ClassA(){
ClassB b = new ClassB();}

ClassB(){
ClassC c = new ClassC();}

ClassC(){}

When I set a = null; how many objects are eligable for the GC?

Next question

A [] testa = new A[4];
Integer [] inta = new Integer[4];
testa = (A) inta;

While I know a invalid cast its detected at runtime, when you cast a array to a array its detected during compiler time, can someone enligh me on the reasons for this. And sorry if I post on the wrong forum.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enrique Arguelles wrote:
While I know a invalid cast its detected at runtime, when you cast a array to a array its detected during compiler time, can someone enligh me on the reasons for this.


Hi,

Welcome to JavaRanch!

The compiler will reject any cast which it can figure out is impossible. It knows that "inta" is an array of Integer; it's impossible that an array of Integer could ever be an instance of A.

But, for example,

Integer[] inta = new Integer[4];
Object[] obja = inta; // OK, since an Integer is-a Object
A[] aa = (A[]) obja; // ALSO ok, since an Object[] could be an A[].

The compiler only considers the types of the references it's dealing with; it never goes back to look at what value has been given to a reference previously.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:The compiler will reject any cast which it can figure out is impossible.

Note that when either side is an interface, nearly all casts are possible. This is because with inheritence, the compiler can't tell if there is an extending class that does implement the interface. Only if the class is final and does not implement the interface will the cast fail:

 
Enrique Arguelles
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thanks for both of you about the replaies.

Still I am kinda lost about the scope question

First scope

public static void main(String ... args){
ClassA a = new ClassA();
a = null;

class ClassA{

ClassA(){
ClassB b = new ClassB();}

ClassB(){
ClassC c = new ClassC();}

ClassC(){}

When I set a = null; how many objects are eligable for the GC?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this isn't complete, compilable code, so it's up to the reader to decide how to interpret it. If the variables "b" and "c" are local to the constructors of classes A and B -- which is what they look like to me -- then the SCJP answer is "1, just the 'A' object". The B and C objects can be collected as soon as the A and B constructors complete, repectively, since these variables go out of scope at those times, and the B and C objects are otherwise unreferenced.

Make sense?
 
Enrique Arguelles
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that make sense I complety forgot to take in account that (>< still got it right by dump luck).

Thanks a lot for the help :P hope I wont be a bother around here as i am sure as the more I study, I would ask more questions to prevent hair loss.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic