Sie sind auf Seite 1von 85

JAVA

What is JAVA

 Java is a programming language and a platform.

 Java is a high level, robust, secured and object-oriented


programming language.

 Java has its own runtime environment (JRE) and


Application Programming Interface (API).
3

APPLICATIONS OF JAVA
 Desktop Applications such as acrobat reader, media
player, antivirus etc.
 Web Applications such as irctc.co.in, javatpoint.com etc.
 Enterprise Applications such as banking applications.
 Mobile
 Embedded System
 Smart Card
 Robotics
 Games etc.
4

TYPES OF JAVA APPLICATIONS


 Standalone Application, desktop application or
window-based application: Application installed on
every machine such as media player, antivirus, etc.
 Web Application: An application that runs on the
server side and creates dynamic page.
 Enterprise Application: An application that is
distributed in nature, such as banking applications
etc.
 Mobile Application: An application that is created for
mobile devices.
5

FEATURES
 Simple
 Object-Oriented
 Platform independent
 Secured
 Robust
 Architecture neutral
 Portable
 Dynamic
 Interpreted
 High Performance
 Multithreaded
 Distributed
6

C++ vs JAVA
C++ Java
C++ is platform-dependent. Java is platform-independent.
C++ is mainly used for system Java is mainly used for application
programming. programming. It is widely used in
window, web-based, enterprise and
mobile applications.
C++ supports goto statement. Java doesn't support goto
statement.
C++ supports multiple inheritance. Java doesn't support multiple
inheritance through class. It can be
achieved by interfaces in java.
C++ supports operator Java doesn't support operator
overloading. overloading.
7

Contd…
C++ Java
C++ supports pointers. You can Java supports pointer internally. But
write pointer program in C++. you can't write the pointer program
in java. It means java has restricted
pointer support in java.
C++ uses compiler only. Java uses compiler and interpreter
both.
C++ supports both call by value and Java supports call by value only.
call by reference. There is no call by reference in java.
C++ supports structures and unions. Java doesn't support structures and
unions.
C++ doesn't have built-in support Java has built-in thread support.
for threads. It relies on third-party
libraries for thread support.
8
Contd…
C++ Java
C++ doesn't support Java supports documentation comment to
documentation comment. create documentation for java source code.
C++ supports virtual Java has no virtual keyword. We can override
keyword so that we can all non-static methods by default. In other
decide whether or not words, non-static methods are virtual by
override a function. default.
C++ doesn't support >>> Java supports unsigned right shift >>>
operator. operator that fills zero at the top for the
negative numbers. For positive numbers, it
works same like >> operator.
C++ creates a new Java uses single inheritance tree always
inheritance tree always. because all classes are the child of Object
class in java. Object class is the root of
inheritance tree in java.
9

DEFINITIONS
 class keyword is used to declare a class in java.
 public keyword is an access modifier which represents
visibility, it means it is visible to all.
 static is a keyword, if we declare any method as static, it is
known as static method. The core advantage of static method is
that there is no need to create object to invoke the static
method. The main method is executed by the JVM, so it doesn't
require to create object to invoke the main method. So it saves
memory.
 void is the return type of the method, it means it doesn't return
any value.
 main represents startup of the program.
 String[] args is used for command line argument.
 System.out.println() is used to print statement.
10

HELLO PROGRAM
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
System.out.println("Welcome to the world of Java programming");
}
}

Output:
Hello Java
Welcome to the world of Java programming
11

COMPILE TIME
 At the compile time, java file is compiled by Java
Compiler (It does not interact with OS) and converts the
java code into bytecode.
12

RUNTIME
Classloader: It is the subsystem
of JVM that is used to load class
files.
Bytecode Verifier: It checks the
code fragments for illegal code
that can violate access right to
objects.
Interpreter: It reads bytecode
stream and execute the
instructions.
13

MULTIPLE CLASSES IN JAVA


SOURCE FILE
14

JAVA VIRTUAL MACHINE (JVM)


 JVM is an abstract machine.
 It is a specification that provides runtime environment in which
java bytecode can be executed.
 JVMs are available for many hardware and software platforms.
 JVM, JRE and JDK are platform dependent because
configuration of each OS differs. But, Java is platform
independent.
 The JVM performs following main tasks:
 Loads code
 Verifies code
 Executes code
 Provides runtime environment
15

JVM provides definitions for the:

 Memory area

 Class file format

 Register set

 Garbage-collected heap

 Fatal error reporting etc.


16

INTERNAL ARCHITECTURE OF JVM


17

Classloader It is a subsystem of JVM that is used to load class


files.
Class (Method) Area It stores per-class structures such as the runtime
constant pool, field and method data, the code for
methods.
Heap It is the runtime data area in which objects are
allocated.
Stack Java Stack stores frames. It holds local variables
and partial results, and plays a part in method
invocation and return.
Each thread has a private JVM stack, created at the
same time as thread.
A new frame is created each time a method is
invoked. A frame is destroyed when its method
invocation completes.
18

Program counter It contains the address of the Java virtual machine


register instruction currently being executed.
Native method It contains all the native methods used in the
stack application.
Execution 1) A virtual processor
engine
2) Interpreter: Read bytecode stream and execute the
instructions.
3) Just-In-Time(JIT) compiler: It is used to improve the
performance. JIT compiles parts of the byte code that
have similar functionality at the same time. It reduces
the amount of time needed for compilation. Here the
term ?compiler? refers to a translator from the
instruction set of a JVM to the instruction set of a
specific CPU.
19

JAVA RUNTIME ENVIRONMENT


(JRE)
 JRE is used to provide runtime
environment.
 It is the implementation of JVM.
It physically exists.
 It contains set of libraries +
other files that JVM uses at
runtime.
 Implementation of JVMs are
also actively released by other
companies besides Sun Micro
Systems.
20

JAVA DEVELOPMENT KIT (JDK)


 JDK physically exists.
 It contains JRE + development tools.
21

VARIABLES
 Variable is name of reserved area allocated in memory.
 In other words, it is a name of memory location.
 It is a combination of "vary + able" that means its value
can be changed.
22

TYPES OF VARIABLE

Local • It is declared inside the method.

• It is declared inside the class but


Instance outside the method. It is not
declared as static.

• It is declared as static. It cannot


Static be local.
23

EXAMPLE
class A{
int data=50; //instance variable
static int m=100; //static variable
void method(){
int n=90; //local variable
}
}//end of class
24

DATA TYPES
 Data types represent the different values to be stored in the
variable.
25

Data Type Default Value Default size


boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
26

UNICODE SYSTEM
Unicode is a universal international standard character
encoding that is capable of representing most of the
world's written languages.

In unicode, character holds 2 byte, so java also uses 2 byte


for characters.

lowest value:\u0000

highest value:\uFFFF
27
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative */%
additive +-
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ?:
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
28

IF-ELSE STATEMENT
 The Java if statement is used to test the condition. It
checks Boolean condition: true or false.

 There are various types of if statement in java.

 if statement

 if-else statement

 nested if statement

 if-else-if ladder
29

JAVA “IF” STATEMENT


The Java if statement
tests the condition. It
executes the if block if
condition is true.

Syntax:
if(condition){
//code to be executed
}
30

EXAMPLE
public static void main(String[] args)
{
int age=20;
if(age>18){
System.out.print("Age is greater than 18");
}
}

Output:
Age is greater than 18
31

IF-ELSE STATEMENT
The Java if-else statement also
tests the condition. It executes
the if block if condition is true
otherwise else block is
executed.

Syntax:
if(condition){
//code if condition is true
}else
{
//code if condition is false
}
32

EXAMPLE
public static void main(String[] args)
{
int number=13;
if(number%2==0){
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
}
Output:
Odd number
33

IF-ELSE-IF LADDER STATEMENT


 The if-else-if ladder statement executes one condition from
multiple statements.

Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
...
else{
//code to be executed if all the conditions are false
}
34
35
public static void main(String[] args) {
int marks=65;
if(marks<50){
System.out.println("fail");}
else if(marks>=50 && marks<60){
System.out.println("D grade");}
else if(marks>=60 && marks<70){
System.out.println("C grade");}
else if(marks>=70 && marks<80){
System.out.println("B grade");}
else if(marks>=80 && marks<90){
System.out.println("A grade");}
else if(marks>=90 && marks<100){
System.out.println("A+ grade");}else{
System.out.println("Invalid!");
}}}
36
SWITCH STATEMENT
The Java switch statement executes one statement from multiple
conditions. It is like if-else-if ladder statement.

Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
37
38

EXAMPLE
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("10");break;
case 20: System.out.println("20");break;
case 30: System.out.println("30");break;
default:System.out.println("Not in 10, 20 or 30");
}
}

Output
20
39
SWITCH STATEMENT IS FALL-
THROUGH
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("10");
case 20: System.out.println("20");
case 30: System.out.println("30");
default:System.out.println("Not in 10, 20 or 30");
}
}
Output
20
30
Not in 10, 20 or 30
40

FOR LOOP
 The Java for loop is used to iterate a part of the program
several times. If the number of iteration is fixed, it is
recommended to use for loop.

 There are three types of for loop in java.

 Simple For Loop

 For-each or Enhanced For Loop

 Labeled For Loop


41
SIMPLE FOR LOOP
The simple for loop is
same as C/C++. We can
initialize variable, check
condition and
increment/decrement
value.

Syntax:

for(initialization;condition
;incr/decr)
{
//code to be executed
}
42
EXAMPLE
public static void main(String[] args) {
for(int i=1;i<=10;i++){
System.out.println(i);
}
}

Output:
1
:
:
10
43

JAVA FOR-EACH LOOP


The for-each loop is used to traverse array or collection in
java. It is easier to use than simple for loop because we don't
need to increment value and use subscript notation.

Syntax:
for(Type var:array)
{
//code to be executed
}
44

EXAMPLE
public static void main(String[] args) {
int arr[]={12,23,44,56,78};
for(int i:arr){
System.out.println(i);
}
}

Output:
12
23
44
56
78
45
JAVA LABELED FOR LOOP
 Normally, break and continue keywords breaks/continues
the inner most for loop only.

Syntax:
labelname:
for(initialization;condition;incr/decr)
{
//code to be executed
}
46
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println(i+" "+j);}
}}}
Output:
11
12
13
21
47
public static void main(String[] args) {

aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){ Output:
break bb; 11
} 12
System.out.println(i+" "+j); 13
21
} 31
32
} 33
}
48

INFINITIVE FOR LOOP


 Ifyou use two semicolons ;; in the for loop, it will be
infinitive for loop.

Syntax:
for(;;)
{
//code to be executed
}
49

public static void main(String[] args) {


for(;;)
{
System.out.println("infinitive loop");
}
}
Output:
infinitive loop
infinitive loop
infinitive loop
ctrl+c
50

WHILE LOOP
 The Java while loop is used
to iterate a part of the
program several times.
 If the number of iteration is
not fixed, it is recommended
to use while loop.

Syntax:
while(condition)
{
//code to be executed
}
51

public static void main(String[] args) {


int i=1;
while(i<=10){
System.out.println(i);
i++;
}
}
Output:
1
2
:
10
52

INFINITIVE WHILE LOOP


 If
you pass true in the while loop, it will be infinitive
while loop.

Syntax:
while(true)
{
//code to be executed
}
53

DO-WHILE LOOP
 The Java do-while loop is used to iterate a part of the
program several times. If the number of iteration is not
fixed and you must have to execute the loop at least
once, it is recommended to use do-while loop.
 The Java do-while loop is executed at least once because
condition is checked after loop body.
Syntax:
do{
//code to be executed
}while(condition);
54
55
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}

Output:
1
2
:
:
10
56

INFINITIVE DO-WHILE LOOP


 Ifyou pass true in the do-while loop, it will be infinitive
do-while loop.

Syntax:
do
{
//code to be executed
}
while(true);
57

public static void main(String[] args) {


do{
System.out.println("infinitive do while loop");
}while(true);
}
}

Output:
infinitive do while loop
infinitive do while loop
infinitive do while loop
ctrl+c
58

BREAK STATEMENT
 The Java break is used
to break loop or switch
statement.
 It breaks the current
flow of the program at
specified condition.
 In case of inner loop, it
breaks only inner loop.

Syntax:
jump-statement;
break;
59

BREAK STATEMENT WITH LOOP


public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
break;
}
System.out.println(i);
}
}

Output:
1
2
3
4
60
BREAK STATEMENT WITH INNER
LOOP
public static void main(String[] args) {
Output
for(int i=1;i<=3;i++){
11
for(int j=1;j<=3;j++){
if(i==2&&j==2){ 12
break; 13
} 21
System.out.println(i+" "+j); 31
32
} 33
}
}
61
CONTINUE STATEMENT
 The Java continue statement is used to continue
loop.
 It continues the current flow of the program and
skips the remaining code at specified condition.
 In case of inner loop, it continues only inner loop.

Syntax:
jump-statement;
continue;
62

public static void main(String[] args


){ Output
1
for(int i=1;i<=10;i++){
2
if(i==5){ 3
continue; 4
} 6
System.out.println(i); 7
8
}
9
} 10
63

CONTINUE STATEMENT WITH


INNER LOOP
 It continues inner loop only if you use
continue statement inside the inner loop. Output
11
public static void main(String[] args) { 12
for(int i=1;i<=3;i++){ 13
for(int j=1;j<=3;j++){ 21
if(i==2&&j==2){ 23
continue; 31
32
}
33
System.out.println(i+" "+j);
}
}
}
64

COMMENTS

 The java comments are statements that are not


executed by the compiler and interpreter.
 The comments can be used to provide information or
explanation about the variable, method, class or any
statement.
 It can also be used to hide program code for specific
time.
65

TYPES OF COMMENTS

Single line
comment

Multi line
Comments
comment

Documentation
comment
66

SINGLE LINE COMMENT


 The single line comment is used to comment only one
line.
Syntax:
//This is a single line comment
Example:
public static void main(String[] args) {
int i=10;//Here, i is a variable
System.out.println(i);
}
Output:
10
67

MULTI LINE COMMENT


 The multi line comment is used to comment multiple
lines of code.
Example:
public static void main(String[] args) {
/* Let's declare and
print variable in java. */
int i=10;
System.out.println(i);
}
Output:
10
68
DOCUMENTATION COMMENT
 The documentation comment is used to create
documentation API. To create documentation API, you
need to use javadoc tool.

public class Calculator {


/** The add() method returns addition of given numbers.*/
public static int add(int a, int b){return a+b;}
/** The sub() method returns subtraction of given
numbers.*/
public static int sub(int a, int b){return a-b;}
}
69

Compile it by javac tool:


javac Calculator.java

Create Documentation API by javadoc tool:


javadoc Calculator.java

Now, there will be HTML files created for your


Calculator class in the current directory. Open the HTML
files and see the explanation of Calculator class provided
through documentation comment.
70

JAVA PROGRAMS
71
FIBONACCI SERIES
public static void main(String args[])
Output:
{
0
int n1=0,n2=1,n3,i,count=10;
1
System.out.print(n1+" "+n2); //printing 0 and 1 1
for(i=2;i<count;++i)//loop starts from 2 because 0 2
and 1 are already printed 3
{ 5
n3=n1+n2; 8
System.out.print(" "+n3); 13
n1=n2; 21
n2=n3; 34
}
}
72
PRIME NUMBER
public static void main(String args[]){
int i,m=0,flag=0;
int n=17;//it is the number to be checked
m=n/2;
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println("Number is not prime");
flag=1;
break;
} Output:
} Number is prime
if(flag==0)
System.out.println("Number is prime");
}
73

PALINDROME NUMBER
public static void main(String args[]){
int r,sum=0,temp;
int n=454;//It is the number variable to be checked
for palindrome
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum) Output:
System.out.println("palindrome number ");
else Palindrome number
System.out.println("not palindrome");
}
74
FACTORIAL NUMBER
public static void main(String args[]){
int i,fact=1;
int number=5; //It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}

Output:
Factorial of 5 is: 120
75
FACTORIAL PROGRAM
USING RECURSION
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,fact=1;
int number=4;//It is the number to calculate factorial
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
Output:
Factorial of 4 is: 24
76

ARMSTRONG NUMBER
public static void main(String[] args) {
int c=0,a,temp;
int n=153;//It is the number to check armstrong
temp=n;
while(n>0){
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
}
Output:
Armstrong number
77

BUBBLE SORT
 In bubble sort algorithm, array is traversed from first
element to last element.
 Here, current element is compared with the next
element.
 If current element is greater than the next element, it is
swapped.
78
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
79
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
System.out.println("Array Before Bubble
Sort");
for(int i=0; i < arr.length; i++){ Output:
System.out.print(arr[i] + " ");
} Array Before Bubble
System.out.println(); Sort
bubbleSort(arr);//sorting array elements 3 60 35 2 45 320 5
using bubble sort
System.out.println("Array After Bubble Array After Bubble Sort
Sort"); 2 3 5 35 45 60 320
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
80
SELECTION
SORT
 In selection
sort algorithm,
we search for
the lowest
element and
arrange it to
the proper
location.
 We swap the
current element
with the next
lowest number.
81
public class SelectionSortExample {
public static void selectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
82
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Selection
Sort");
for(int i:arr1){ Output:
System.out.print(i+" ");
Before Selection Sort
}
9 14 3 2 43 11 58 22
System.out.println();
selectionSort(arr1);//sorting array using After Selection Sort
selection sort 2 3 9 11 14 22 43 58
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
83

INSERTION SORT
 Insertion is good for small elements only because it
requires more time for sorting large number of
elements.
84
INSERTION SORT ALGORITHM
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
85
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Insertion
Sort");
Output:
for(int i:arr1){
System.out.print(i+" "); Before Insertion Sort
} 9 14 3 2 43 11 58 22
System.out.println();
insertionSort(arr1); After Insertion Sort
//sorting array using insertion sort 2 3 9 11 14 22 43 58
System.out.println("After Insertion
Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}

Das könnte Ihnen auch gefallen