App Unit-2 (First Half)
App Unit-2 (First Half)
PARADIGMS
Syllabus
Object and Classes; Constructor; Data types; Variables; Modifier and
Operators - Structural Programming Paradigm: Branching, Iteration,
Decision making, and Arrays - Procedural Programming
Paradigm:Characteristics; Function Definition; Function Declaration
and Calling; Function Arguments - Object-Oriented Programming
Paradigm: Abstraction; Encapsulation; Inheritance; Polymorphism;
Overriding -Interfaces: Declaring, Implementing; Extended and Tagging
- Package: Package Creation.
What is Java?
Java is a high level, robust, object-oriented and secure programming language
Application
1.Desktop Applications such as acrobat reader, media player, antivirus, etc.
2.Web Applications such as irctc.co.in, javatpoint.com, etc.
3.Enterprise Applications such as banking applications.
4.Mobile
5.Embedded System
6.Smart Card
7.Robotics
8.Games, etc.
Types of Java Applications
1) Standalone Application
Standalone applications are also known as desktop applications or window-based applications. These are
traditional software that we need to install on every machine. Examples of standalone application are
Media player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for creating web
applications in Java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called an enterprise
application. It has advantages like high-level security, load balancing, and clustering. In Java, EJB is
used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application. Currently, Android and
Java ME are used for creating mobile applications.
Java Platforms / Editions
The execution of all Java programs starts from the main() method. In other words, it is an entry point of the class. It must be inside the class.
Inside the main method, we create objects and call the methods. We use the following statement to define the main() method:
public static void main(String args[]) {
}
For example:public class Student //class definition {
public static void main(String args[]) {
//statements
} }
Methods and behavior
We define the functionality of the program by using the methods. The methods are the set of instructions that we want to perform. These
instructions execute at runtime and perform the specified task. For example:
public class Demo //class definition {
public static void main(String args[]) {
void display() {
System.out.println("Welcome to java");
} statements
} }
Object
➢ An object is a real-world entity.
➢ An object is a runtime entity.
class <class_name>{
field;
method;
}
Object and Class Example
main within the class main outside the class
Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists. They are arranged in a way that each constructor performs a different task.
//Java program to overload constructors void display(){System.out.println(id+" "+name+"
class Student5{ "+age);}
int id;
String name; public static void main(String args[]){
int age; Student5 s1 = new Student5(111,"Karan");
//creating two arg constructor Student5 s2 = new Student5(222,"Aryan",25);
Student5(int i,String n){ s1.display();
id = i; s2.display();
name = n; }
} }
//creating three arg constructor Output:
Student5(int i,String n,int a){ 111 Karan 0
id = i; 222 Aryan 25
name = n;
age=a;
}
Data Types in Java
class A{
private int data=40;
private void msg(){
System.out.println("Hello java");
}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
Example of default access modifier
//save by A.java
package pack;
class A{
void msg() {System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
void display()
{System.out.println("Hello");}
A obj = new A();//Compile Time Error
B s1 =new B();
s1.display();
obj.msg();//Compile Time Error
} }
Example of protected access modifier
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Output:Hello
Example of public access modifier
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello"); } }
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
Operators in Java
The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:
incrementing/decrementing a value by one
negating an expression
Output:
inverting the value of a boolean
10
Java Unary Operator Example: ++ and -- 12
public class OperatorExample{ 12
10
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Java Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as
basic mathematical operations.
Java Arithmetic Operator Example
public class OperatorExample{
public static void main(String args[]){
int a=10; Output:
int b=5; 15
System.out.println(a+b);//15 5
50
System.out.println(a-b);//5 2
System.out.println(a*b);//50 0
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Java Left Shift Operator
The Java left shift operator << is used to shift all of the bits in a value to the left
side of a specified number of times.
Java Left Shift Operator Example
public class OperatorExample
{
public static void main(String args[])
{
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
}
}
Output:
40
80
Java Right Shift Operator
The Java right shift operator >> is used to move the value of the left
operand to right by the number of bits specified by the right operand.
Java Right Shift Operator Example
public OperatorExample{
public static void main(String args[]) Output:
2
{ 5
System.out.println(10>>2);//10/2^2=10/4=2 2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}
}
Java AND Operator Example: Logical && and
Bitwise &
The logical && operator doesn't check the second condition if the first condition is false. It
checks the second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is true or
false.
public class OperatorExample{
public static void main(String args[])
{ Output:
int a=10; False
false
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}
}
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check the second condition if the first condition is true. It
checks the second condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
Java Ternary operator is used as one line replacement for if-then-else statement and used a
lot in Java programming. It is the only conditional operator which takes three operands.
Java Ternary Operator Example
public class OperatorExample
{
public static void main(String args[])
{ Output:
2
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Java Assignment Operator
Java assignment operator is one of the most common operators. It is used to assign the value on its right to
the operand on its left.
Java Assignment Operator Example
public class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3 Output:
System.out.println(a); 13
a-=4;//13-4 9
System.out.println(a); 18
a*=2;//9*2 9
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
STRUCTURAL PROGRAMMING
PARADIGM
➢ Loop statements
➢ do while loop
➢ while loop
➢ for loop
➢ for-each loop
➢ Jump statements
➢ break statement
➢ continue statement
Decision-Making statements:
It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement is given below.
if(condition) {
statement 1; //executes when condition is true
}
Output:
Student.java
x + y is greater
public class Student {
than 20
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
if-else statement
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
3) if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if statements
Student.java
public class Student {
public static void main(String[] args) {
String city = "Delhi";
Output:
if(city == "Meerut") {
Delhi
System.out.println("city is meerut");
}else if (city == "Noida") {
System.out.println("city is noida");
}else if(city == "Agra") {
System.out.println("city is agra");
}else {
System.out.println(city);
}
}
}
Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if
or else-if statement.
Syntax of Nested if-statement.
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
Switch Statement
The switch statement contains multiple blocks of code called cases
and a single case is executed based on the variable which is being
switched. The switch statement is easier to use instead of if-else-if
statements. It also enhances the readability of the program.
Cases cannot be duplicate
Default statement is executed when any of the case doesn't match
the value of expression. It is optional.
Break statement terminates the switch block when the condition is
satisfied.It is optional, if not used, next case is executed.
The syntax to use the switch statement
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
Example switch statement
public class Student implements Cloneable {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0"); Output:
break; 2
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}
Loop Statements
➢ do-while loop
Java for loop
It enables us to initialize the loop variable, check the condition, and increment/decrement in a
single line of code. We use the for loop only when we exactly know the number of times, we want
to execute the block of code.
for(initialization, condition, increment/decrement) {
//block of statements
}
Calculation.java
public class Calculattion {
public static void main(String[] args) {
int sum = 0; Output:
for(int j = 1; j<=10; j++) { The sum of first 10 natural numbers is
sum = sum + j; 55
}
System.out.println("The sum of first 10 na
tural numbers is " + sum);
}
}
Java for-each loop
for(data_type var : array_name/collection_name){
//statements
}
E.g
public class Calculation {
public static void main(String[] args) { Output:
Printing the content of the
// TODO Auto-generated method stub
array names:
String[] names = {"Java","C","C++","Python","JavaScript"}; Java
System.out.println("Printing the content of the array names:\n"); C
for(String name:names) { C++
System.out.println(name); Python
JavaScript
}
}
}
Java while loop
It is also known as the entry-controlled loop since the condition is checked at the start of the loop.
If the condition is true, then the loop body will be executed; otherwise, the statements after the
loop will be executed.
The syntax of the while loop is given below.
while(condition){
//looping statements
}
public class Calculation {
public static void main(String[] args) { Output:
// TODO Auto-generated method stub Printing the list of first 10
int i = 0; even numbers
System.out.println("Printing the list of fir 0
st 10 even numbers \n"); 2
while(i<=10) { 4
System.out.println(i); 6
i = i + 2; 8
} 10
}
}
Java do-while loop
The do-while loop checks the condition at the end of the loop after executing the loop statements. When the
number of iteration is not known and we have to execute the loop at least once, we can use do-while loop.It is also
known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop
do
{
//statements
} while (condition);
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
do {
Printing the list of first 10 even
System.out.println(i); numbers
i = i + 2; 0
}while(i<=10); 2
} 4
} 6
8
10
Jump Statements
Jump statements are used to transfer the control of the program to the specific statements
The continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of
the loop immediately.
public class ContinueExample {
public static void main(String[] args) {
// TODO Auto-generated method stub Output:
0
for(int i = 0; i<= 2; i++) {
1
for (int j = i; j<=5; j++) { 2
if(j == 4) { 3
continue; 5
} 1
System.out.println(j); 2
} 3
} 5
}
}
Java - Arrays
An array is a collection of similar type of elements which has
contiguous memory location.
Advantages
Code Optimization: It makes the code optimized, we can retrieve or
sort the data efficiently.
Random access: We can get any data located at an index position.
Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
Types of Array in java
There are two types of array.
➢ Single Dimensional Array
➢ Multidimensional Array
arrayRefVar=new datatype[size];
Multidimensional Array in Java
In such case, data is stored in row and column based index (also known as matrix form).