Java
Unit-1 Part-2
Prepared By Mr. Amit Kumar
Introduction
• Java is a high level, object-oriented and
secure programming language.
• Java was developed by Sun Microsystems in
the year 1995.
• James Gosling is known as the father of Java.
• Before Java, its name was Oak.
Types of Java Applications
1) Standalone Application
Standalone applications are also known as desktop
applications or window-based applications. 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 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 enterprise
application. 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
1) Java SE (Java Standard Edition)
It is a Java programming platform. It includes Java programming APIs
such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It
includes core topics like OOPs, String, Exception, Multithreading,
I/O Stream, Networking, AWT, Swing, Collection, etc.
2) Java EE (Java Enterprise Edition)
It is an enterprise platform which is mainly used to develop web and
enterprise applications. It includes topics like Servlet, JSP, Web
Services, EJB etc.
3) Java ME (Java Micro Edition)
It is a micro platform which is mainly used to develop mobile
applications.
Features of Java
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
Simple
• Java is very easy to learn, and its syntax is simple, clean
and easy to understand. According to Sun, Java language
is a simple programming language because:
• Java syntax is based on C++ (so easier for programmers
to learn it after C++).
• Java has removed many complicated and rarely-used
features, for example, pointers, operator overloading,
etc.
• There is no need to remove unreferenced objects
because there is an Automatic Garbage Collection in
Java.
Object-oriented
• Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Platform Independent
• Java is platform independent because it is
different from other languages like C, C++, etc.
which are compiled into platform specific
machines while Java is a write once, run
anywhere language. A platform is the
hardware or software environment in which a
program runs.
Secured
• Java is best known for its security. With Java,
we can develop virus-free systems. Java is
secured because:
• No explicit pointer
• Java Programs run inside a virtual machine
sandbox
Robust
• Robust simply means strong. Java is robust
because:
• Compile time and runtime error checking
• lack of pointers that avoids security problems.
• automatic garbage collection
• exception handling and the type checking
mechanism in Java.
All these points make Java robust.
Portable
• Java is portable because it facilitates you to
carry the Java bytecode to any platform.
High-performance
• Java is faster than other traditional interpreted
programming languages because
multithreading in java also increase execution
speed of java programs.
Distributed
• Java is distributed because it facilitates users
to create distributed applications in Java. RMI
and EJB are used for creating distributed
applications. This feature of Java makes us
able to access files by calling the methods
from any machine on the internet.
Multi-threaded
• Multithreading in Java is a process of
executing multiple Thread simultaneously.
• A Thread is a lightweight sub process, smallest
unit of processing.
• It is used for achieving multitasking.
• Threads are important for multi-media, Web
applications, etc.
Dynamic
• Java is a dynamic language. It supports
dynamic loading of classes. It means classes
are loaded on demand.
• Java supports dynamic compilation and
automatic memory management (garbage
collection).
Difference between JDK, JRE, and JVM
• JVM
• JVM (Java Virtual Machine) is an abstract
machine. It is called a virtual machine because
it doesn't physically exist. It is a specification
that provides a runtime environment in which
Java bytecode can be executed. It can also run
those programs which are written in other
languages and compiled to Java bytecode.
• The JVM performs the following main tasks:
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
JRE
• JRE is an acronym for Java Runtime
Environment. It is also written as Java RTE. The
Java Runtime Environment is a set of software
tools which are used for developing Java
applications. It is used to provide the runtime
environment. It physically exists. It contains a
set of libraries + other files that JVM uses at
runtime.
JDK
• JDK means Java Development Kit. The Java
Development Kit (JDK) is a software
development environment which is used to
develop Java applications and applets. It
physically exists. It contains JRE +
development tools.
Java Variables
• A variable is a container which holds the value
while the Java program is executed. A variable
is assigned with a data type
• Variable is a name of memory location. There
are three types of variables in java: local,
instance and static.
1) Local Variable
• A variable declared inside the body of the
method is called local variable. You can use
this variable only within that method and the
other methods in the class aren't even aware
that the variable exists.
• A local variable cannot be defined with "static"
keyword.
2) Instance Variable
• A variable declared inside the class but
outside the body of the method, is called
instance variable. It is not declared as static.
• It is called instance variable because its value
is instance specific and is not shared among
instances.
3) Static variable
• A variable which is declared as static is called
static variable. It cannot be local. You can
create a single copy of static variable and
share among all the instances of the class.
Memory allocation for static variable happens
only once when the class is loaded in the
memory.
Example to understand the types of variables in java
class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class
Data Types in Java
• Primitive data types: The primitive data types
include boolean, char, byte, short, int, long,
float and double.
• Non-primitive data types: The non-primitive
data types include Classes, Interfaces,
and Arrays.
Operators in Java-The Arithmetic Operators
The Relational Operators
The Bitwise Operators
The Logical Operators
The Assignment Operators
Conditional Operator ( ? : )
• Conditional operator is also known as
the ternary operator.
• This operator consists of three operands and is
used to evaluate Boolean expressions. The
goal of the operator is to decide, which value
should be assigned to the variable. The
operator is written as −
• variable x = (expression) ? value if true : value
if false
Java If-else Statement
• if statement
• if-else statement
• if-else if statement
• nested if statement
Switch 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;
}
Loop
• for loop
• while loop
• do-while loop
For Loop
Syntax:
for(initialization;condition;incr/decr){
//statement or code to be executed
}
While Loop
Syntax:
while(condition){
//code to be executed
}
Do While Loop
Syntax:
do{
//code to be executed
}while(condition);
Break statement
for(int i=1;i<=10;i++){
if(i==5){
//breaking the loop
break;
}
Continue Statement
for(int i=1;i<=10;i++){
if(i==5){
//using continue statement
continue;//it will skip the rest statement
}
Comments in Java
• Single Line Comment
• Multi Line Comment
Single line comment-
Syntax:
//This is single line comment
Multi-line comment
Syntax:
/*
This
is
multi line
comment
*/