JAVA Unit 1
JAVA Unit 1
Java is a programming language and a platform. Java is a high level, robust, object-oriented
and secure programming language.
Java was developed bySun Microsystems(which is now the subsidiary of Oracle) in the year
1995.James Goslingis known as the father of Java. Before Java, its name wasOak. Since Oak
was already a registered company, so James Gosling and his team changed the name from Oak
to Java.
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,EJBis 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.
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 is the implementation of JVM. It physically
exists. It contains a set of libraries + other files that JVM uses at runtime.
The implementation of JVM is also actively released by other companies besides Sun Micro
Systems.
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop Java applications andapplets. It physically
exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:
JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform
dependent because the configuration of eachOSis different from each other. However, Java is
platform independent. There are three notions of the JVM:specification,implementation,
andinstance.
Loads code
Verifies code
Executes code
What is JVM
It is:
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader, memory area,
execution engine etc.
1) Classloader
Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java
program, it is loaded first by the classloader. There are three built-in classloaders in Java.
1.Bootstrap ClassLoader: This is the first classloader which is the super class of
Extension classloader. It loads thert.jarfile which contains all class files of Java Standard
Edition like java.lang package classes, java.net package classes, java.util package
classes, java.io package classes, java.sql package classes etc.
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 frames. It 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.
7) Execution Engine
It contains:
Types of Variables
There are three types of variables inJava:
1. local variable
2. instance variable
3. static variable
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.
It is called an instance variable because its value is instance-specific and is not shared among
instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can create
a single copy of the static variable and share it among all the instances of the class. Memory
allocation for static variables happens only once when the class is loaded in the memory.
1.Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
2.Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example:
The byte data type is used to save memory in large arrays where the memory savings is most
required. It saves space because a byte is 4 times smaller than an integer. It can also be used
in place of "int" data type.
Example:
The short data type can also be used to save memory just like byte data type. A short data
type is 2 times smaller than an integer.
Example:
The int data type is generally used as a default data type for integral values unless if there is
no problem about memory.
Example:
Example:
Example:
float f1 = 234.5f
Example:
double d1 = 12.3
Char Data Type
The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000'
(or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store characters.
Example:
Type casting
Convert a value from one data type to another data type is known as type casting.
For example, the conversion between numeric data type to char or Boolean is not done
automatically. Also, the char and Boolean data types are not compatible with each other. Let's
see an example.
WideningTypeCastingExample.java
1. double -> float -> long -> int -> char -> short -> byte
Let's see an example of narrowing type casting.
In the following example, we have performed the narrowing type casting two times. First, we
have converted the double type into long data type after that long data type is converted into
int type.
NarrowingTypeCastingExample.java
public class NarrowingTypeCastingExample
{
public static void main(String args[])
{
double d = 166.66;
//converting double data type into long data type
long l = (long)d;
//converting long data type into int data type
int i = (int)l;
System.out.println("Before conversion: "+d);
//fractional part lost
System.out.println("After conversion into long type: "+l);
//fractional part lost
System.out.println("After conversion into int type: "+i);
}
}
Output