Java
Java
Java
Java Programming
Akshay Sarvaiya
What Is Java?
History
How its work
Characteristics of Java
2
History
Java was developed by James Gosling and
team at Sun Microsystems in 1991.
First its name is Oak but its renamed Java in
May 20, 1995
Totally depended on oop concept.
J2SE, J2ME, and J2EE.
3
JDK Editions
Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications
such as Java servlets and Java ServerPages.
Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
4
Versions of Java
Oak (1991): Designed for embedded
devices
Java (1995): Original version (had applets)
Java 1
Java 1.1 (1997): Adds inner classes and a completely
new event-handling model
Java 1.2 (1998): Includes “Swing” but no new syntax
Java 1.3 (2000): Additional methods and packages, Java 2
but no new syntax
Java 1.4 (2002): More additions and the assert statement
Java 1.5 (2004): Generics, enums, new for loop, and
Java 5.0
other new syntax
5
Characteristics of Java
Java is simple
Java is object-oriented
Java is distributed
Java is interpreted
Java is robust
Java is secure
Java is architecture-neutral
Java is portable
Java’s performance
Java is multithreaded
Java is dynamic
6
A class consists of variables
called fields together with
functions called methods that
act on those fields.
or
A class is a collection of
variables , methods
An object is a variable whose
type is a class. An object has
the fields and can call the
methods of its class.
Getting Started with Java
Programming
A Simple Java Application
Compiling Programs
Executing Applications
9
A Simple Application
Example 1.1
//This application program prints Welcome
//to Java!
package chapter1;
10
Creating and Compiling Programs
Create Source code
On command line
– javac file.java
Source code
bytecode
result 11
public means that main() can be called from
anywhere.
static means that main() doesn't belong to a
specific object
void means that main() returns no value
main is the name of a function.
– main() is special because it is the start of the
program.
String[] means an array of String.
12
Executing Applications
On command line
– java classname
Bytecode
13
Comments
14
Reserved Words
Java reserved words are keywords that are
reserved by Java for particular uses and cannot
be used as identifiers
For example
variable names,
function names,
class names).
The list of reserved words in Java is provided
below.
15
Characteristics of java
Simple:-
– Java is easy to learn and developed by taking the
best features from other languages mainly like C and
C++.
– It is very easy to learn Java who have knowledge of
object oriented programming concepts.
Portable :-
– java program is fully portable to make program
portable java compiler generates executable code
which is known as bytecode.
– This bytecode is machine language code which
every machine can read.
Secure:
– Security is the most important need for any programming
language.
– Any outside entity should not access our program as it
can harm or steal sensitive information.
– In case of java there is no risk of it because java provide
FIREWALL between your computer and the application
from which the computer download file.
Multithreaded:-
A thread is a sub process. Using multithreading you can
execute mote then one thread simultaneously
java has a thread class and its method to accomplish this.
Object-oriented:-
– The object oriented language must support the
characteristics of the OOPs. and Java is a fully object
oriented language .
– it supports all the characteristics needed to be object
oriented. In the Java every thing is treated as objects to
which methods are applied.
Robust:-
– Java has the strong memory allocation and automatic
garbage collection mechanism.It carries out type
checking at both compile and runtime making sure that
every data structure has been clearly defined and typed.
– Java manages the memory automatically by using an
automatic garbage collector. all the above features makes
Java language robust.
Platform Independent:-
– Java Provide the facility of cross-platform
programs by compiling in intermediate code
known as bytecode. this bytecode can be
interpreted on any system which have Java
Virtual Machine(JVM).
compiled and interpreted:-
The execution of java program is a two step process.
First the source code is compiled using java compliler
and bytecode generated.
This bytecoe which is machine independent is then
interpreted using java virtual machine.
Data type
20
Data type in java
Data type in java
The data
primitive
typesdata
thattypes
are derived
are thefrom
basic data
types thatdata
primary are types
available
are known
in mostasofnon-
the
programming
Primitive data languages.
types. TheseThedatatypes
primitiveare
data
typesto
used are
store
usedgroup
to represent
of values.
single values.
Integer
Integer types can hold whole numbers such as 123
and −96. The size of the values that can be stored
depends on the integer type that we choose.
Type Size Range of values that can be stored
byte 1 byte −128 to 127
short 2 bytes −32768 to 32767
int 4 bytes −2,147,483,648 to 2,147,483,647
9,223,372,036,854,775,808 to
long 8 bytes
9,223,372,036,854,755,807
1 . Integer
The range of values is calculated as
−(2n−1) to (2n−1)−1
where n is the number of bits required.
For example, the byte data type requires
1 byte = 8 bits.
−(28−1) to (28−1)−1
= −27 to (27) -1
= −128 to 127
2. Floating Point
Floating point data types are used to represent
numbers with a fractional part.
29
A variable is a container that holds values that
are used in a Java program. To be able to use a
variable it needs to be declared.
Java is a strongly typed programming
language.This means that every variable must
have a data type associated with it.
For example, a variable could be declared to
use one of the eight primitive data types :-
byte, short, int, long, float, double, char or
boolean.
Syntex
Datatype variable_name
For example
int a;
31
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value
of PI.
char a = 'a'; // the char variable a iis initialized with
value 'a'
32
Types of Variable
33
Local
A variable
variables
that isare
declared
declaredinside
asinstatic
methods,
theisclass
calledbut
static
constructors,
outside
variable. the method
or blocks.
is called instance variable .
ItIt
Local
iscannot
notvariables
declared
be local.are
as static.
created when the method,
constructor or block is entered
Local variables are visible only within the declared
method, constructor or block.
class A
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
int n=90; //local variable
}
}//end of class 35
Java arrays
36
An array is a type of variable that can store multiple
values.
The array is an object in Java that contains similar
data type values.
A few main points about arrays in Java:
– Array is a data structure in java that can hold one or
more values in a single variable.
– Array in java is a collection of similar type of values.
– Array index starts at 0.
– Java has two types of arrays –
singledimensional and
two-dimensional arrays
multidimensional arrays.
How to declare an array
Syntex
– First way of declare array
– ArrayDataType ArrayName[] = {value}
//auto size array
– second way of declare array
– ArrayDataType arrayName[] = new int[10];
//fix size array
public class bca_array {
public static void main(String []args)
{
int first_way[] = {10,20,30}; //Declaring and initializing
an array of three elements
int sec_way[] = new int[3]; //declaring array of three
items
sec_way[0] =10;
sec_way[1] =20;
sec_way[2] =30;
System.out.println(first_way[0]+"\
n"+sec_way[1]);
}
}
2-dimensional
Two-dimensional arrays are used whenever the
model data is best represented with rows and
columns
Syntex
– First way of declare array
– ArrayDataType ArrayName[][] = {value1,val2}
//auto size array
– second way of declare array
– ArrayDataType arrayName[][]= new int[2][2];
//fix size array
int a[][] = new int[2][4]; // Two rows and four
columns.
a[0][0] a[0][1] a[0][2] a[0][3]
43
In Java Variables are Value Container used to store
some value.
In order to Perform some operation on the Data we
use different operators such as arithmetic Operator,
Logical Operator etc…..
Arithmetic Operators
Relational Operators(Comparision)
Assignment Operators
Logical Operators
Bitwise Operators
Arithmetic Operators
Comparison and Logical operators are used to test for true or false.
Comparison operators are used in logical statements to determine
equality or difference between variables or values.
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y
47
Logical Operators
Operator Description Example
&& and (x < 10 && y > 1) is
true
|| or (x==5 || y==5) is false
48
Bitwise Operators
Operato
Description Example
r
(A & B) will give 12 which is
& Binary And
0000 1100
(A | B) will give 61 which is
| Binary OR
0011 1101
(A ^ B) will give 49 which is
^ Binary XOR
0011 0001
(~A ) will give -61 which is
Binary Ones Complement Operator is unary and 1100 0011 in 2's complement
~
has the effect of 'flipping' bits. form due to a signed binary
number.
49
Ternary operator (? :)
? : is also known as ternary operator.
Syntex
– Var= (Condition) ? (Expression1) : (Expression2);
here if conditon is true then expression1 is
evaluated if false then expression2 evaluatd.
50
class lg
{
public static void main(String ar[])
{
int a=30,b=20;
String s;
s=(a>b)?"a gtr":"b gtr";
System.out.println(s);
}
} 51
Control Statements
52
The control statement are used to controll the flow
of execution of the program .
This execution order depends on the conditional
logic.
It will be handle by
– branching statement
– looping statement
– Jump statement
branching statement
Branching statement can be achieved by two
way…..
1) If-else
2) Switch case
54
If condition
This is a control statement to execute a single
statement or a block of code, when the given
condition is true
if it is false then it skips.
Syntax:
if(conditional){
<statements>;
...;
...;
}
55
class ifdm
{
public static void main(String ar[])
{
int n = 11;
if(n%2 == 0)
{
System.out.println("This is even
number");
}
} 56
If-else condition
This is a control statement to execute statement
or a block , when the given condition is true
if it is false then it execute else part
Syntax:
if(conditional){
<statements>;
}
else{
<statements>;
}
57
class ifdm
{
public static void main(String ar[])
{
int n = 11;
if(n%2 == 0)
{
System.out.println("This is even number");
}
else
{
System.out.println("This is odd number");
}
}} 58
Switch Statement:
The keyword "switch" is followed by an
expression that should evaluates to byte, short,
char or int primitive data types only.
In a switch block there can be one or more
labeled cases.
The expression that creates labels for the case
must be unique.
The switch expression is matched with each
case label. Only the matched case is
executed ,if no case matches then the default
statement is executed. 59
Switch Statement:
Syntax:
switch(control_expression)
{
case expression 1:
<statement>;
break;
case expression 2:
<statement>;
break;
default:
<statement>;
} 60
class swdm{
public static void main(String ar[]){
int day = 5;
switch(day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid entry");
break;
}
}} 61
looping statement
looping statement can be achieved by three way…..
1) while loop statements:
2) Do-while loop statements:
3) for loop statements:
62
while loop statements:
While loop is entry controlled loop.
In while loop condition check first during the
entry time and then executed block
If block is executed then increment value and
check condition again if true then executed
next time….
Syntax:
while(expression)
{
<statement>;
}
63
class while_demo
{
public static void main(String ar[])
{
int i = 1;
while (i <= 10)
{
System.out.println("Num " + i);
i++;
}
}
}
64
Do-while loop statements:
Do-While loop is exit controlled loop.
In do- while loop block of statement is executed first
and then condition is checked.
If the condition is true the block is executed next
time else not.
So the block executed at least once even if the
condition is false.
Syntax:
do
{
<statement>;
}
while (expression); 65
class do_while_demo
{
public static void main(String ar[])
{
int i = 11;
do
{
System.out.println("Num: " + i);
i++;
}
while(i <= 10);
}
} 66
for loop statements:
This is also a loop statement that provides a
facility repeat over a range of values.
it executes the statements within this block
repeatedly till the specified conditions is true.
Syntax:
for (initialization; condition; inc or dec)
{
<statement>;
}
67
class for_loop_demo
{
public static void main(String ar[])
{
for (int num = 1; num <= 10; num++)
{
System.out.println("Num: " + num);
}
}
}
68
Jump statement
jump statement can be achieved by three way…..
1) break;
2) continue;
3) return;
69
break
The break statement is used for breaking the
execution of a loop (while, do-while and for).
It also terminates the switch statements.
Syntax:
break;
70
class brk_demo
{
public static void main(String ar[])
{
int num[]={2,9,4,5,7};
int srch=4;
for(int i=1;i<num.length;i++)
{
if(num[i]==srch)
{
System.out.println("data found");
break;
}
}
}
} 71
continue;
Continue statement that are used in the looping
statements (while, do-while and for) to skip the
current iteration of the loop and resume the next
iteration .
72
class cont_demo
{
public static void main(String ar[])
{
int num[]={2,9,4,5,7};
int srch=4;
for(int i=1;i<num.length;i++)
{
if(num[i]!=srch)
{
System.out.println("cont");
continue;
}
}
}
} 73
return;
Return statement that transfers the control to the
caller of the method.
This statement is used to return a value to the caller
method and terminates execution of method.
This has two forms:
– one that returns a value (the returned value type must
match the return type of method. )
– other that can not return.
Syntax:
return;
return values;
74