Basic Java Syntax
Comments
- C style: /* */
- C++ style: //
- Documentation style: /** */
Character Set
Other languages, like C or C++, use the ASCII character set. The ASCII
character set represents characters by a byte. Actually 7 bits of the
byte are used. So at most 128 characters can be represented. This is
sufficient to represent the letters of the English alphabet as well
as the decimal digits from 0 to 9, and all the punctuation marks. However,
if you have to represent letters of the alphabet of other languages,
a byte does not offer sufficient storage. Java from the beginning has
adopted the 16-bit Unicode system. The ASCII representation is a subset
of the Unicode system. To represent a character from another
language one has to use the acutal Unicode. The convention used is
\uxxxx where xxxx are 4 hexadecimal numbers.
Identifiers
- An identifier must start with a letter, an underscore (_), or a
dollar sign ($).
- An identifier cannot be a reserved word.
- An identifier can be any sequence of letters, digits, underscore,
and dollar signs subject to the above constraints.
Variables
A variable marks a place in memory to store data. Every variable has
a type which tells the compiler how to interpret the string of bits
in that memory location. A variable has to be declared before using.
Primitive Types
In Java there are 8 primitive types. The sizes of these types are fixed
for the Java Virtual Machine.
- boolean: true or false
- byte: 8 bit
- char: 16 bit
- short: 16 bit
- int: 32 bit
- long: 64 bit
- float: 32 bit
- double: 64 bit
Octal integer literal begins with 0 and hexadecimal integer
literal begins with 0x or 0X.
Declaring a Variable
The generic way of declaring a variable is
dataType variableName;
int i;
char ch;
boolean found;
Assignment
variable = expression;
int i = 0;
short x = 0xBF;
float area = length * width;
Constants
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int RATE = 6;
Constants have declared and initialized. You cannot change the
value of a constant.
Numeric Operators
- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /
- Remainder: %
A literal is a numeric value that is assigned to a variable like
2 or 5.8. Floating point numbers can be represented in the
scientific notation 5.43e+2 which is 543. The numeric operators
+, -, *, / can be applied to all integers and floating
point numbers. Integer division truncates the result. There are
two symbols used to handle overflow:
- NaN (not a number) e.g. 0 / 0
- ∞ (infinity) e.g. 1 / 0
Shortcut Operators
x += 4 | x = x + 4 |
x -= 4 | x = x - 4 |
x *= 4 | x = x * 4 |
x /= 4 | x = x / 4 |
x %= 4 | x = x % 4 |
Increment and Decrement Operators
Increment and decrement operators can be applied to all
integers and floating point types. They can be used either prefix
(--x, ++x) or postfix (x--, x++) mode.
Prefix Increment Operation
int x = 2;
int y = ++x; // x == 3, y == 3
Postfix Increment Operation
int x = 2;
int y = x++; // x == 3, y == 2
Type Conversions
Conversion from a lower range type to higher range type is
called widening and is done automatically. However,
to go from a higher range type to lower range type (narrowing)
you have to explicitly use type casting.
float x = 5.67;
int i = (int) x;
Character Data Type
Java uses 16-bit Unicode to represent characters. The Unicode
representation is of the form '\uxxxx', where xxxx
is hexadecimal notation and ranges from 0000 to FFFF.
char ch = 'A';
char alpha = '\u03b1';
Boolean Data Type
Boolean data types can have two values either true
or false.
Comparison Operators
less than | < |
less than or equal to | <= |
greater than | > |
greater than or equal to | >= |
equal to | == |
not equal to | != |
Boolean or Logical Operators
NOT | ! |
AND | && |
OR | || |
EXCLUSIVE OR | ^ |
Truth Table for NOT
Truth Table for AND
A | B | A && B |
F | F | F |
F | T | F |
T | F | F |
T | T | T |
Truth Table for OR
A | B | A || B |
F | F | F |
F | T | T |
T | F | T |
T | T | T |
Truth Table for EXCLUSIVE OR
A | B | A ^ B |
F | F | F |
F | T | T |
T | F | T |
T | T | F |
Conditional Boolean Operator
If you have an expression of the form A && B then Java
evaluates A first. If A is true, then Java
evaluates B. If A is false, then Java does not
evaluate B.
If you have an expression of the form A || B then Java
evaluates A first. If A is true, then Java does
not evaluate B. If A is false, then Java does
evaluate B.
Bitwise Operators
The bitwise operators include the AND operator &, the OR
operator |, the EXCLUSIVE OR operator ^. The bitwise
operators applies to boolean and integer types. Both operands are always
evaluated.
Shift Operators
Shift operators are applied only to integer types.
- x << k : shift the bits in x, k places to the left
- x >> k : shift the bits in x, k places to the right filling in
with the highest bit on the left hand side
- x >>> k : shift the bits in x, k places to the right and fill in
with 0
Conditional Operator
The conditional operator ? : uses the boolean value of one
expression to decide which of the two other expressions should be
evaluated.
exp1 ? exp2 : exp3
If exp1 is true, then exp2 is chosen. If exp1 is false then exp3 is
chosen.
Operator Precedence
In Appendix C of your book you have a chart showing the list of
operators in Java. When two operators have the same precedence,
their associativity determines the order of evaluation. All binary
operators are left associative. The assignment operators are right
associative.
Operand Evaluation Order
The left hand operand of a binary operator is evaluated before
any part of the right hand operand is evaluated. The order for
evaluating operands takes precedence over the operator precedence
rule.
Expression Statements
An assignment or increment / decrement expression can be made into
a statement by adding a semi-colon. An expression statement is
executed by evaluating the expression.
if Statement
The if statement allows conditional execution of a statement
(with a single if ) or a conditional choice of two statements
(with if-else ), executing one or the other but not both.
if ( Expression )
Statement;
If the Expression evaluates to true, then Statement
is executed. If the Expression evaluates to false then
nothing happens.
if ( Expression )
Statement1;
else
Statement2;
If the Expression evaluates to true, then Statement1
is executed. If the Expression evaluates to false then
Statement2 is executed.
Switch Statement
The switch statement transfers control to one of several
statements depending on the value of an Expression. The type of the
Expression must be char, byte, short, or int.
switch (k)
{
case 0: System.out.println ("zero"); break;
case 1: System.out.println ("one"); break;
default: System.out.println ("out of range"); break;
}
The body of a switch statement is known as a switch block. Any statement
immediately contained by the switch block may be labeled with one or more
case or default labels. When the switch
statement is executed, first the Expression is evaluated. If one of the
case constants is equal to the value of the expression, then all
the statements after the matching case label in the switch
block are executed. If no case matches but there is a
default label, then all the statements after the matching default
label are executed. If there is a break statement then the
switch statement completes execution.
while Statement
The while statement executes an Expression and a
Statement repeatedly until the value of the Expression
is false. The Expression must have type boolean. If the
value of the Expression is false the first time it is
evaluated, then the Statement is not executed.
while ( Expression )
Statement;
do Statement
The do statement executes an Expression and a
Statement repeatedly until the value of the Expression
is false. The Expression must have type boolean. If the
value of the Expression is false the first time it is
evaluated, then the Statement is executed at least once.
do
Statement
while ( Expression );
for Statement
The for statement executes some initialization code, then
executes an Expression, a Statement, and some update code repeatedly
until the value of the Expression is flase.
for ( Init; Expression; Update )
Statement;
The Init code is a list of statement expressions, the expressions are
evaluated in sequence from left to right. If in the Init code, there is
a local variable declaration then the scope of the local variable is in
the for block. If the value of the Expression is false the
first time it is evaluated, then the Statement is not executed.