JAVA Unit-1
JAVA Unit-1
• Platform Any hardware or software environment in which a program runs, known as a platform. Since Java has its own Runtime Environment
(JRE) and API, it is called platform.
• According to Sun, 3 billion devices run java. There are many devices where java is currently used. Some of them are as follows:
Mobile
Embedded System
Smart Card
Robotics
Games etc.
TYPES OF JAVA APPLICATIONS
1) Standalone Application
It is also known as desktop application or window-based application. An application that
we need to install on every machine such as 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 dynamic page, is called web
application. Currently, servlet, jsp, struts, 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. It has the
advantage of high level security, load balancing and clustering. In java, EJB is used for
creating enterprise applications.
4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME are used
for creating mobile applications.
History of Java
• The history of java starts from Green Team. Java team members (also
known as Green Team), initiated a revolutionary task to develop a
language for digital devices such as set-top boxes, televisions etc.
• For the green team members, it was an advance concept at that time.
But, it was suited for internet programming. Later, Java technology as
incorporated by Netscape.
• Currently, Java is used in internet programming, mobile devices,
games, e-business solutions etc. There are given the major points that
describes the history of java.
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language
project in June 1991. The small team of sun engineers called Green Team.
2) Originally designed for small, embedded systems in electronic appliances like set-top
boxes.
3) Firstly, it was called "Greentalk" by James Gosling and file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries
like U.S.A., France, Germany, Romania etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
7) Why they choosed java name for java language? The team gathered to choose a new
name. The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA" etc.
They wanted something that reflected the essence of the technology: revolutionary,
dynamic, lively, cool, unique, and easy to spell and fun to say.
• According to James Gosling "Java was one of the top choices along with Silk". Since java
was so unique, most of the team members preferred java.
8) Java is an island of Indonesia where first coffee was produced (called java coffee).
FEATURES OF JAVA OR BUZZWORDS
• Simple
• Object-Oriented
• Platform independent
• Secured
• Robust
• Architecture neutral
• Portable
• Dynamic
• Interpreted
• High Performance
• Multithreaded
• Distributed
Simple Object-oriented
• According to Sun, Java language is simple because: • Object-oriented means we organize our software
as a combination of different types of objects that
• syntax is based on C++ (so easier for programmers incorporates both data and behavior.
to learn it after C++).
• Object-oriented programming(OOPs) is a
• removed many confusing and/or rarely-used methodology that simplify software development
features e.g., explicit pointers, operator and maintenance by providing some rules
overloading etc.
• Basic concepts of OOPs are:
• No need to remove unreferenced objects because
there is Automatic Garbage Collection in java. Object
Class
Robust Inheritance
• Robust simply means strong. Polymorphism
• Java uses strong memory management. Abstraction
• There are lack of pointers that avoids security Encapsulation
problem.
• There is automatic garbage collection in java.
• There is exception handling and type checking
mechanism in java.
• All these points makes java robust.
Platform Independent
• A platform is the hardware or software environment in which a program
runs.
• There are two types of platforms software-based and hardware-based.
• Java provides software-based platform. The Java platform differs from
most other platforms in the sense that it's a software-based platform that
runs on top of other hardware-based platforms.
• It has two components:
Runtime Environment
API(Application Programming Interface)
• Java code can be run on multiple platforms e.g. Windows, Linux,Sun
Solaris, Mac/OS etc.
• Java code is compiled by the compiler and converted into bytecode which
is WORA i.e. Write Once and Run Anywhere(WORA).
Distributed
Architecture-neutral
We can create distributed applications in java.
• There is no implementation dependent RMI and EJB are used for creating distributed
features e.g. size of primitive types is set. applications.
We may access files by calling the methods
Portable from any machine on the internet.
• We may carry the java bytecode to any
platform. Multi-threaded
A thread is like a separate program, executing
High-performance concurrently.
• Java is faster than traditional We can write Java programs that deal with
interpretation since byte code is "close" many tasks at once by defining multiple
to native code still somewhat slower than threads.
a compiled language (e.g., C++) The main advantage of multi-threading is that
it shares the same memory.
Threads are important for multi-media, Web
applications etc.
Simple Program of Java
• class keyword is used to declare a class in java.
import java.lang.*; • public keyword is an access modifier which represents
visibility, it means it is visible to all.
import java.io.*; • static is a keyword, if we declare any method as static,
class Simple{ it is known as static method.
public static void main(String args[]){ • The core advantage of static method is that there is no
need to create object to invoke the static method.
System.out.println("Hello Java");
• 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.
• save this file as Simple.java
• main represents startup of the program.
• To compile: javac Simple.java
• String[] args is used for command line argument. We
• To execute: java Simple will learn it later.
• System.out.println() is used print statement. We will
learn about the internal working of System.out.println
statement later.
What happens at runtime?
What happens at compile time
DIFFERENCE BETWEEN JDK, JRE AND JVM
• JVM (Java Virtual Machine) is an abstract
machine. It is a specification that provides
runtime environment in which java bytecode
can be executed.
• JRE is an acronym for Java Runtime
Environment. It 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.
• JDK is an acronym for Java Development Kit. It
physically exists. It contains JRE + development
tools.
INTERNAL ARCHITECTURE OF JVM
• Class loader is a subsystem of JVM that is used to load class files.
• Class(Method) Area 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.
• Java Stack stores frames. It holds local variables and partial results, and plays a part
in method invocation and return
• PC (program counter) register: It contains the address of the Java virtual machine
instruction currently being executed.
• Native Method Stack: It contains all the native methods used in the application.
• Execution Engine: It contains:
1) A virtual processor
2) Interpreter: Read bytecode stream then 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, and
hence reduces the amount of time needed for compilation.
Here the term ?compiler? refers to a translator from the instruction set of a Java virtual
machine (JVM) to the instruction set of a specific CPU.
Variables in Java • Variable Initialization in Java
• A variable in Java is a container that 1. Static:
holds the value during the execution of When the class is loaded into the memory, the
Java program. memory is determined for the variables. Such
variables are known as static variables or class
variables in java.
void method(){
int n=90;//local variable
}
}//end of class
Example int totalMarks = cMarks + pMarks + mMarks;
public class Student System.out.println("Total marks in PCM: " +totalMarks);
{ }
Constants Meaning
‘b‘ back space
‘f‘ form feed
‘n‘ new line
‘r‘ carriage return
‘t‘ horizontal tab
‘‘‘ single quote
‘”‘ double quote
‘ ‘ backslash
Operators in Java • Classification of Operators based on
Symbols and Named in Java
A ~A
0 1
1 0
Shift Operator
Left Shift Operator