1

In the question below I not getting the bold line. Does that line mean create an array objects of class compartment? Because as per my knowledge Java doesn't have concept of pointers.

Create an abstract class compartment to represent a rail coach. Provide an abstract function notice in the class. Derive FirstClass, General, Ladies and Luggage classes from the Compartment class. Override notice function in each of them. Create a class TestCompartment. Write main function to do the following: Declare an array of compartment pointers of size 10.

2
  • 1
    "Reference" and "pointer" are the same thing. (It is very poorly phrased. Whoever wrote it should get a "D".) Commented May 11, 2013 at 11:21
  • (JLS 4.3.1 Line 2) The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object. Commented May 11, 2013 at 11:28

4 Answers 4

2

On one hand, Java does not have a concept of pointers; on the other hand, everything other than primitives in Java (i.e. all Object-derived things) could be through of as "pointers", although technically they are not called that.

Java calls them references, but since there is such thing as null reference, they behave very much like pointers in C and C++.

Anyway, when you create an array of ten non-primitives, you create an array of references, each one set to null:

Compartment[] compartments = new Compartment[10];

This is different from creating ten Compartment objects, in that the objects themselves are not created when you create an array, only places through which you could reference these objects later if you need to.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, in Java there is a null reference :-)
0

It most likely means: create a size 10 array of references.

3 Comments

What does references exactly mean? Should we create objects?
@Lizzie - If it says create the array, presumably you just create the array -- the values will be null. But as I said above, it's a very poorly phrased statement.
A references is the type of the var in Compartement con; con is a reference to a Compartement object but until you create the object it points to null
0

Java has no pointers as such.Everything that's not a primitive is a reference.

4 Comments

And what's the difference between a "reference" and a "pointer"?
@HotLicks: One thing exists, the other doesn't. What's the difference between something that exists and something that doesn't?
@HotLicks Adding to Kerrek,If you defenetly need it, AFAIK the difference is we can not arithmetic operations on references.we can on pointers
@Baadshah - You can do arithmetic on pointers in some languages.
0

Although the underlying implementation of references in Java comes down to C-like pointers (because it does, at some point), I think references would have been a better way of putting through the request.

You only have to declare a classic array.

2 Comments

References are not always implemented with pointers in java. The current 64 bit jvm from Oracle does have a mode, where references are stored as 32 bit values, which are then shifted 2 times before usage. Thus only using 32 bit for each reference, but still allowing access to 16GB ram.
@MTilsted - And there have been cases of pointers implemented with similar manipulations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.