3 Structure
3 Structure
Documentation Section
Package Statement
Import Statement
Interface Statement
Class Defnition
Main method class
{
Main method defntion
}
Documentation Section:
Package Statement: this statement declares package name and informs the
compiler that the classes defined here belongs to this package.
Ex . package student;
Import Statement : this is used to import a class or all the classes in the package.
Class Definitions: java program may contain multiple class definitions. These
classes are made up of instance variables and methods .The number of classes
used depend on the complexity of the program.
Main method class: java program execution always start from main method. The
main method creates objects of various classes and establishes communication
between them
…………………………………………….
Java Tokens
Smallest individual units in a program are known as tokens. A java program is a collection of
tokens, comments and white spaces.Java language includes five types of tokens. They are
Keywords
Identifiers
Literals
Operators
Separators
Smallest units of java language are the characters used to write java tokens.
These characters are defined by the Unicode
Keywords:
Java keywords are also known as reserved words. These are predefined words by Java
so it cannot be used as a variable or object name.
Identifiers:
Identifiers are the names of classes, methods, variables, objects, labels, packages and
interfaces in a program. Ex int marks=78;
short
int
long
double
boolean
char
Literals
A constant value which can be assign to the variable is called Literals.
Ex : int x=10;
Integer Literals:
Integral data types ( byte, short, int, long), we can specify literals in 4 ways.
1. Decimal literals (Base 10) : In this form the allowed digits are 0-9.
int x=101;
2. Octal literals (Base 8) : In this form the allowed digits are 0-7.
Octal numbers should be prefix with 0;
int x= 0146;
3. Hexa-decimal literals (Base 16) : In this form the allowed digits are 0-9 and
characters are a-f. We can use both uppercase and lowercase characters.
It should be prefix with 0X or ox
int x=OX123Face;
By default, every literal is of int type, we can specify explicitly as long type by suffixed with l
or L
Floating-Point literal:
For Floating-point data types, we can specify literals in only decimal form .
double d=123.454;
By default every floating point literal is of double type and hence we can’t assign directly to
float variable. But we can specify floating point literal as float type by suffixed with f or F.
We can specify explicitly floating point literal as double type by suffixed with d or D. Of
course this convention is not required.
Char literal
For char data types we can specify literals in 4 ways:
1. Single quote : We can specify literal to char data type as single character within
single quote.
Char ch=’a’;
String literal
Any sequence of characters within double quotes is treated as String literals.
String s=”hello”;
Operators in java
Operators are used to perform operations on variables and values. Java provides a rich set of
operators to manipulate variables. We can divide all the Java operators into the following
groups −
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are
used in algebra. The following table lists the arithmetic operators −
Assume integer variable A=10 and variable B = 20, then
exampleLive Demo
public class Test {
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("b / a = " + (b / a) );
System.out.println("b % a = " + (b % a) );
System.out.println("c % a = " + (c % a) );
System.out.println("a++ = " + (a++) );
System.out.println("b-- = " + (a--) );
exampleLive Demo
public class Test {
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}
Bitwise Operators
Java defines several bitwise operators, which can be applied to the integer types, long, int,
short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and
b = 13; now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
(A & B) will
Binary AND Operator copies a bit to the result if it
& (bitwise and) give 12 which
exists in both operands.
is 0000 1100
(A | B) will
Binary OR Operator copies a bit if it exists in either
| (bitwise or) give 61 which
operand.
is 0011 1101
(A ^ B) will
Binary XOR Operator copies the bit if it is set in one
^ (bitwise XOR) give 49 which
operand but not both.
is 0011 0001
~ (bitwise Binary Ones Complement Operator is unary and has (~A ) will give
compliment) the effect of 'flipping' bits. -61 which is
1100 0011 in
2's
complement
form due to a
signed binary
number.
A << 2 will
Binary Left Shift Operator. The left operands value is
give 240
<< (left shift) moved left by the number of bits specified by the right
which is 1111
operand.
0000
Binary Right Shift Operator. The left operands value is A >> 2 will
>> (right shift) moved right by the number of bits specified by the give 15 which
right operand. is 1111
Logical Operators
The following table lists the logical operators −
Assume Boolean variables A holds true and variable B holds false, then −
Assignment Operators
Following are the assignment operators supported by Java language −
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator consists of
three operands and is used to evaluate Boolean expressions.
variable x = (expression) ? value if true : value if false
example:
public class Test {
Output:
Value of b=30
Value of b=20
Instanceof Operator:
This operator is used only for object reference variables. The operator checks whether the
object is of a particular type (class type or interface type). instanceof operator is written
as
( Object reference variable ) instanceof(class/interface type)
Live Demo
public class Test {
Output:
True