public static void main(String[] args) { // statements }
Escape Sequence |
Meaning |
---|---|
\n | newline |
\t | tab |
\b | backspace |
\r | carriage return |
\" | double quote |
\' | single quote |
\\ | backslash |
In calls to print and println, multiple items can be printed in a single call through the use of the + operator, which represents string concatenation when at least one of its operands is a string. (If both operands are numeric items, the + operator is addition).
Example of string concatenation in
output
Example of string concatenation
vs. addition in output
/* This is a comment */
int x; // This is a comment x = 3; // This is a comment
datatype variableName
Always starts with a type!
int x; float y; char ch; bool answer; int x = 5; // can initialize variable on the declaration line char item = 'A'; int a, b, c; // can also list several variables in one declaration // statement, separated by commas float a=3.4, b=5.1, c;
A variable can be declared constant by using the keyword final - must initialize in the same statement
final double PI = 3.14159;
The basic operators are: + , - , * , / , %
These do arithmetic as expected (addition, subtraction, etc).
Division is a special case. For integers, the / operator gives
the quotient of a division, and the % operator, called the
modulus operator, gives the remainder.
int remainder = 19 % 5; // gives the result 4 int quotient = 19 / 5; // gives the result 3This example illustrates the arithmetic operations in Java
Order of Operations
Unary Operations are first, ie -1+3 evaluates the -1 first
Then *, /, and % have equal precedence
After any of the above have been evaluated, addition and subtraction
is done.
Associativity on arithmetic operators at the same level are as usual
-- left to right.
variable = expressionNote that the right side can be an expression (i.e. a computation involving values, variables, operators, etc), which can be evaluated down to a single value. The left side must be a storage location (i.e. a variable).
// Valid assignment examples x = 5; y = 2.34; option = 'X'; x = a*(3/y)+z; 3 = x; // ILEEGAL. (backwards) a + 2 = b; // ILLEGAL. Not a variable on the leftThe assignment operator's associativity is right to left:
a = b = c = d = 5;In the above statement, the operation d = 5 executes first, and the value of the assignment (5) is returned, to be used as the operand for the next assignment operator.
v += e; means v = v + e; v -= e; means v = v - e; v *= e; means v = v * e; v /= e; means v = v / e; v %= e; means v = v % e;
++x and x++ used to increment x (equivalent to
x = x + 1)
--x and x-- used to decrement x (equivalent to
x = x - 1)
Note: This only matters if the variable is actually used in another expression. These two statements by themselves have the exact same effect:
x++; // increment x ++x; // increment xExamples of pre-increment and post-increment
int a = 5, b = 3; int c = b * ++a; // After this statement, c is 18, a is 6 int x = 6, y = 4; int d = x - y++; // After this statement, d is 2, y is 5
int i1 = 5, i2; short s = 3; double d1 = 23.5, d2; float f = 12.3; byte b = 10; d2 = i1; // automatically allowed i1 = b; // automatically allowed s = (short)i1; // requires cast operation (some data may be lost) i2 = (int)d1; // requires cast operation (decimal data may be lost) d2 = f + d1; // automatically allowed i2 = b + s; // automatically allowed