Professional Documents
Culture Documents
Program Structure in Java: Introduction, Writing Simple Java Programs, Tokens in Java
Programs, Command Line Arguments, Comments.
Data Types, Variables, and Operators: Introduction, Data Types in Java, Static Variables
and Methods, Attribute Final, Operators.
Control Statements: If Expression, Switch Statement, Loops.
Java SE 8 March 2014 Added Date and time API, Repeating annotation, JavaFX.
Result
Relational Operators:
Operator Description
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
Logical Operators:
Operator Description
&& Greater than
|| Greater than or equal to
! Less than
Assignment Operators:
Operator Description
+= Add and assign to
-= Subtract and assign to
*= Multiply and assign to
/= Divide and assign to
%= Modulus and assign to
Increment / Decrement Operators:
Operator Description
++ Increment by one
-- Decrement by one
Bitwise Operators:
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise compliment
>> Shift Right
<< Shift Left
>>> Shift right with Zero fill
Conditional Operators:
Operator Description
?: Used to construct Conditional expression
5. Separators: The separators in Java is also known as punctuators. There are nine separators
in Java, are as follows: separator <= ; | , | . | ( | ) | { | } | [ | ]
• Square Brackets []: It is used to define array elements. A pair of square brackets
represents the single-dimensional array, two pairs of square brackets represent the two-
dimensional array.
• Parentheses (): It is used to call the functions and parsing the parameters.
• Curly Braces {}: The curly braces denote the starting and ending of a code block.
• Comma (,): It is used to separate two values, statements, and parameters.
• Assignment Operator (=): It is used to assign a variable and constant.
• Semicolon (;): It is the symbol that can be found at end of the statements. It separates
the two statements.
• Period (.): It separates the package name form the sub-packages and class. It also
separates a variable or method from a reference variable.
7. Comments: Comments allow us to specify information about the program inside our
Java
8. code. Java compiler recognizes these comments as tokens but excludes it form further
processing. The Java compiler treats comments as whitespaces. Java provides the
following two types of comments:
• Line Oriented: It begins with a pair of forwarding slashes (//).
• Block-Oriented: It begins with /* and continues until it founds */.
1.5 Comments
The Java comments are the statements in a program that are not executed by the
compiler and interpreter.
Why do we use comments in a code?
• Comments are used to make the program more readable by adding the details of the
code.It makes easy to maintain the code and to find the errors easily.
• The comments can be used to provide information or explanation about the variable,
method, class, or any statement.
Types of Java Comments
There are three types of comments in Java.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment
1) Java Single Line Comment
The single-line comment is used to comment only one line of the code. It is the widely used
and easiest way of commenting the statements. Single line comments starts with two forward
slashes (//).
Syntax:
//This is single line comment
2) Java Multi Line Comment
The multi-line comment is used to comment multiple lines of code. It can be used to
explain a complex code snippet or to comment multiple lines of code at a time. Multi-line
comments are placed between /* and */. Any text between /* and */ is not executed by Java.
Syntax:
/*
This is
multi line
comment
*/
3) Java Documentation Comment
Documentation comments are usually used to write large programs for a project or
software application as it helps to create documentation API. These APIs are needed for
reference, i.e., which classes, methods, arguments, etc., are used in the code. The
documentation comments are placed between /** and */.
Syntax:
/**
*
*We can use various tags to depict the parameter
*or heading or author name
*We can also use HTML tags
*
*/
Static Block
If you need to do computation in order to initialize your static variables, you can declare
a static block that gets executed exactly once, when the class is first loaded. It is executed
before the main method at the time of classloading. Consider the following java program
demonstrating use of static blocks.
Example of static block
//static Block
class Static
{
static
{
System.out.println("Static Block");
int a=20;
int b=30;
System.out.println("Sum="+(a+b));
}
public static void main(String args[])
{
}
}
Output
Static Block
Sum=50
1.9 Operators
An operator performs an action on one or more operands. An operator that performs an action
on one operand is called a unary operator (+, –, ++, – –). An operator that performs an action
on two operands is called a binary operator (+, –, / , * , and more). An operator that performs
an action on three operands is called a ternary operator (? :). Java provides all the operators.
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Operator Type Category Precedence
postfix expr++ expr--
Unary
prefix ++expr --expr +expr -expr ~ !
multiplicative */%
Arithmetic
additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
1. Unary Operator
The Java unary operators require only one operand. Unary operators are used to perform
various operations i.e.:
• incrementing/decrementing a value by one
• negating an expression
• inverting the value of a Boolean
Example: ++ and –
class OperatorExample
{
public static void main(String args[])
{
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}
}
Output
10
12
12
10
2. Arithmetic Operator
Java arithmetic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.
Example
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
System.out.println(a+b); //15
System.out.println(a-b); //5
System.out.println(a*b); //50
System.out.println(a/b); //2
System.out.println(a%b); //0
}
}
Output
15
5
50
2
0
3. Shift Operator
Right Shift Operator
The Java right shift operator >> is used to move left operands value to right by the
number of bits specified by the right operand.
class OperatorExample
{
public static void main(String args[])
{
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}
}
Output
2
5
2
Left Shift Operator
The Java left shift operator << is used to shift all of the bits in a value to the left side of
a specified number of times.
class OperatorExample
{
public static void main(String args[])
{
System.out.println(10<<2); //10*2^2=10*4=40
System.out.println(10<<3); //10*2^3=10*8=80
System.out.println(20<<2); //20*2^2=20*4=80
System.out.println(15<<4); //15*2^4=15*16=240
}
}
Output
40
80
80
240
4. Relational Operator
Relational operators in Java return either true or false as a boolean type. Relation
operators are ==, !=, <,>,<=,>=.
class Main
{
public static void main(String[] args)
{
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
5. Bitwise Operator
Bitwise operators in Java are used to perform operations on individual bits. For example,
Bitwise complement Operation of 35
35 = 00100011 (In Binary)
~ 00100011
________
11011100 = 220 (In decimal)
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
6. Logical Operator
These operators are used to perform logical “AND(&&)”, “OR(||)” and “NOT(!)”
operation, i.e. the function similar to AND gate and OR gate in digital electronics. They are
used to combine two or more conditions/constraints or to complement the evaluation of the
original condition under particular consideration.
Example
public class Test
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
}
}
Output
a && b = false
a || b = true
!(a && b) = true
7. Assignment Operator
Java assignment operator is one of the most common operator. It is used to assign the
value on its right to the operand on its left.
Example
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=20;
a+=4; //a=a+4 (a=10+4)
b-=4; //b=b-4 (b=20-4)
System.out.println(a);
System.out.println(b);
}
}
Output
14
16
8. Ternary Operator
Java Ternary operator is used as one liner replacement for if-then-else statement and used a lot
in Java programming. it is the only conditional operator which takes three operands.
Example
class OperatorExample
{
public static void main(String args[])
{
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Output
2
III. Control Statements
1.10 Control Statements
Java compiler executes the java code from top to bottom. The statements are executed
according to the order in which they appear. However, Java provides statements that can be
used to control the flow of java code. Such statements are called control flow statements.