Unit1
Unit1
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 by Sun Microsystems (which is now the subsidiary of Oracle) in the
year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak.
Since Oak was already a registered company, so James Gosling and his team changed the
name from Oak to Java.
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 each OS is different from each other.
However, Java is platform independent. There are three notions of the
JVM: specification, implementation, and instance.
o Loads code
o Verifies code
o Executes code
o 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 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 and applets. 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:
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.
Java Class Libraries
Java class libraries, also known as Java APIs (Application Programming Interfaces), are pre-built
sets of classes and interfaces that provide a wide range of functionality for developing Java
applications. These libraries are organized into packages, each containing related classes and
interfaces.
Here are some important and commonly used Java class libraries:
1.Java Standard Library (Java SE):
java.lang: Fundamental classes and basic language features.
java.util: Utility classes for data structures and collections (e.g., lists, maps, queues).etc
2. Java Enterprise Edition (Java EE):
javax.servlet: Classes for creating web applications using Servlets.
javax.ejb: Enterprise JavaBeans for building enterprise-level applications.
3. JavaFX:
javafx.scene: Classes for creating rich graphical user interfaces (GUIs).
4.Apache Commons: A collection of reusable Java components and libraries that extend the
functionality provided by the Java Standard Library. Examples include Apache Commons
Collections, Apache Commons Lang, and Apache Commons IO.
5.Spring Framework: A popular open-source framework that provides a comprehensive
programming and configuration model for modern Java-based enterprise applications.
6.Hibernate: An object-relational mapping (ORM) library that simplifies database interaction in
Java applications.
7.JUnit: A widely used testing framework for Java that simplifies the process of writing and
running unit tests.
8.Log4j: A logging utility for Java that provides flexible logging within applications.
9.Jackson: A library for working with JSON data, providing JSON parsing and serialization
capabilities
Creating Hello World Example
Let's create the hello java program:
class Simple{
public static void main(String args[]){
System.out.println("Hello 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.
Variable is a name of memory location. There are three types of variables in java: local,
instance and static.
Variable
A variable is the name of a reserved area allocated in memory. In other words, it is a name
of the memory location. It is a combination of "vary + able" which means its value can be
changed.
Types of Variables
There are three types of variables in Java:
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
Instance variable: A variable declared inside the class but outside the body of the method,
is called an instance variable. It is not declared as static. It is called an instance variable
because its value is instance-specific and is not shared among instances.
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.
Java Arrays
Normally, an array is a collection of similar type of elements which has contiguous
memory location.
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
Typecasting
Typecasting in Java is the process of converting a value from one data type to another. There are
two types of typecasting: implicit (automatic) and explicit (manual).
1.Implicit Typecasting (Widening Conversion):
- This occurs when the target data type can hold all possible values of the source data type.
- Java automatically performs this type of conversion without the need for an explicit cast.
For example: int x = 10;
double y = x; // Implicit casting from int to double
2. Explicit Typecasting (Narrowing Conversion):
- This is when you convert a value from a larger data type to a smaller data type.
- This type of conversion requires an explicit cast to avoid potential loss of information.
Syntax: (targetDataType) value
For example: double a = 10.5;
int b = (int) a; // Explicit casting from double to int
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator
Decision-Making statements:
1) If Statement:
In Java, there are four types of if-statements given below:
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
1) Simple if statement:
if(condition) {
statement 1; //executes when condition is true
}
2) if-else statement
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
3) if-else-if ladder:
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
4. Nested if-statement
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
Switch Statement:
switch (expression){
case value1:
statement1;
break;
. . .
case valueN:
statementN;
break;
default:
default statement;
}
Loop Statements
Jump Statements
Jump statements are used to transfer the control of the program to the specific statements. In
other words, jump statements transfer the execution control to the other part of the program.
There are two types of jump statements in Java, i.e., break and continue.
The break statement cannot be used independently in the Java program, i.e., it can only
be written inside the loop or switch statement.
Consider the following example to understand the functioning of the continue statement
in Java.