jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes How many references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many references" Watch "How many references" New topic
Author

How many references

Brian K Smith
Greenhorn

Joined: Nov 01, 2010
Posts: 12
Hello, I am taking a course in Java. Here is a simple question from our midterm, and the professor, the class, and I can see at least two possible answers to it. While it was a multiple choice question, I'm going to leave it open ended. What do others think?


In the following code:

public class Main {

public static void main(String[] args) {
int numberofTrees = 5;
Integer ageOfFarm = 14;
float averageHeightOfTree = 124.2f;
Tree treeType;
int [] heightOfEachTree;
}

}

How many objects are being referenced?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Brian K Smith wrote:... I can see at least two possible answers to it...

What are your answers? And the reasoning behind them?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

By the way, I see 2 objects here.
Brian K Smith
Greenhorn

Joined: Nov 01, 2010
Posts: 12
The two answers were 1 or 3.

Integer ageOfFarm = 14; //1 for sure
Tree treeType; // Tree is supposed to be an object, the prof. probably meant to type something like TreeSet treeType. I'll check
int [] heightOfEachTree; // 3rd

Only the first gets initialized. In fact, if you try to use the others as is, the compiler gives you an error. No new() no allocation of memory, not even a reference you can check, I'm thinking just one, the correct answer was three, and yes, that Tree is probably a typo.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You are correct that only the first of these 3 is initialized: ageOfFarm references an Integer object.

The other two variables (treeType and heightOfEachTree) contain null references, so they do not reference an "object."

But, there is yet 1 more to make the total 2.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Brian K Smith wrote:... How many objects are being referenced?

I'm reading the question literally as, "How many objects are being referenced?" And I see 2 objects being referenced in this code.

If we interpret this to mean, "How many 'object references' are there?" then we could make the argument that a null reference is an "object reference" even though it does not point to an object. (Note that JLS - 4.3.1 covers "a special null reference, which refers to no object" under its discussion of object reference types.) In that case, we would have a total of 4 object references (2 of which point to objects, and 2 of which are null).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

marc weber wrote:
Brian K Smith wrote:... How many objects are being referenced?

I'm reading the question literally as, "How many objects are being referenced?" And I see 2 objects being referenced in this code.

I've looked at the code several times and I only see one object being created and referenced (the Integer object). No Tree objects are created and no int[] objects are created, and the other two variables are of primitive types and couldn't refer to objects anyway.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Ah, but there is also a String[] called "args."
Brian K Smith
Greenhorn

Joined: Nov 01, 2010
Posts: 12
marc weber wrote:Ah, but there is also a String[] called "args."


So far I think Marc has gotten it right. And everyone in class missed the args array object.
 
GeeCON Prague 2014
 
subject: How many references