• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Scope and array questions

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic