Sie sind auf Seite 1von 13

visit @ http://www.infotechcomputer.

in

Chapter 3 Java Revision Tour


Rapid Appication Development - It is a method of developing software through the use of pre
programmed tools and wizards. it helps the programmer to develop programs and softwares rapidly.

IDE - (Integrated Development Environment ) - It is a programming environment provided to programmer


which helps of programming to perform various tasks. Netbeans is an Integrated Development
Environment for Java. other one is eclipse
Components of IDE
1. Title Bar
2. Menu Bar
3. Toolbar
4. GUI Builder
5. The Palette
5. Inspector Window
7. Properties Window
8. Code Editor Window etc.

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 1

visit @ http://www.infotechcomputer.in
Event - An event represents the occurence of an activity. for example click of a button, window activated
event etc
Message - It is the information/request sent to the application. for example parameters passed to a
function or a method
Programming Techniques 1. Procedure oriented e.g. c language (we can only create functions or methods)
2. Object oriented e.g. java (we can create classes and objects)
3. Event oriented e.g. visual basic (we can create forms, buttons, menus, etc)
Note : Java is procedure oriented + object oriented + event oriented

Java Chracter Set - It is a set of valid character set that a language can recoginse.
Tokens - The smallest individual unit of a program is known as a Token.
Types of Tokens 1. Keywords - int, float , for, while etc
2. Identifiers - int a; float b, public void setval()
3. Literals - a = 5 then 5 is a literal and a is a variable
4. Punctuators - , ; : etc symbols which are used seperate something
5. Operators - + - * / > < >= etc

Keywords : These are the reserved words of java that can't be used to name any variable or any other
element. few keywords are for, while, break, continue etc
Identifiers : These are the names given to different parts of the programs like variables, objects, classes
functions etc. Identifier naming rules are as follows :
1. it can have alphabets, digits, underscore or dollar symbol
2. it must not be a keyword
3. it must not begin wih a digit
4. it can be of any length
5. java is strictly case sensitive
Literals - these are the data items that have fixed values.
Types of literals
a. integer literal - a=5
b. floating literal - a=5.5f
c. boolean literal - a=true
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 2

visit @ http://www.infotechcomputer.in
d. character literal - a='z'
e. string literal - a="Ram"
f. null literal - a=null

a="z" a="9"

Punctuator - These are also called as seperators few of them are () [ ] ; , etc
Operators - Operators are the elements which performs some action on operands. for eg. c=a+b. in this
statement a,b and c are the operands and +,= are operators.
Types of Operators :
1. Mathematical Operators :
+
*
/
%
2. Increament and Decreament Operators :
++ (prefix and postfix) a++ and ++a
-a=5
sout(a++) : first print the value of x and then increase the value of x
sout(++a) : First increase the value of x and then print it
3. Relational Operators :
> : greater than
< : less than
>= : greate than equals to
<= : less than equals to
!= : Not equals to
== : comparison operator
4. Logical Operators :
&& (Logical And Operator)
||
(Logical OR Operator)
!
(Logical Not Operator)

eg. !(5>7)

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 3

visit @ http://www.infotechcomputer.in
5. Bitwise Operators :
&
(Bitwise AND Operator)
|
(Bitwise OR Operator)
!
(Bitwise NOT Operator)
6. Assigment Operators :
=
+= a=a+5 a+=5
-=
/=
*=
%=
7. Shift Operators
Right Shift >> (Right shift 1 time means divide the value by 2)
Left Shift <<
(Left shift 1 time means multiply the value by 2)

8. Conditional Operator (also called as ternary operator)


c=a>b?a:b

Escape Sequence : these character combinations have special meanings.


\n = next line
\t = tab
\a = alert bell

Data Types : They are the types which are used to identify the data items that can be stored under it and
the operations that can be done on them.
Types of Data Types :
1. Primitive Data Types : (Implicit Data Types) (Inbuilt Data Types)
2. Non Primitive Data Types : (Explicit Data Types) (Reference Data Types) (User Defined Data Types)
Primitive Data Types are the inbuilt data types of java like int, double, float etc.
Non Primitive Data Types of reference data Types are the user defined data types created by the help of
primitive data types for eg. classes and interfaces etc.

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 4

visit @ http://www.infotechcomputer.in
Variables : A Variable is a name of memory location which holds a data value of particular data type.
declaration of variable - int a;
initialization of variable - a=5
Constant - A variable whose value can't be changed
for eg. final pi=3.14;

Operator Precedence - Operator precedince determnes the order in which expressions are evaluated. for
example if the statement is
a+b*c/d then first of all * operator will be solved beacuse precedence of * and / is same but associativity
is left to right, then / is solved and in the last + is resolved.
Expressions - An expression in java is any valid combination of operator,s, constants, and variables i.e. a
legal combinations of java tokens.
Types of java expressions are as follows :
1. Arithmetic expression
2. Compound Expression
3. Relational or logical expression.
1. Arithmetic Expression : These expressions are either pure integer expressions or pure real expressions.
we can also form mixed expressions with combining integer and real values for example
Given
int a,b,c
float p,q,r
1. a/b is a pure integer expression
ii. p/q
is a pure real expression
iii. a*p/q is a mixed expressioin
2. Relational or Logical expression : the expressions that result in true or false values are called boolean
expressions. these are combination of constants, variables and logical and relational operators. for
example
i. x>y
ii. (y+z)>=(x/z) etc
3. Compound Expressions : A compound expression is the one which is made up by combining two or
more simple expressions with the help of operators. for example
(a+b)/(c+d)
or
(a>b) || (b>c)
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 5

visit @ http://www.infotechcomputer.in

TYPE CONVERSION : The process of converting one predefined type into another is called Type
conversion. In java there are two types of conversions :
1. implicit type conversion (coercion)
2. explicit type conversion (type casting)
1. Implicit Type conversion : The implicit type conversion is the type of conversion which automatically
takes places according to the given data types. it is also known as COERCION. for example
int a,b;
float c;
a=15;
b=4;
c=a/b;
sout(c)

eg. 2
int a=5;
jtfa.setText(""+a);
in the above program the result of a/b will automatically get stored in c into integer form.
2. Explicit Type conversion : It is the type of conversion where the programmer or user converts one data
type into another explicitly. it is also called as TYPE CASTING. for example
int a,b;
float c;
a=15;
b=4;
c=(float)a/b;
sout(c)
in the above program the result of a/b will get converted into float form and then it will be stored in c.

JAVA STATEMENTS : A statement forms a complete unit of execution. it is just like a sentence in natural
language. we can say that any line of a program can be termed as a java statement
Block : A block is a group of zero or more statements between balanced braces. it is normally used in a
if, while, do-while, for construct or in a method defination. for example

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 6

visit @ http://www.infotechcomputer.in
if(a>b)
{
...
...
...
...
}
in the above if construct dotted lines are represented as a block. a block always starts with a { symbol
and ends with a } symbol.
Note: if there is only one statement in any (if, while , do-while , for) construct then we can omit the
block characters ( { ..... } ). In this case Only a single line will be termed as a block in that construct.
Null or Empty Statement : We know that all of the java statements are terminated with a semicolon
symbol. if there is only a semicolon in a java statement then it can be termed as a empty or null
statement for example
;
JAVA PROGRAMMING CONSTRUCTS :
In java there are three types of programming constructs just like any other programming language.
these basic programming constructs are
1. sequence
2. selection
3. iteration

1. Sequence : it means any set of statements in a java program that are executed in a sequence
2. Selection : it means the execution of a statement or a set of statements depending upon a condition
test. if the given condition is true then one set of statements execute otherwise the other set executes.
it is also called as decision construct. Normally if , switch and conditional statement comes into this
category of java constructs.
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 7

visit @ http://www.infotechcomputer.in
3. Iteration : it is also called as a loop. here a statment or a set of statements are executed repeatatively
based on a given condition test. while, do-while and for loop comes into this category of constructs.
Selection Statements in Java : In java there are two Selection Statements
1. If Statement
a. Simple If Statement
b. If - else statement
c. Nested If Statement
d. conditional Statement
2. Switch Statement
1. If Statement : if we want to execute a statement or a set of statements according to a given condition
or criteria then we use simple if statement as follows:
if (a>b && a>c)
{
sout ("Largest is "+a);
}
2. If - else statement : if we want to execute a statement or a set of statements among the two sets
according to a given condition or criteria then we use if - else statement as follows :
if (a>b)
{
sout("Largest is "+a);
}
else
{
sout("Largest is "+b);
}
3. Nested If statement : if there are various conditions to be check to execute a set of statments among
the few sets then we use nested if statement as follows :
if(a>b)
{
if(a>c)
{
sout("Largest is "+a);
}
else
{
sout("Largest is "+c);
}
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 8

visit @ http://www.infotechcomputer.in
}
else
{
if(b>c)
{
sout("Largest is "+b);
}
else
{
sout("Largest is "+c);
}
}
4. Conditional Statement : if we want to replace the simple if statement or the nested if statement with a
single statement then we can use conditional statement as follows :
example 1
c=a>b?a:b;
example 2
c=a>b?a>c?a:c:b>c?b:c;
2. Switch Statement : it is a better replacement of if statement only if the condition set of if statement is
related to a single value. if is known as multiple branch selection statement. it is used as follows:
switch(a)
{
case 1: sout("Monday");
break;
case 2:sout("Tuesday");
break;

case 3:sout("Wednesday");
break;
case 4:sout("Thursday");
break;
Note : the fall of control to the cases of matching case in a switch statement is called Fall-through
Iteration: In Java there are three Iterative Constructs. Iterative Constructs are also called as loops
1. While
2. Do - While
3. For
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 9

visit @ http://www.infotechcomputer.in

Each of the loop contains the following basic components


1. Initialization Expression (a=1)
2. Test Expression (a<=10)
3. Update Expression (a++)
4. Body of the Loop {......}
1. While Loop - when we want to execute a statement or a set of statements repeatatively a number of
times according to a given condition or criteria then we use while loop. it comes under the category of
conditional looping. when we do not know about the exact number of repeatative executions of the set
of statements then we use while loop. its syntax and example is as follows :
syntax.
initialization expression
while (test expression)
{
.
body of the loop
.
update expression
}
Example
a=1;
while(a<=10)
{
sout(a);
a++;
}
2. Do - while loop - It is same as while loop but the basic differences between while loop and do - while
loop are as follows :
While
1. While is a entry controlled loop
2. While is also called as Top-tested or pre-tested loop
3. it is not necessary that this loop must execute at least once

Do While
it is an exit controlled loop
it is also called as bottom-tested or
post-tested loop
it is necessary that this loop will execute
at least once

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 10

visit @ http://www.infotechcomputer.in
Syntax and example of Do-while loop is as follows :
initialization expression
do
{
.
body of the loop
.
update expression
}while (test expression);
Example
a=1;
do
{
sout(a);
a++;
}while(a<=10);
3. For loop - when we want to execute a statement or a set of statements repeatatively a number of
times according to a given condition or criteria then we use while loop. it comes under the category of
sequential looping. when we exactly know about the number of repeatative executions of the set of
statements then we use for loop. its syntax and example is as follows :
syntax.
for(initialization expression ; test expression ; update expression)
{
.
body of the loop
.
}

Difference between while and for loop


While loop
For loop
------------------------------------------------------------------------------------------------------------------------------1. all of the parts of loop are in seperate lines
all of the parts of loop are in a single
line
2. while is a conditional loop
for is a sequential loop
3. we use while loop when the number of
we use for loop when the number of
iterations are not known
iterations are normally known.

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 11

visit @ http://www.infotechcomputer.in
JUMP OR BRANCH STATEMENTS IN JAVA
-------------------------------------------------------------------------------------------------------------------------------------The jump statements unconditionally transfer program control within a function. Java has three jump
statements
1. return
2. break
3. continue
1. Return statement : it is mainly used inside a method to return a value. example of return statement is
as follows:
static int sum(int x,int y)
{
int c;
c=x+y;
return(c);
}
public static void main(String args[])
{
int t;
t=sum(5,7);
System.out.println("Sum is "+t);
}
2. Break Statement - It is used to take the control out of the innermost loop. it takes the control out of
the while, do-while, for or switch statement. example of break statement is as follows :
int x,i,flag=0;
x=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Value"));
for(i=2;i<=x-1;i++)
{
if(x%i==0)
{
flag=1;
break;
}
}
if(flag==0)
sout("Number is prime");
else
sout("Number is not prime");
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 12

visit @ http://www.infotechcomputer.in

Note : In java Labelled Break are also allowed. we can transfer the control outside any of the loop using
labelled break statement.

3. Continue Statement : it is an another jump statement which takes the control back to the next
iteration of the loop and skips the rest code in body of the loop. example of continue statement is as
follows :
int x;
for(x=1;x<=10;x++)
{
if(x==2 || x==5 || x==7)
continue;
System.out.println(x);
}

Output 1 3 4 6 8 9 10

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 13

Das könnte Ihnen auch gefallen