Java Notes
Java Notes
Variables:
Literals
Operators
Assignment Operator:
Arithmetic Operator:
Relational Operator:
Logical Operator:
2. Logical OR (||):
Returns true if at least one condition is true.
If both conditions are false, the result is false.
2. Unary Operator:
3. Ternary Operator:
Syntax:
variable = (condition) ? expression1 : expression2
Example:
package First_Package;
public class New_class {
public static void main(String[] args) {
Flow of Control
1. Conditional Statements:
a) If…else:
if (condition1)
{
// Lines of code
}
else if(condition2)
{
// Lines of code
}
………
else
{
// Lines of code
}
Example:
package First_Package;
public class First_Program {
if(number > 0)
{
System.out.println(number + " is positive");
}
else if (number < 0)
{
System.out.println(number + " is negative");
}
else
{
System.out.println("The Number is Zero");
}
}
}
if (condition)
{
if (condition)
{
//Lines of code
}
}
b) Switch-case:
Java has a shorthand for multiple if statement—the
switch-case statement.
Here is how we can write the above program using a
switch-case:
Syntax:
switch (x)
{
case 0:
// Lines of code
doSomething0();
break;
case 1:
// Lines of code
doSomething1();
break;
...
case n:
// Lines of code
doSomethingN();
break;
default:
doSomethingElse();
}
Example:
package First_Package;
public class First_Program {
}
}
}
2. Loops:
The purpose of loop statements is to execute Java
statements many times.
There are three types of loops in Java—
a) For,
b) While, and
c) Do-while.
a) For Loop:
Syntax:
b) While Loop:
Syntax:
while (condition)
{
//code to be executed
Increment / decrement
}
c) Do-while Loop:
Syntax:
do{
//code to be executed
}while (condition);
for-each Loop:
The for-each loop in Java also called the enhanced
for loop which was introduced in Java 5.
It provides an alternative approach to traverse the
array or collection in Java.
The advantage of the for-each loop is that it makes
the code more readable.
The drawback of the enhanced for loop is that it
cannot traverse the elements in reverse order.
Here, you do not have the option to skip any element
because it does not work on an index basis.
Syntax:
Example:
package First_Package;
public class New_class {
}
}
3. Branching Mechanism:
a) break Statement:
The break statement is used to jump out of a loop.
In other words, break is used to exit from the loop or
switch case.
When break statement is encountered inside a loop,
the loop is immediately terminated and the Program
control moves to the next statement after the loop.
Syntax:
break;
Example:
package First_Package;
public class First_Program {
}
}
b) Continue Statement:
Continue statement is used to skip the current
iteration and continues with the next iteration in the
loop.
We can use Java continue statement in all types of
loops such as for loop, while loop and do-while loop.
Syntax:
continue;
Example:
package First_Package;
public class First_Program {
Operator Precedence:
When various operators are used within the same statement a problem
arises that which operation should be performed first. To solve this
problem there is the concept of operator precedence.
Operator Precedence is a set of rules that defines the priority for all the
operators.
Operators with higher precedence are evaluated first before those with
lower precedence.
Example:
a= 4+5/10;
Output:
a=4.5
Associativity:
If we have operators which have same precedence in the same
statement, then we use associativity to figure out which operation
should be performed first.
associativity determines the order of evaluation. Operators can be left-
associative or right-associative.
Example:
a= 5*5/10
Output:
a=2.5
int value i is promoted to double and then the two double values (i& d)
are added to produce a double result.
double -> float -> long -> int -> char -> short -> byte
Ex., consider the reverse of box example. A bigger box has to be placed
in a small box. Then the bigger box has to be chopped so that the bigger
box can be placed in the small box. Casting is not implicit in nature.
Loss of data may occur.
Example:
inti = (int)(10.0/2.0);