0% found this document useful (0 votes)
46 views

JAVA Unit 1

Java is an object-oriented programming language created in 1995 by James Gosling at Sun Microsystems. There are four main types of Java applications: standalone, web, enterprise, and mobile. The Java Runtime Environment (JRE) provides the runtime environment for Java programs and includes the Java Virtual Machine (JVM) which executes Java bytecode. The Java Development Kit (JDK) contains the JRE plus development tools and is used to develop Java applications. The JVM is an abstract machine that provides a runtime environment allowing Java bytecode to be executed on various platforms.

Uploaded by

Tofajul haque
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

JAVA Unit 1

Java is an object-oriented programming language created in 1995 by James Gosling at Sun Microsystems. There are four main types of Java applications: standalone, web, enterprise, and mobile. The Java Runtime Environment (JRE) provides the runtime environment for Java programs and includes the Java Virtual Machine (JVM) which executes Java bytecode. The Java Development Kit (JDK) contains the JRE plus development tools and is used to develop Java applications. The JVM is an abstract machine that provides a runtime environment allowing Java bytecode to be executed on various platforms.

Uploaded by

Tofajul haque
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

What is Java?

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.

Types of Java Applications


There are mainly 4 types of applications that can be created using Java programming:

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:

Standard Edition Java Platform

Enterprise Edition Java Platform

Micro Edition Java Platform


The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator
(Javadoc), etc. to complete the development of a Java Application.
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.

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.

The JVM performs the following main tasks:

Loads code

Verifies code

Executes code

Provides runtime environment

What is JVM
It is:

1.A specificationwhere working of Java Virtual Machine is specified. But


implementation provider is independent to choose the algorithm. Its implementation has
been provided by Oracle and other companies.

2.An implementationIts implementation is known as JRE (Java Runtime Environment).

3.Runtime InstanceWhenever you write java command on the command prompt to


run the java class, an instance of JVM is created.

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.Extension ClassLoader: This is the child classloader of Bootstrap and parent


classloader of System classloader. It loades the jar files located
inside$JAVA_HOME/jre/lib/extdirectory.

3.System/Application ClassLoader: This is the child classloader of Extension


classloader. It loads the classfiles from classpath. By default, classpath is set to current
directory. You can change the classpath using "-cp" or "-classpath" switch. It is also
known as Application classloader.

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.

5) Program Counter Register


PC (program counter) register 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:

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.

8) Java Native Interface


Java Native Interface (JNI) is a framework which provides an interface to communicate with
another application written in another language like C, C++, Assembly etc. Java uses JNI
framework to send output to the Console or interact with OS libraries.

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.

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 an instance
variable. It is not declared asstatic.

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.

Example to understand the types of variables in java


public class A
{
static int m=100;//staticvariable
void method()
{
int n=90;//localvariable
}
public static void main(String args[])
{
int data=50;//instancevariable
}
}

Data Types in Java


Data types specify the different sizes and values that can be stored in the variable. There are
two types of data types in Java:

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.

Java Primitive Data Types


In Java language, primitive data types are the building blocks of data manipulation. These are
the most basic data types available in Java language.

There are 8 types of primitive data types:


1. boolean data type

2. byte data type

3. char data type

4. short data type

5. int data type

6. long data type

7. float data type

8. double data type

Boolean Data Type


The Boolean data type is used to store only two possible values: true and false. This data type
is used for simple flags that track true/false conditions.

The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.

Example:

Boolean one = false

Byte Data Type


The byte data type is an example of primitive data type. It isan 8-bit signed two's complement
integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and
maximum value is 127. Its default value is 0.

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:

byte a = 10, byte b= -20

Short Data Type


The short data type is a 16-bit signed two's complement integer. Its value-range lies between -
32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its
default value is 0.

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:

short s = 10000, short r = -5000

Int Data Type


The int data type is a 32-bit signed two's complement integer. Its value-range lies between -
2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is -
2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.

The int data type is generally used as a default data type for integral values unless if there is
no problem about memory.

Example:

1. int a = 100000, int b = -200000

Long Data Type


The long data type is a 64-bit two's complement integer. Its value-range lies between -
9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1) (inclusive). Its
minimum value is - 9,223,372,036,854,775,808and maximum value is
9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a
range of values more than those provided by int.

Example:

long a = 100000L, long b = -200000L

Float Data Type


The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is
unlimited. It is recommended to use a float (instead of double) if you need to save memory in
large arrays of floating point numbers. The float data type should never be used for precise
values, such as currency. Its default value is 0.0F.

Example:

float f1 = 234.5f

Double Data Type


The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is
unlimited. The double data type is generally used for decimal values just like float. The double
data type also should never be used for precise values, such as currency. Its default value is
0.0d.

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:

char letterA = 'A'

Type Casting in Java


In Java, type casting is a method or process that converts a data type into another data type
in both ways manually and automatically. The automatic conversion is done by the compiler
and manual conversion performed by the programmer.

Type casting
Convert a value from one data type to another data type is known as type casting.

Types of Type Casting


There are two types of type casting:

1. Widening Type Casting

2. Narrowing Type Casting

Widening Type Casting


Converting a lower data type into a higher one is called widening type casting. It is also
known as implicit conversion or casting down. It is done automatically. It is safe because
there is no chance to lose data. It takes place when:

1. Both data types must be compatible with each other.

2. The target type must be larger than the source type.


1. Byte -> short -> char -> int -> long -> float -> double

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

public class WideningTypeCastingExample


{
public static void main(String[] args)
{
int x = 7;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
float z = y;
System.out.println("Before conversion, int value "+x);
System.out.println("After conversion, long value "+y);
System.out.println("After conversion, float value "+z);
}
}
Output

Before conversion, the value is: 7

After conversion, the long value is: 7

After conversion, the float value is: 7.0

Narrowing Type Casting


Converting a higher data type into a lower one is called narrowing type casting. It is also
known as explicit conversion or casting up. It is done manually by the programmer. If we do
not perform casting then the compiler reports a compile-time error.

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

Before conversion: 166.66

After conversion into long type: 166

After conversion into int type: 166

You might also like