Java Programming (1)
Java Programming (1)
Bird
Attributes
Object Information
Message
JAVA TOKENS
The smallest individual units in a program are known as tokens. Java language
includes five types of tokens. They are:
• Reserved Keywords
• Identifiers
• Literals
• Operators
• Separators.
Keywords
• Keywords are an essential part of a language definition. Java language has reserved
50 words as keywords. Some of them are
Abstract Boolean break
Byte case implements
Import extends package
Return super throw
Identifiers:
Identifiers are programmer-designed tokens. They are used for naming classes,
methods, variables, objects, labels, packages and interface in a program. The rules:
• They can have alphabets, digits and the underscore, dollar sign.
• They must not begin with a digit.
• Uppercase an lowercase letters are distinct.
• They can be of any length.
Literals
Literals in java are a sequence of characters (digits, letters and other
characters) they represent constant values to be stored in variables. The five
major types of literals are
• Integer literals
• Floating point literals
• Character literals
• String literals
• Boolean literals
Operators:
An operator is a symbol that takes one or more arguments and operates on
them to produce a result.
Separators:
Separators are symbols used to indicate where groups of code are divided
and arranged. They basically define the shape and function of our code. Some of
the are
• Semicolon (;)
• Comma (,)
• Period (.)
• Braces ({})
JAVA STATEMENTS
A statement is an executable combination of tokens ending with a semicolon (;)
mark. Statements are usually executed in sequence in the order in which they appear.
STAETEMENT DESCRIPTION
Labelled Statement Any statement may begin with a label. Such labels must not be keywords etc
Selection statement These select one of several control flows. If-else and switch
Iteration Statement These specify how looping will take place.While,do and for
Jump Statement Jump statement pass control to the beginning or end of the current block.Break,
continue, return and throw.
Synchronization Statement These are used for handling issues with multithreading
CONSTANTS
Constants in java refer to fixed values that do not change during the
execution of a program. Java supports several types.
I. Integer constants:
An integer constant refers to a sequence of digits. There are three types of integer
Decimal Integer
It consists of a set of digits, 0 through 9.
Example: valid
123, -123, 0, 45673
Invalid
15 20.000 $1000
An octal integer constant consists of any combination of digits from the set 0
through 7, with a leading 0.
Example: valid
037 0 0435 0552
Invalid
45 1023
A sequence of digits preceded by 0x or 0X as Hexadecimal integer. They may
also include alphabets A through F or a through f.
Example: valid
0x2 0x9f0xbcd
Invalid: 09 f56
II ) Real Constants:
These quantities are represented by numbers containing fractional
partslike17.548.Such numbers are called real (or floating point) constants.
Example:
0.456 78.98 654.12
TYPE SIZE
Byte One Byte
Short Two Byte
Int Four Byte
Long Eight byte
• Floating Point Types
We use floating point type to hold numbers containing fractional parts such
as 27.59 and -89.34.
The float type values are single-precision numbers while the double types
represent double-precision numbers.
Character Type:
In order to store character constants in memory, java provides a character data type
called char. The char type assumes a size of 2 bytes but, basically, it can hold only a
single character.
Boolean Type:
The Boolean type is used when we want to test a particular condition during the
execution of the program. There are only two values that a Boolean type can take: true
or false.
TYPE CASTING:
Where there is a need to store a value of one type into a variable of another type. In such
situation, we must cast the value to be stored by preceding it with the type name in
parentheses. The syntax is:
Type variable1= (type) variable2;
The process of converting one data type to another is called casting. Examples
Int m = 50;
Byte n = (byte) m;
Longcount = (long) m;
OPERATORS:
Java operators can be classified into a number of
related categories as below.
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment Operators
• Increment and Decrement operators
• Conditional Operators
• Bitwise operators
• Special operators
• Arithmetic Operators:
Arithmetic operators are used to construct mathematical expression
as in algebra. Java provides all the basic arithmetic operators. They are
listed below
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division(Remainder)
i) Integer Arithmetic:
When both the operands in a single arithmetic expression such as a+b
are integer, the expression is called an integer expression, and the operation
is called integer arithmetic.
a-b=10
a+b=18
Operator Meaning
== Is equal to
!= Is not equal to
Example:
A<b x>20
Logical Operators:
Java has three logical operators, which are given in table
Operator Meaning
|| Logical OR
! Logical NOT
Example:
a>b &&x==10
An expression of this kind which combines two or more relational
expressions is termed as a logical expression or a compound relational
expression.
Assignment Operators:
This operator is used to assign the value of an expression to a variable. We
have seen the usual assignment operator,”=”.
V op=exp;
Where v is a variable, exp is an expression and op ia a java binary operator.
Example:
x=y+1; a=x+(y+2);
++ and --
The operator ++ adds 1 to the operand while – subtracts 1.Both are unary
operators and are used in the following form
++m m++
--I i—
Conditional Operator:
The character pair? : is ternary operator available in java. This operator is used
to construct conditional expression of the form
Exp1? Exp2:exp3
Where exp1, exp2 and exp3 are expression.
Example:
X= (a>b)? a: b;
Bitwise Operators:
These operators are used for testing the bits or shifting them to the right or
left. Bitwise operators may not be applied to float or double.
Operator Meaning
! Bitwise OR
^ Bitwise Exclusive OR
EXPRESSION
An arithmetic expression is a combination of variables,
constants and operators arranged as per the syntax of the
language.
• Simple If stamen
• If… else statement
• Nested if…else statement
• Else if ladder
• Simple I f statement:
Example:
If (category==sports)
{
marks=marks+bonus_marks;
}
System.out.println (marks);
………………………………………..
• The IF….ELSE statement
The general form is
If (test expression)
{
True-Statement-block;
}
Else
{
False-statement-block;
}
Statement-x;
If the test expression is true, then the true-statement-block immediately following the
if statements are executed; otherwise the false-statement-block are executed.
Example:
If(code==1)
{
Boy=boy+1;
}
Else
{
Girl=girl+1;
}
……………………………….
• NESTING OF IF…ELSE STATEMENT:
When a series of decisions are involved, we may have to use more than
one if…..else statement in nested form as follows.
Syntax:
If(test condition)
{
If(test condition)
{
Statement-1;
}
Else
{
Statement-2;
}
}
Else
{
Statement3;
}
Statement-x;
• If condition1 is false, the statment3 will be executed; otherwise it continues to
perform the second test.
• If the condition2 is true, the statement1 will be executed; otherwise the
statement2 will be evaluated and then the control is transferred to the
statement-x.
Example:
If(sex=female)
{
If(balance>10000)
{
Bonus=0.05*balance;
}
Else
{
Bonus=0.03*balance;
}
}
Else
{
Bonus=0.02*balance
}
Balance=balance+bonus;
• The Else If Ladder:
It takes the following general form as
If (condition 1)
Statement-1;
Else if (condition 2)
Statement-2;
Else if (condition 3)
Statement-3;
…………………………………
…………………………………
Else if (condition n)
Statement-n;
Else
Default-statement;
Statement-x;
Switch (expression)
{
Case value-1:
Block-1;
Break;
Case value-1:
Block-1;
Break;
…………………
…………………
Default:
Default-Block;
Break;
}
Statement-x;
• The expression is an integer expression or characters.
• Value-1, value-2…..is constants or constant expression and is known as case
labels.
• Each of these values should be unique within a switch statement, block-1,
block-2… may contain zero or more statements.
• When the switch is executed the value of the expression is successively
compared against the values value-1, value-2…..
• If a case is found then the block of statement that follows the case are
executed.
• The break statement signals the end of a particular case.
• The default is an optional case. It will be executed if the value of the
expression does not match with any of the case values.
Example:
Switch(index)
{
Case 10:
Case 9:
Case 8:
Grade=”Honours”;
Break;
Case 7:
Case 6:
Grade=”First Class”;
Break;
Case 5:
Grade=”Second Class”;
Break;
Case 4:
Grade=”Third Class”;
Break;
Default:
Grade=”Fail”;
Break;
}
System.out.println("Your grade is " + Grade);
LOOPING STATEMENT
Example:
I=1;
Sum=0;
Do
{
Sum=sum+I;
I=i+2;
} while (sum<40||s<10);
………………………………
……………………………..
• The for statement:
The for loop is another entry-controlled loop that provides a more concise
loop control structure. The general form is
For (initialization; test condition; incre/decr)
{
Body of the loop;
}
• Initialization of the control variables is done first, using assignment statements
such as i=1 and count=0.
• The test condition is a relational expression, such as i<10 that determines when
the loop will exit.
• If the condition is true, the body of the loop is executed; otherwise the loop is
terminated.
• The control variable is incremented using an assignment statement such as
i=i+1;
• This process continues till the value of the control variable fails to satisfy the
test condition.
Example:
for(x=0; x<=9; x++)
{
System.out.println(x);
}