Basics Of Java
Topics
Java – What, Where,and why?
History Of Java
Features of Java
Example Of java Program
Internal work of java Program
Difference between JDK,JRE and JVM
Internal Details of JVM
Variable and DataType
Naming Convention
Type Casting
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 API ,it is called Platform
Where it is used?
• Desktop Applications such as acrobat reader,
media player, antivirus etc.
• Web Applications such as [Link]
• Enterprise Applications such as banking
applications.
• Mobile.
• Smart card.
• Robotics.
• Games etc.
Types of Java Application
There are mainly 4 types of applications that can
be created using java programming.
• Standalone Application
• Web Application
• Enterprise Application
• Mobile Application
Standalone Application
• It is also known as desktop application or
windows-based application.
• An application that we need to install on every
machine such as media player, antivirus etc.
• AWT(Abstract Window Toolkit) and Swing are
used in java for creating standalone
application.
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.
Enterprise Application
• An application that is distributed in nature,
such as banking application it is known as
Enterprise Application.
• It has the advantage of high level security,
load balancing and clustering.
• EJB is used for creating enterprise application.
Mobile Application
• An application that is created for mobile
devices.
• Currently Android and Java ME are used for
creating mobile applications.
History Of Java
• Java was developed by James Gosling at Sun
Microsystems and released in 1995.
• James Gosling, Mike Sheridan and Patrick Naughton, the
small team of sun engineers called Green Team in 1991.
• Firstly , it was called “Greentalk”.
• After that, it is called Oak.
• In 1995, Oak was renamed as ‘Java’ because it was
already a trademark by Oak Technologies.
• Java is an island of Indonesia where first coffee was
produced (called java coffee)
Java Version History
• JDK Alpha and Beta (1995)
• JDK 1.0 (23rd Jan, 1996)
• JDK 1.1 (19th Feb, 1997)
• J2SE 1.2 (8th Dec, 1998)
• J2SE 1.3 (8th May, 2000)
• J2SE 1.4 (6th Feb, 2002)
• J2SE 5.0 (30th Sep, 2004)
• Java SE 6 (11th Dec, 2006)
• Java SE 7 (28th July, 2011)
• Java SE 8 (18th March, 2014)
Features of Java
• Simple
• Object-Oriented
• Platform independent
• Secured
• Robust
• Architecture neutral
• Portable
• Dynamic
• Interpreted
• High Performance
• Multithreaded
• Distributed
1) Simple
• According to Sun, Java language is simple
because: syntax is based on C++ (so easier for
programmers to learn it after C++).
• Removed many confusing and/or rarely-used
features e.g., explicit pointers, operator
overloading etc.
• No need to remove unreferenced objects
because there is Automatic Garbage
Collection in java.
2) Object-Oriented
• Object-oriented means we organize our software as a
combination of different types of objects that
incorporates both data and behaviour.
• Basic concepts of OOPs are:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
3) Platform Independent
• 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)
4) Secured
• Java is secured because:
- No explicit pointer
- Programs run inside virtual machine sandbox.
5) Robust
• Robust simply means strong.
• Java uses strong memory management.
• There are lack of pointers that avoids security
problem.
• There is automatic garbage collection in java.
• There is exception handling and type checking
mechanism in java.
• All these points makes java robust.
6) Architecture-neutral
• There is no implementation dependent
features e.g. size of primitive types is set.
7) Portable
• We may carry the java bytecode to any
platform
8)High-Performance
• Java is faster than traditional interpretation
since byte code is "close" to native code still
somewhat slower than a compiled language
(e.g., C++)
9) Distributed
• We can create distributed applications in java.
RMI and EJB are used for creating distributed
applications.
10)Multi-Threaded
• A thread is like a separate program, executing
concurrently.
• We can write Java programs that deal with
many tasks at once by defining multiple
threads.
• The main advantage of multi-threading is that it
shares the same memory.
• Threads are important for multi-media, Web
applications etc.
Example of Java Program
• Let's create the hello java program:
class Simple{
public static void main(String args[]){
[Link]("Hello Java");
}
}
• save this file as [Link]
• To compile: javac [Link]
• To execute: java Simple
Internal working of program
What happens at runtime?
• At runtime, following steps are performed:
• Classloader: is the subsystem of JVM that is
used to load class files.
• Bytecode Verifier: checks the code fragments
for illegal code that can violate access right to
objects.
• Interpreter: read bytecode stream then
execute the instructions.
Meaning of the Program
• 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.
• [Link]() is used print statement.
Valid java main method signature
• public static void main(String[] args)
• public static void main(String []args)
• public static void main(String args[])
• public static void main(String... args)
• static public void main(String[] args)
• public static final void main(String[] args)
• final public static void main(String[] args)
• final strictfp public static void main(String[] args)
Invalid java main method signature
• public void main(String[] args)
• static void main(String[] args)
• public void static main(String[] args)
• abstract public static void main(String[] args)
Difference Between
• JVM
• JRE
• JDK
JVM
• JVM (Java Virtual Machine) is an abstract
machine. It is a specification that provides
runtime environment in which java bytecode
can be executed.
• The JVM performs following main tasks:
- Loads code
- Verifies code
- Executes code
- Provides runtime environment
JRE
• JRE is an acronym for Java Runtime
Environment.
• It is used to provide runtime [Link] is
the implementation of JVM.
• It physically [Link] contains set of libraries +
other files that JVM uses at runtime.
JDK
• JDK is an acronym for Java Development Kit.
• It physically exists. It contains JRE +
development tools.
What is JVM
It is:
• A specification where working of Java Virtual Machine is
specified. But implementation provider is independent to
choose the algorithm. Its implementation has been
provided by Sun and other companies.
• An implementation Its implementation is known as JRE
(Java Runtime Environment).
• Runtime Instance Whenever you write java command on
the command prompt to run the java class, and instance
of JVM is created.
What it does?
The JVM performs following operation:
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
JVM provides definitions for the:
• Memory area
• Class file format
• Register set
• Garbage-collected heap
• Fatal error reporting etc.
Internal Architecture of JVM
1) Classloader:
Classloader is a subsystem of JVM that is used to
load class files.
2) Class(Method) Area:
Class(Method) Area stores per-class structures such
as the runtime constant pool, field and method data,
the code for methods.
3) Heap:
It is the runtime data area in which objects are
allocated.
4) Stack:
Java Stack stores [Link] 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.
5) Program Counter Register:
PC (program counter) register. It contains the
address of the Java virtual machine instruction
currently being executed.
6) Native Method Stack:
It contains all the native methods used in the
application.
7) Execution Engine:
It contains:
• A virtual processor
• Interpreter:Read bytecode stream then execute the
instructions.
• Just-In-Time(JIT) compiler:It is used to improve the
[Link] compiles parts of the byte code that
have similar functionality at the same time, and
hence reduces the amount of time needed for
compilation.
Variable
• Variable is a name of memory location.
• There are three types of variables: local,
instance and static.
• Variable is name of reserved area allocated in
memory.
Types of Variable
There are three types of variables in java .
• local variable
• instance variable
• static variable
• Local Variable:A variable that is declared
inside the method is called local variable.
• Instance Variable:A variable that is declared
inside the class but outside the method is
called instance variable . It is not declared as
static.
• Static variable:A variable that is declared as
static is called static variable. It cannot be
local.
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
Data Types in Java
In java, there are two types of data types
• primitive data types
• non-primitive data types
Java Naming conventions
• Java naming convention is a rule to follow as
you decide what to name your identifiers such
as class, package, variable, constant, method
etc.
• But, it is not forced to follow. So, it is known as
convention not rule.
• All the classes, interfaces, packages, methods
and fields of java programming language are
given according to java naming convention.
NAME Convention
class name should start with uppercase letter and be
a noun e.g. String, Color, Button, System,
Thread etc.
interface name should start with uppercase letter and be
an adjective e.g. Runnable, Remote,
ActionListener etc.
method name should start with lowercase letter and be
a verb e.g. actionPerformed(), main(),
print(), println() etc.
variable name should start with lowercase letter e.g.
firstName, orderNumber etc.
package name should be in lowercase letter e.g. java,
lang, sql, util etc.
constants name should be in uppercase letter. e.g. RED,
YELLOW, MAX_PRIORITY etc.
CamelCase in java naming conventions
• Java follows camelcase syntax for naming the
class, interface, method and variable.
• If name is combined with two words, second
word will start with uppercase letter always
e.g. actionPerformed(), firstName,
ActionEvent, ActionListener etc.