Beruflich Dokumente
Kultur Dokumente
What is JAVA
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
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
Memory area
Register set
Garbage-collected heap
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
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
UNICODE SYSTEM
Unicode is a universal international standard character
encoding that is capable of representing most of the
world's written languages.
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.
if statement
if-else statement
nested if statement
if-else-if ladder
29
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
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.
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
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
Syntax:
for(;;)
{
//code to be executed
}
49
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
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
Syntax:
do
{
//code to be executed
}
while(true);
57
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
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
COMMENTS
TYPES OF COMMENTS
Single line
comment
Multi line
Comments
comment
Documentation
comment
66
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+" ");
}
}