Java Language

How can you pass a parameter by reference?


(Submitted by Nelson Yu)
All objects in java are passed by reference. The only things not passed by reference are ints, chars, etc. the primitive data types.

(Submitted by Cliff Berg)

It is true that all objects are passed by reference. What this means in Java, however, is that the handle to the object is passed, but cannot be changed: i.e. the handle is passed by value. Thus, if you reassign the value of a parameter inside a method, the original reference is unaffected once the function returns.

Also, many objects cannot change their values. Strings, for example, if modified inside a method, retain their original value on return. One can, however, reassign the elements of an array parameter. Thus, to pass an int by reference:

void myMethod(int[] p)
{
	... assign new value to p[0];
}

...

{
	int i = 0;
	...
	// now I want to call myMethod() and have it compute a new value for i:
	int[] ai = {i};
	myMethod(ai);	// i[0] is now updated
	i = ai[0];
}

How do I make an array of classes in Java?


(Provided by Cliff Berg)
We have a question on this because it is widely misunderstood, since Java behaves differently than C++ with regard to constructing arrays. While C++ automatically allocates all elements of an array and calls the constructor for each element, Java does not. Java requires you to call new() for each element, since each element is merely a reference - there is no object attached to that reference until you call new for that element. For example, in the following,

class C
{
	int i = 3;
}

class D
{
	C c[] = new C[2];
}

c[0] = new C();
an element is allocated for c[0]. However, no element is allocated for c[1], and so a statement such as
System.out.println(c[1].i);

would produce an exception (Java.lang.NullPointerException).


How do I accomplish the equivalent of the C++ sizeof operator?


(Submitted by John Cleland)
To the best of my knowledge you cannot. C has types whose size is system dependent (e.g. int) so you need a compile time measure of the actual size. In Java all primitive types have a fixed size so the sizeof operator is unnecessary. In general the only time you would need sizeof is to cast an object, struct or array to a byte pointer and then do a write or memcpy. Java doesn't let you do dangerous things like this so sizeof is unnecessary. Java's I/O functions handle writing the primitives to disk in a platform independent fashion, which is all you usually need - you serialize the state (i.e. data members) of an object, not sizeof(object) bytes of binary data.

How do I compare the value of two String's (or other objects)?

"The expression (string1 == string2) does not seem to work!"

(Submitted by Cliff Berg)
When you compare (string1 == string2) Java compares the references, not the values of the objects pointed to by the references. Thus, it is comparing whether string1 and string2 reference the SAME OBJECT, not whether they contain the same string value. (Note that a call to substring() returns a new object.) To compare object VALUES, use equals().

How do I create my own packages?


(Submitted by Jeff Johnson)
We've developed a Tutorial and quick reference on using Java Packages, available at:

http://v2ma09.gsfc.nasa.gov/JavaPackages.html
http://v2ma09.gsfc.nasa.gov/javapkg_brief.html

How do I ask a Java object to list its member variables and/or methods?