• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many references

 
Brian K Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian K Smith wrote:... I can see at least two possible answers to it...

What are your answers? And the reasoning behind them?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I see 2 objects here.
 
Brian K Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, but there is also a String[] called "args."
 
Brian K Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic