Click Here for Free Traffic!
Click Here for your Free Traffic!
Google
 

Thursday, April 5, 2007

Operators

Java Operators

Java operators produce new values from one or more operands (just so we’re all clear, the operands are things on the right or left side of the operator). The result of most operations is either a boolean or numeric value. And because you know by now that Java is not C++, you won’t be surprised that Java operators can’t be overloaded. There is, however, one operator that comes overloaded out of the box: If applied to a String, the + operator concatenates the right-hand operand to the operand on the left. Stay awake. The operators and assignments portion of the exam is typically the one where exam takers see their lowest scores. We aren’t naming names or anything, but even some of the exam creators (including one whose last name is a mountain range in California) have been known to get a few of these wrong.

Assignment Operators

Assigning a value to a variable seems straightforward enough; you simply assign the stuff on the right side of the = to the variable on the left. Well, sure, but don’t expect to be tested on something like this:

x = 6;

No, you won’t be tested on the no-brainer (technical term) assignments. You will, however, be tested on the trickier assignments involving complex expressions and casting. We’ll look at both primitive and reference variable assignments. But before we begin, let’s back up and peek inside of a variable.

What is a variable? How are thevariable and its value related?

Variables are just bit holders, with a designated type. You can have an int holder, a double holder, a Button holder, and even a String[] holder. Within that holder is a bunch of bits representing the value. For primitives, the bits represent a numeric value (although we don’t know what that bit pattern looks like for boolean, but we don’t care). A byte with a value of 6, for example, means that the bit pattern in the variable (the byte holder) is 00000110, representing the 8 bits. So the value of a primitive variable is clear, but what’s inside an object holder? If you say

Button b = new Button();

what’s inside the Button holder b? Is it the Button object?

No! A variable referring to an object is just that—a reference variable. A reference variable bit holder contains bits representing a way to get to the object. We don’t know what the format is; the way in which object references are stored is virtual-machine specific (it’s a pointer to something, we just don’t know what that something really is). All we can say for sure is that the variable’s value is not the object, but rather a value representing a specific object on the heap. Or null. If the reference variable has not been assigned a value, or has been explicitly assigned a value of null, the variable holds bits representing—you guessed it—null. You can read

Button b = null;

as “The Button variable b is not referring to any object.” So now that we know a variable is just a little box o’ bits, we can get on with the work of changing those bits. We’ll look first at assigning values to primitives, and finish with assignments to reference variables.

Primitive Assignments

The equal (=) sign is used for assigning a value to a variable, and it’s cleverly named the assignment operator. There are actually 12 assignment operators, but the other 11 are all combinations of the equal sign and other arithmetic operators,

= *= /= %=
+= -= <<= >>=
>>>= &= ^= |=

Reference Variable Assignments

You can assign a newly created object to an object reference variable as follows:

Button b = new Button();

The preceding line does three key things:
■ Makes a reference variable named b, of type Button
■ Creates a new Button object on the heap
■ Assigns the newly created Button object to the reference variable b

You can also assign null to an object reference variable, which simply means the variable is not referring to any object:

Button c = null;

The preceding line creates space for the Button reference variable (the bit holder for a reference value), but doesn’t create an actual Button object.

public class Foo {
public void doFooStuff() {
}
}

public class Bar extends Foo {
public void doBarStuff() {
}
}
class Test {
public static void main (String [] args) {
Foo reallyABar = new Bar(); // Legal because Bar is a subclass of Foo
Bar reallyAFoo = new Foo(); // Illegal! Foo is not a subclass of Bar
}
}

Assigning One Reference Variable to Another

With primitive variables, an assignment of one variable to another means the contents (bit pattern) of one variable are copied into another. Object reference variables work exactly the same way. The contents of a reference variable are a bit pattern, so if you assign reference variable a to reference variable b, the bit pattern in a is copied and the new copy is placed into b. If we assign an existing instance of an object to a new reference variable, then two reference variables will hold the same bit pattern—a bit pattern referring to a specific object on the heap. Look at the following code:

import java.awt.Dimension;
class ReferenceTest {
public static void main (String [] args) {
Dimension a = new Dimension(5,10);
System.out.println("a.height = " + a.height);
Dimension b = a;
b.height = 30;
System.out.println("a.height = " + a.height +"after change to b");
}
}


In the preceding example, a Dimension object a is declared and initialized with a width of 5 and a height of 10. Next, Dimension b is declared, and assigned the value of a. At this point, both variables (a and b) hold identical values, because the contents of a were copied into b. There is still only one Dimension object—the one that both a and b refer to. Finally, the height property is changed using the b Java Operators reference.