Professional Documents
Culture Documents
WORKSH
OBJECTIVES
Learning JAVA.
Understanding Salient features of JAVA.
Implementing JAVA PROGRAMS on your
own.
Development of JAVA PROJECTS on your
own.
INDEX
About
JAVA language.
Program structures
Simple program
JVM
OOP principles.
keywords
Data types
Operations
Arrays
Java History
Computer language innovation and
1970s.
C++ designed by Bjarne Stroustrup in
1979.
In 1990, Sun Microsystems started a project called Green.
Objective: to develop software for consumer electronics.
Project was assigned to James Gosling, a veteran of
Java Buzzwords
Simple
Secure
Portable
Object-oriented
Robust
Multithreaded
Architecture-neutral
Interpreted
High performance
Distributed
Dynamic
L 1.9
Structure of C
Document section
Link section
Definition section
Global declaration section
Main()
{
declaration part
Functional section
executable part
}
Sub program section
function 1
function 2
defined functions
-----------function n
User
Structure of JAVA
Document section
Package section
Import statements
Class definitions
{
Variables
Methods
}
Main Class
{
Main Method Definition
}
/*A program
JAVA WORLD
class Welcome
{
public static void main(String args[])
{
System.out.println(WELCOME TO
JAVA WORLD);
}
}
machine code.
java compilers produces an intermediate
code known as
Byte code for a machine.
this machine is called Java Virtual Machine,
and it exists only inside the computer
memory.
the virtual machine code is not machine
specific, the machine code is generated by
Java interpreter.
Java
source code
program
Java
compiler
Process of compilation
Virtual
Byte
code
machine
virtual machine
real machine
Process of converting byte
Java code into machine
Machinecode
Byte code
interpreter
code
OOP Vs POP
Object oriented programing
It is concerned to develop an
application based on real time .
PRINCIPLES
Encapsulation: It is the mechanism
that binds together code and the data it
manipulates.
Data
info. in &Method
info. out
Flying
birds
Robin
Nonflying
birds
Swallo
w
Pengui
n
Kiwi
Circle object
Draw(circle)
Box object
Draw(box)
Triangle
object
Draw(triangl
e)
CLASS
Structure of Class
A class is declared by use of the class keyword.
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
continue
goto
package
synchroni
zed
assert
default
if
private
this
boolean
do
implements protected
throw
break
double
import
public
throws
byte
else
instanceof
return
transient
case
extends
int
short
try
catch
final
interface
static
void
char
finally
long
strictfip
volatile
class
float
native
super
while
const
For
new
switch
Data Types
Java defines eight simple types:
Variables
Java uses variables to store data.
To allocate memory space for a variable JVM
requires:
1) to specify the data type of the variable
2) to associate an identifier with the variable
3) the variable may be assigned an initial
value
All done as part of variable declaration.
L 2.2
Variable Declaration
datatype identifier [=value];
datatype must be
A simple datatype
User defined datatype (class type)
Variable Scope
Scope determines the visibility of program elements with
}
A variable declared inside the scope is not visible outside:
{
int n;
}
n = 1;// this is illegal
L 2.5
Arrays
An array is a group of liked-typed variables
referred to by a common
name, with individual variables accessed by
their index.
Arrays are:
1)
2)
3)
4)
declared
created
initialized
used
dimensions.
L 2.7
Array Declaration
Array declaration involves:
Array Creation
After declaration, no array actually
exists.
In order to create an array, we use the
new operator:
type array-variable[];
array-variable = new type[size];
This creates a new array to hold size
elements of type type, which reference
will be kept in the variable arrayvariable.
L 2.9
Array Initialization
Arrays can be initialized when they are
declared:
int monthDays[] =
{31,28,31,30,31,30,31,31,30,31,30,31};
Note:
1) there is no need to use the new operator
2) the array is created large enough to hold all
specified elements
L 2.11
Array Indexing
Later we can refer to the elements of this
L 2.10
Multidimensional Arrays
Multidimensional arrays are arrays of arrays:
1) declaration:
int array[][];
2) creation:
int array = new int[2][3];
3) initialization
int array[][] = { {1, 2, 3}, {4, 5, 6} };
Two-dimensional array
L 2.12
Two-dimensional array
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
Operators Types
Java operators are used to build value
expressions.
Java provides a rich set of operators:
1) assignment
2) arithmetic
3) relational
4) logical
5) bitwise
L 2.13
Arithmetic assignments
+=
v += expr;
v = v + expr ;
-=
v -=expr;
v = v - expr ;
*=
v *= expr;
v = v * expr ;
/=
v /= expr;
v = v / expr ;
%=
v %= expr;
v = v % expr ;
L 2.14
op1 + op2
ADD
op1 - op2
SUBSTRACT
op1 * op2
MULTIPLY
op1 / op2
DIVISION
op1 % op2
REMAINDER
L 2.15
Relational operator
==
Equals to
!=
Not equals to
>
Greater than
<
Less than
>=
<=
L 2.16
Logical operators
&
Logical AND
op1 | op2
Logical OR
&&
||
op1 || op2
Short-circuit
AND
Short-circuit OR
! op
Logical NOT
op1 ^ op2
Logical XOR
L 2.17
Bitwise operators
program
class Bits
{
public static void main(String args[])
{
byte x,y;
x=10;
y=11;
System.out.println("~x="+(~x));-> -11
System.out.println("x&y="+(x&y)); -> 10
System.out.println("x/y="+(x/y)); -> 11
System.out.println("x^y="+(x^y)); -> 1
System.out.println("x<<2="+(x<<2)); -> 40
System.out.println("x>>2="+(x>>2)); -> 22
System.out.println("x>>>2="+(x>>2)); -> 22
}
}
presented
by
U. LOVARAJU
Assoc. Professor
IT-Department