Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Non-object-oriented language basics, Part 2

Delve into operators and expressions

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Last month we began surveying the Java language by focusing on some of Java's non-object-oriented language entities. Specifically we concentrated on comments, identifiers, data types, literals, and variables. Although those entities are important to any Java program, they're also somewhat boring. To create more interesting programs, you need to work with operators, expressions, and statements. This month I'll introduce operators and expressions; I'll leave statements for next month's discussion.



Operators

An operator is a combination of characters that identifies a data operation, such as addition. Operators transform one or more operands, data items that serve as arguments to an operator, into a new data item. Operators evaluate the operands according to a predefined evaluation order. For example, the subtraction operator subtracts its rightmost operand from its leftmost operand.

Classifying operators: Unary, binary, and ternary

The number of required operands classify an operator. The resulting classifications are unary, binary, and ternary.

A unary operator is an operator that takes one operand; negation is an example. If an operator takes two operands, it is a binary operator. Examples of binary operators include multiplication and division. Finally, a ternary operator is an operator that takes three operands. Java's only ternary operator is the conditional operator.

The compiler ensures, via widening and narrowing conversion rules, that the data types of a binary or ternary operator's operands match. For example, if one operand's data type to the division operator is integer, and the other operand's data type is short integer, the compiler will generate code that converts the short integer to an integer prior to generating code that performs the division.

Classifying operators: Prefix, postfix, and infix

The position of an operator relative to its operand(s) classifies the operator. These classifications are prefix, postfix, and infix.

A prefix operator is an operator that precedes its operand, whereas a postfix operator trails its operand. An example of a prefix operator is bit-wise complement; an example of a postfix operator is postincrement. All unary operators are either prefix or postfix operators.

An infix operator is situated between its operands. An example of an infix operator is the addition operator, as the plus sign appears between its operands. All binary and ternary operators are infix operators.

Assignment operator

The assignment binary infix operator is the simplest of all operators. It assigns its rightmost operand to its leftmost operand, which must be a variable. The syntax of the operator is:

variable_name '=' operand


Example:

int num = 27; // The assignment operator is used to initialize a variable.
num = 32;     // This operator is also used to assign a value to an existing variable.        


Arithmetic operators

Java provides 11 arithmetic operators: addition, subtraction, multiplication, division, modulus, preincrement, postincrement, predecrement, postdecrement, negation, and unary plus.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources