aspose file tools*
The moose likes Beginning Java and the fly likes Scope and array questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scope and array questions" Watch "Scope and array questions" New topic
Author

Scope and array questions

Enrique Arguelles
Greenhorn

Joined: Feb 09, 2009
Posts: 10
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

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:



SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Enrique Arguelles
Greenhorn

Joined: Feb 09, 2009
Posts: 10
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Feb 09, 2009
Posts: 10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scope and array questions
 
Similar Threads
"=" oprator question
NullPointer exception
Question on Constructor, instance var & static var
A Timeout scenario
Question regarding encapsulation