Skip to content

Assignment Operator Java Definition Of A Method

Programs declare variables to store/remember information; they manipulate (examine and update) this information when they run. Simple variables typically store some value input by the user, or some value calculated by the program from user-inputs. When a program runs, the values in some of its variables change: thus, the value stored in a variable can vary as the program runs.

The EBNF rules for variable declaration appear below. Each declaration is a statement -a complete command to the computer- which the computer executes. We will cover many of Java's other statements in the next lecture.

    primitive-type  <= int | double | boolean | char
    reference-type <= String | (we will learn others soon)
    type                 <= primitive-type | reference-type

    expression <= literal | (we will generalize this rule later)

    variable-declarator  <= identifier [=expression]
    variable-declarators <= variable-declarator{,variable-declarator}

    local-variable-declaration-statement <= typevariable-declarators ;

Declarations are simple statements, which means that they end with a semicolon (see the last rule above). Variables are always declared with a type (e.g., a primitive type like int or a reference type like String), one or more names (an identifier that the programmmer chooses) whic can be optionally initialized to store a specified value.

The simplest form of a declaration is int sum; which declares a variable named sum to be of the type int, meaning that sum stores only int values. Again, notice the semicolon ending this statement. When a variable is declared this way, the value that it initially stores is undefined. We will have more to say about what Java does with undefined variables later; it is not a mistake to declare certain variables without initializing them.

If we want to declare a variable and at the same time initialize it, we can write something like int gamesPlayed = 0; explicitly telling Java to store zero in this variable initially.

In fact, we can declare a few variables in the same declaration: e.g., double angle, magnitude; declares two variables, both of type double and both storing unknown values. In multi-variable declarations, all the variables are declared to be of the same type -the one type that starts the declaration.

If we want to declare and initialize multiple variables in a single declaration (using the repetition in the variable-declarators EBNF rule), we must explicity specify the initial value of each variable. For example, int n = 0, sum = 0; initializes each variable to zero. WARNING: int n,sum = 0; initializes sum to zero, but leaves n uninitialized; making this mistake is common for beginning programmers. In fact, Java always executes declarations with multiple variables as a sequence of declarations of single variables. So executing int n,sum = 0; is equivalent to executing int n; then int sum = 0;, which makes this problem obvious.

Java imposes a syntax constraint on initialized variables: the type of the variable must be compatible with the type of the expression. We will discuss compatibility more, when we discuss implicit conversions; for now, assume that the two type must be the same.

So in the declaration int n = true; although the syntax is correct, the Java compiler will detect and report a syntax constraint error because true (a boolean literal) is not an int value; likewise boolean atCapacity = 0; exhibits the same error.

To be truthful, Java will in fact automatically convert an int value into a double if necessary , so double x = 1; is legal, and is treated as equivalently to double x = 1.; More obscurely, Java will automatically convert a char value into an int value (and vice-versa) if necessary. We will learn more about implicit type conversion later in this lecture.

Programmers often use line-oriented comments (here called side-bar comments) in declarations to document some interesting facet of a variable that is not captured by even a well-chosen name. For example, in the declarations statements

double tankSize; //Gallons double mileage; //Miles/Gallon Here the programmer has used the comments to describe the units of the quantity the variable stores. Extending the variable name to tankSizeInGallons is probably making it a bit too long. Note that for this style of declaration/comment, we declare just one variable per declaration statement. Pragmatically, most declarations declare just one variable.

The Simple Assignment Operator

One of the most common operators that you'll encounter is the simple assignment operator "". You saw this operator in the Bicycle class; it assigns the value on its right to the operand on its left:

int cadence = 0; int speed = 0; int gear = 1;

This operator can also be used on objects to assign object references, as discussed in Creating Objects.

The Arithmetic Operators

The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "", which divides one operand by another and returns the remainder as its result.

Additive operator (also used for String concatenation)
Subtraction operator
Multiplication operator
Division operator
Remainder operator

The following program, , tests the arithmetic operators.

class ArithmeticDemo { public static void main (String[] args) { int result = 1 + 2; // result is now 3 lbhfb.czechian.netn("1 + 2 = " + result); int original_result = result; result = result - 1; // result is now 2 lbhfb.czechian.netn(original_result + " - 1 = " + result); original_result = result; result = result * 2; // result is now 4 lbhfb.czechian.netn(original_result + " * 2 = " + result); original_result = result; result = result / 2; // result is now 2 lbhfb.czechian.netn(original_result + " / 2 = " + result); original_result = result; result = result + 8; // result is now 10 lbhfb.czechian.netn(original_result + " + 8 = " + result); original_result = result; result = result % 7; // result is now 3 lbhfb.czechian.netn(original_result + " % 7 = " + result); } }

This program prints the following:

1 + 2 = 3 3 - 1 = 2 2 * 2 = 4 4 / 2 = 2 2 + 8 = 10 10 % 7 = 3

You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, and both increment the value of by 1.

The operator can also be used for concatenating (joining) two strings together, as shown in the following program:

class ConcatDemo { public static void main(String[] args){ String firstString = "This is"; String secondString = " a concatenated string."; String thirdString = firstString+secondString; lbhfb.czechian.netn(thirdString); } }

By the end of this program, the variable contains "This is a concatenated string.", which gets printed to standard output.

The Unary Operators

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Unary plus operator; indicates positive value (numbers are positive without this, however)
Unary minus operator; negates an expression
Increment operator; increments a value by 1
Decrement operator; decrements a value by 1
Logical complement operator; inverts the value of a boolean

The following program, , tests the unary operators:

class UnaryDemo { public static void main(String[] args) { int result = +1; // result is now 1 lbhfb.czechian.netn(result); result--; // result is now 0 lbhfb.czechian.netn(result); result++; // result is now 1 lbhfb.czechian.netn(result); result = -result; // result is now -1 lbhfb.czechian.netn(result); boolean success = false; // false lbhfb.czechian.netn(success); // true lbhfb.czechian.netn(!success); } }

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code and will both end in being incremented by one. The only difference is that the prefix version () evaluates to the incremented value, whereas the postfix version () evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

The following program, , illustrates the prefix/postfix unary increment operator:

class PrePostDemo { public static void main(String[] args){ int i = 3; i++; // prints 4 lbhfb.czechian.netn(i); ++i; // prints 5 lbhfb.czechian.netn(i); // prints 6 lbhfb.czechian.netn(++i); // prints 6 lbhfb.czechian.netn(i++); // prints 7 lbhfb.czechian.netn(i); } }

« Previous • Trail • Next »