Java Expressions
A Java statement can be a comment, a block, a declaration, or an expression. Most statements in Java end in a semicolon-blocks and comments are about the only holdouts.
Java considers spaces, tabs, and newline characters to be "white space" and ignores them except when they appear in the middle of an operator or an identifier.
Comments
There are three types of comments defined in Java. The most common type of comment is one line long and begins with a double slash (//). This type of comment continues from the // until the end of the line.
The second comment style is the block comment, which begins with a slash and an asterisk (/*) and ends with an asterisk and a slash (*/). Newline characters are ignored in this type of comment.
The third type of comment, the Javadoc-type comment, begins with a slash and a double asterisk (/**) and ends with an asterisk and a slash (*/). This type of comment is the same as the block comment, except Visual J Plus Plus assumes that the statement immediately following the comment is the name of a method (function):
/** * The following function is an example. * @param input is an integer that is ignored */ void example(int input) { . . . }
In this example, as soon as the program finishes the definition of the example() method, Visual J Plus Plus extracts the documentation contained within the Javadoc comment and adds it to the statement completion feature. This feature helps the programmer complete expressions.
Declaring Variables
You must declare a local variable before using it. A variable declaration consists of the type, followed by the name of the variable, optionally followed by the assignment operator (=) and an expression representing the initial value of the variable.
int nAnInt; // declare a variable int nASecondInt = 10; // declare another variable and // give it an initial value
Intrinsic Data Types
Table A-1 shows the variable types that are intrinsic to Java. To enhance portability, Java sets the specific size of each type. In addition, a variable is initialized when you declare it. If you don't initialize a variable explicitly, Java gives it the corresponding value as shown in the table. All of the numeric types shown are signed. Java doesn't support unsigned data types.
Table A-1. Size of each Visual J Plus Plus intrinsic type.
Data Type | Size [bits] | Default value |
---|---|---|
boolean | 8 | false |
byte | 8 | 0 |
char | 16 | `x0' |
short | 16 | 0 |
int | 32 | 0 |
long | 64 | 0 |
float | 32 | 0.0F |
double | 64 | 0.0D |
The boolean type isn't a numeric type-it can have only the value true or false. All other types are numeric and can be mixed within a single computation. The char type is a full 16 bits, and is assumed to be Unicode. (Unicode is a superset of ASCII. The first 128 characters are the same as ASCII. After that, Unicode associates other values with special characters like accented characters, umlauts, and ideographic characters.)
Identifiers
A variable name can be any valid Java identifier. Identifiers must start with a letter of the alphabet, an underscore (_), or a dollar sign ($). Subsequent letters can also be the numeric digits 0 through 9.
Constants
There are four types of constants: boolean, numeric, character, and string.
Boolean constants
There are two boolean literals, true and false.
Numeric constants
Java assumes that any identifier beginning with a digit is a numeric constant. Constants that begin with 0 are octal, while those that begin with 0x or 0X are hexadecimal. All other constants are decimal.
The digits A through F in a hexadecimal number can be in either uppercase or lowercase. Thus, the following numbers all have the same value:
255, 0377, 0xff, 0xFF
Java assumes that a numeric literal containing either an exponent or a decimal point is a floating-point number. By default, a floating-point number is assumed to be of type double. A floating-point number that ends with the character F is of type float. Thus, in the following line the first three constants are double while the fourth constant is a float:
3.14159, 0.1, 1.602E-19, 3.14159F
Character constants
Character constants are enclosed in single quotes; for example ' ' is a space. In addition, a character literal can be defined using `\xNN', where NN is the Unicode value of the character. Thus, `\x20' is also a space. Java defines special symbols for certain common nonprintable characters. For example `\n' is the newline character. The literal `\\' represents a single backslash character.
String constants
String constants (also known as string literals) are any number of characters contained within double quotes. For example, "Hello, world" is a string constant.
NOTE
A Java string constant isn't implemented as a null-terminated array of characters (often called an ASCIIZ string), as it is in C and C++.
Smooth Operators
Table A-2 shows the Java operators. The numbers in the left column indicate the precedence, with 1 being the highest precedence and 15 being the lowest precedence.
Table A-2. Operator precedence in Java.
Precedence | Operators |
---|---|
1 | . [] () |
2 | ++ -- ! ~ instanceof |
3 | * / % |
4 | + - |
5 | << >> >>> |
6 | < > <= >= |
7 | == != |
8 | & |
9 | ^ |
10 | | |
11 | && |
12 | || |
13 | ?: |
14 | = op= |
15 | , |
Precedence refers to the order in which operations are carried out. For example, multiplication (*) is performed before addition (+) when they appear in the same expression. Operators with the same precedence are executed from left to right.
NOTE
The Java operators work the same as their C equivalents, except for the addition of two new operators (>>> and ^) and the way the logical operators work when applied to booleans. Even the precedence is the same.
The most important of all operators is the assignment operator (=). (The assignment operator is not to be confused with the equality operator ==.) The assignment operator takes the value on its right, and stores it in the object on its left.
In order to make an assignment successfully, the return type of the expression on the right must be the same as the type of the object on the left. If they're not already of the same type Java will try to cast the expression for you, but only if it can perform that cast without losing information. The value and type of the assignment operator expression is the resulting value and type of the left object.
Unary operators
Operations on integers fall into two categories: unary and binary. A unary operator involves a single argument. Java doesn't like to deal with arguments smaller than an int. Therefore, if the argument to a unary operator is a byte, char, or short type, the result is an int. Otherwise, the result is the same type as the argument. (That is, int begets int, long begets long.)
The unary operators are - (negation), ~ (bitwise complement), ++ (increment), and -- (decrement).
The increment operator increments its argument by one, and the decrement operator decrements its argument by one. The increment and decrement operators both come in two flavors: pre- and post-. The preincrement operator (++x) increments the argument and then evaluates it. The postincrement operator (x++) evaluates the argument and then increments it.
Binary operators
The binary operators involve two arguments, one on either side of the operator. Binary operators include + (addition), - (subtraction), != (inequality), and so on. If both arguments are int, then the operation is performed in int precision. If either argument is long, then the other argument is converted to long and the result is long. If the resulting value of an expression is greater than what can be contained in the current precision, then the upper bits are lopped off. No overflow indication is generated.
Operations on floats
The same operators that work on integers can also be applied to floating point values.
Java follows the rules of the Institute of Electrical and Electronic Engineers (IEEE) Standard 754 for floating point calculations.
Operations on booleans
There are two boolean operators: && AND, plus || OR. Java performs short-circuit evaluation of boolean operators; for example, in the expression a && b, b isn't evaluated if a is false. Likewise, in the expression a || b, b isn't evaluated if a is true.
NOTE
The boolean operators work the same in Java as in C/C++.
Operations on strings
The only operator (other than the assignment operator) that Java defines for strings is addition. If either argument to the plus operator is of class String, Java converts the other argument into a String and concatenates the two strings.
Special operators
Java supports several sets of unusual operators. The first is a set of operators that combine a binary operator and the assignment operator into one. If op were a conventional binary operator, then the following op= would exist:
a op= b; // is equivalent to a = a op b
This works for all binary operators.
Casting about
Java converts expressions implicitly from smaller types to larger types (so-called promoting) unless the cast might lose significant digits.
You can explicitly cast a value to convert it from its current type to another type. The cast appears as the desired type enclosed in parentheses before the value to be converted, as in the following:
// convert the float into an integer int nIntValue = (int)fFloatValue;