|
Operators & Operator hierarchy:
Operators are special symbols that perform specific operations on one or more operands, and then return a result.
The operators in the following table are listed according to precedence order. Operators with higher precedence
are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence.
When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first.
All binary operators except for the assignment operators are evaluated from left to right.
The assignment operators are evaluated right to left.
Operators | Precedence |
postfix | expr++, expr-- |
Unary | ~, !
|
multiplicative | *, /, %
|
additive | +, -
|
Shift | <<, >>
|
Relational | <, >, <=, >=
|
Equality | ==, !=
|
Bitwise AND | &
|
Logical Operators | &&, ||
|
Assignment Operators | =, +=, -=, *=, %=, /=
|
In general-purpose programming, certain operators appear more frequently than others.
Eg: the assignment operator "=" is far more common than the unsigned right shift operator ">>".
|
Expressions
An expression is a construct made up of variables, operators, and method invocations, which are constructed
according to the syntax of the language, that evaluates to a single value.
int i = 0;
int result = 1 + 2; // result is now 3
if (value1 == value2)
System.out.println("Equal");
The data type of the value returned by an expression depends on the elements used in the expression.
The expression i = 0 returns an int because the assignment operator returns a value of the same data type
as its left-hand operand; in this case, i is an int. As you can see from the other expressions, an expression
can return other types of values as well, such as boolean or String.
The Java programming language allows you to construct compound expressions from various smaller expressions
as long as the data type required by one part of the expression matches the data type of the other.
Here's an example of a compound expression:
1 * 2 * 3
The following expression gives different results, depending on whether you perform the addition or
the division operation first:
x + y / 100 // ambiguous
You can specify exactly how an expression will be evaluated using parenthesis ( and ).
For example, to make the previous expression unambiguous, you could write the following:
(x + y) / 100 // unambiguous, recommended
If you don't explicitly indicate the order for the operations to be performed, the order is determined by
the precedence assigned to the operators in use within the expression. Operators that have a higher precedence
get evaluated first. For example, the division operator has a higher precedence than does the addition operator.
Therefore, the following two statements are equivalent:
x + y / 100
x + (y / 100) // unambiguous, recommended
|
Type conversion & Casting:
If the two types are compatible, then Java will perform the conversion automatically.
For example, assign an int value to a long variable.
For incompatible types we must use a cast.
Casting is an explicit conversion between incompatible types.
Java's Automatic Conversions:
An automatic type conversion will be used if the following two conditions are met:
The two types are compatible.
The destination type is larger than the source type.
Ex:
public class Type_Conversion{
public static void main(String a[]){
byte b=12;
int i=0;
i=b;
System.out.println("b = "+b);
System.out.println("i = "+i);
}
}
Casting Incompatible Types:
A narrowing conversion is to explicitly make the value narrower. A cast is an explicit type conversion.
It has this general form: (target-type)value.
target-type specifies the desired type. The following code
casts double value to int & byte type value.
public class Type_Cast {
public static void main(String args[]) {
byte b;
int i = 1;
double d = 1.123;
System.out.println("Conversion of double to int.");
i = (int) d;
System.out.println("d: " + d + " i: " + i);
System.out.println("Conversion of double to byte.");
b = (byte) d;
System.out.println("d: " + d + " b: " + b);
}
}
|
Enumerated Types:
An enum type is a type whose fields consist of a fixed set of constants. Common examples include compass directions
(values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Because they are constants, the names of an enum type's fields are in uppercase letters.
In the Java programming language, you define an enum type by using the enum keyword.
For example, you would specify a days-of-the-week enum type as:
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
Ex:
enum Months {
JAN(1), FEB(2), MAR(3), APR(4), MAY(5), JUN(6),
JUL(7), AUG(8), SEP(9), OCT(10), NOV(11), DEC(12);
int Code;
Months(int v){
this.Code = v;
}
}
public class Enum_Ex {
static Months myMonth;
public static void main(String args[]){
System.out.println(myMonth.FEB.Code);
}
}
|
Conditional statements: - If Else & Switch Statements
The if-then statement is the most basic of all the control flow statements.
It tells your program to execute a certain section of code only if a particular
test evaluates to true.
The if-then-else statement provides a secondary path of execution when
an "if" clause evaluates to false.
class If_Else_Ex{
public static void main(String a[]){
int Total = 340;
char Grade;
if (Total>=360) Grade = 'A';
else if (Total>=300) Grade = 'B';
else Grade = 'F';
System.out.println("Grade = "+Grade);
}
}
Switch statement:
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths.
A switch works with the byte, short,char, and int primitive data types.
It also works with enumerated types (discussed in Enum Types), the String class and
a few special classes that wrap certain
primitive types: Character, Byte, Short, and Integer
The following example declares an int named option whose value represents a direction.
The code displays the name of the direction, based on the value of option, using the switch statement.
public class Switch_Ex {
public static void main(String[] args) {
int option = 2;
String direction;
switch (option) {
case 1: direction = "NORTH";
break;
case 2: direction = "EAST";
break;
case 3: direction = "WEST";
break;
case 4: direction = "SOUTH";
break;
default: direction = "Invalid...";
break;
}
System.out.println(direction);
}
}
|
Loops - For, While & do While statements:
The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as
the "for loop" because of the way in which it repeatedly loops until a particula>r condition is satisfied.
The general form of the for statement can be expressed as follows:
for (initialization; termination; increment){
statements
}
class For_Ex{
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: "+ i);
}
}
}
in While loop, expressions with in the loop executed as long as particular condition is True.
class While_Ex {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
in do_While loop, expressions in loop executed first, then condition is tested.
class do_while_Ex {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: "
+ count);
count++;
} while (count < 11);
}
}
|
|