B21DA0302_Unit1
B21DA0302_Unit1
History of JAVA
Features of JAVA
INTRODUCTION TO JAVA
History of JAVA
• Java team members (also known as Green Team), initiated this project to
etc.
• James Gosling and his team members started the project in the
early '90s.
• After that, it was called Oak and was developed as a part of the
Green project.
INTRODUCTION TO JAVA
History of JAVA
□ JDK 1.0 released in(January 23, 1996). After the first release of
Java, there have been many additional features added to the
language.
FEATURES OF JAVA
□ Simple
□ Object-Oriented
□ Portable
□ Platform
independent
□ Secured
□ Robust
FEATURES OF JAVA
□ Simple
□ Object-Oriented
□ Portable
□ Platform
independent
□ Secured
□ Robust
TOPICS DISCUSSED IN PREVIOUS
CLASS
History of JAVA
Features of JAVA
TOPICS TO BE LEARNED FROM
THIS CLASS
JAVA Environment
HOW JAVA DIFFER
FROM C
□ Java does not include the C unique statement keywords sizeof, and typedef.
□ Java does not contain the data type struct and union.
□ Java does not define the type modifiers keywords auto, extern, register,
signed and
unsigned.
□ Java does not support an explicit pointer type.
□ Java does not have a preprocessor and therefore we cannot use # define, #
include, and # ifdef statements.
□ Java requires that the functions with no arguments must be declared
with empty parenthesis and not with the void keyword as done in C.
□ Java adds new operators such as instanceof and >>>.
□ Java adds labelled break and continue statements. Java adds many features
required for object-oriented programming.
HOW JAVA DIFFER
FROM C++
□ Platform-independent □ Structure and Union
□ Mainly used for
□ Thread Support
□ Design Goal
□ Goto □ Documentation comment
□ Multiple inheritance □ Virtual Keyword
□ Operator Overloading □ unsigned right shift >>>
□ Pointers
□ Inheritance Tree
□ Compiler and Interpreter
□ Hardware
□ Call by Value and Call by reference
□ Object-oriented
JAVA
ENVIRONMENT
□ An Environment variable is a dynamic "object" on a computer that stores a value,
which can be referenced by one or more software programs in Windows.
□ Like for Java, we will set an environment variable with name "java" and its value will
be the path of the /bin directory present in Java directory.
□ So whenever a program will require Java environment, it will look for the java
environment variable which will give it the path to the execution directory.
JAVA
ENVIRONMENT
1. Local Environment Setup
□ Setting Up the Path for Windows
JAVA Environment
TOPICS TO BE LEARNED FROM
THIS CLASS
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.
OVERVIEW OF JAVA
LANGUAGE
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called
enterprise application. It has advantages of the high-level security, load balancing, and
clustering.
In Java, EJB is 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.
OVERVIEW OF JAVA
LANGUAGE
Java Platforms / Editions
1) Java SE (Java Standard Edition)
It is a Java programming platform. It includes Java programming APIs such as
java.lang, java.io, java.net, java.util, java.sql, java.math etc.
It includes core topics like OOPs, String, Regex, Exception, Inner classes,
Multithreading, I/O Stream, Networking, AWT, Swing, Reflection, Collection, etc.
4) JavaFX
It is used to develop rich internet applications. It uses a light-weight user interface
API.
JAVA Program
Structure
class Simple{
args[])
}
{ System.out.println("Hello
}
ToJava");
compile: javac
Simple.java
To execute: java Simple
JAVA Program
Structure
• void is the return type of the method. It means it doesn't return any
value.
JAVA Tokens
JAVA Statements
What is
token
□ The Java compiler breaks the line of code into text
(words) is called Java tokens.
1.Keywords
2.Identifiers
3.Constants
4.Special
Symbols
Types of
token
1.Keywords:
06. case 07. catch 08. char 09. continue 10. default
Types of
token
2. Identifiers:
Types of
token
3.Constants/Literals:
Constants are also like normal variables. But the only difference is, their values
can not be modified by the program once they are defined. Constants refer to
fixed values.
separator <= ; | , | . | ( | ) | { | } | [ | ]
_
Types of
token
• Square Bracfiets []: It is used to define array elements. A pair of square
brackets represents the single-dimensional array, two pairs of square
brackets represent the two-dimensional array.
• Parentheses (): It is used to call the functions and parsing the parameters.
• Curly Braces {}: The curly braces denote the starting and ending of a code
block.
• Semicolon (;): It is the symbol that can be found at end of the statements.
It separates the two statements.
• Period (.): It separates the package name form the sub-packages and class.
_
Types of
token
5. Operators:
Java provides many types of operators which can be used according to the
need. They are classified based on the functionality they provide.
_
Java Statements
1. Declaration statement
2. Expression statement
Java Statements
1vDeclaration statement:
A declaration statement is used to declare a
variable.
For example:
int ,
num;
int num2 =
100; String
str;
For example:
_
Java Statements
/Increment and decrement expressions
num++;
++num;
num--; ,
--num;
/ Assignment expressions num = 100;
num *= 10;
/ Method invocation expressions
Systemvoutvprintln("This is a statement");
_
Java Statements
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:
∙ Standard Edition Java Platform
∙ Enterprise Edition Java Platform
∙ Micro Edition Java Platform
JDK: Java Development Kit
The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (Java), a compiler (javac), an archive (jar), a documentation generator (Javadoc) etc.
to complete the development of a Java Application.
COMPONENTS OF JDK
Following is a list of primary components of
JDK:
appletviewer: This tool is used to run and debug Java applets without a web browser.
The loader for Java applications. This tool is an interpreter and can interpret the class files generated by
java: the javac compiler. Now a single launcher is used for both development and deployment. The old
deployment launcher, jre, no longer comes with Sun JDK, and instead it has been replaced by this new
java loader.
javac: It specifies the Java compiler, which converts source code into Java bytecode.
javadoc: The documentation generator, which automatically generates documentation from source code comments
The specifies the archiver, which packages related class libraries into a single JAR file. This tool also helps
jar:
manage JAR files.
javap: the class file disassembler.
jdb: the debugger.
JAVA VIRTUAL MACHINE
□ JVM (Java Virtual Machine) is an abstract machine. It is a
specification that provides runtime environment in which java
bytecode can be executed.
□ What is JVM
□A specification
□An implementation
□Runtime Instance
JAVA VIRTUAL MACHINE
□ What it
does
□Loads code
□Verifies code
□Executes code
□Provides
runtime
environment
JRE
□The Java Runtime Environment is a set
of software tools which are used for
developing Java applications.
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 class
loaders in Java.
2.Extension Class Loader: This is the child class loader of Bootstrap and parent class loader of
System class loader. It loaders the jar files located inside $JAVA_HOME/jre/lib/ext
directory.
3.System/Application Class Loader: This is the child class loader of Extension class loader.
It loads the class files from class path. By default, class path is set to current directory.
JVM
ARCHITECTURE
2. Class Area
The area of the memory which holds the statement of currently executing method and
also static data members of classes that are loaded.
3. Heap
The area of the memory which holds the objects created during program run.
4. Stack
The area of the memory which holds local variables of the methods, which is currently
being executed.
It clears up when the execution of method ends.
JVM ARCHITECTURE
7. Execution Engine
It contains:
8. A virtual processor
9. Interpreter: Read bytecode stream then execute the instructions.
JVM
ARCHITECTURE
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. NativeMethod Interface
Java Native Interface (JNI) is a framework which provides an interface to communicate
with another application written in another language like C, C++, and Assembly etc.
Java uses JNI framework to send output to the Console or interact with OS libraries.
JAVA COMMAND LINE ARGUMENTS
class CommandLineExample{
"+args[0]);
}
TOPICS TO BE LEARNED FROM
THIS CLASS
Data types
□Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.
java: local,
instanc
static.
Variable
• local variable
• instance variable
• static variable
1) Local Variable
Class Employee {
public static void main(String args[]){
System.out.println(e1.companyId);
}
}
3) Static variable
System.out.println(e1.companyID);
}
}
Constants in
JAVA
□ Syntax:
Scope of variables
Type Casting
SCOPE OF VARIABLES Example
public class Test
□ Scope of a variable is the part {
// All variables defined directly inside
of the program where the a class
variable is accessible.
int a;
□ Java programs are organized in private String b
the form of classes. void method1()
□ Member Variables (Class {....}
int method2()
Level Scope)
□These variables must be declared inside
} {....} char c;
class (outside any function).
□They can be directly accessed anywhere in
class.
SCOPE OF VARIABLES
Modifier Package Subclass World
□ We can declare class
variables anywhere in public Yes Yes Yes
}
}
TYPE CASTING
Narrowing or Explicit Conversion
class Test
{
□ If we want to assign a value of larger data public static void main(String[]
type to a smaller data type we perform args)
{
explicit type casting or narrowing double d = 100.04;
□ This is useful for incompatible data types where //explicit type
automatic conversion cannot be done. casting long l =
(long)d;
□ Here, target-type specifies the desired type System.out.println("Double value
to convert the specified value to. "+d);
}
}
TOPICS TO BE LEARNED FROM
THIS CLASS
Operators in JAVA
OPERATORS IN JAVA
times. *8=80
System.out.println(20<<2);//20*2^2=20
*4=80
System.out.println(15<<4);//
JAVA SHIFT OPERATOR EXAMPLE: >> VS
>>>
class OperatorExample{
public static void main(String args[]){
//For positive number, >> and >>>
works same
System.out.println(20>>2);
System.out.println(20>>>2);
//For negative number, >>> changes parity bit
(MSB) to 0 System.out.println(-20>>2);
System.out.println(-20>>>2);
}}
LOGICAL && AND BITWISE &
class OperatorExample{
□ The logical && operator
public static void main(String
doesn't check second
args[]){ int a=10;
condition if first
int
condition is false.
b=5;
□ It checks second int
condition only if first c=20;
one is true. System.out.println(a<b&&a<c);//false &&
true = false
□ The bitwise & System.out.println(a<b&a<c);//false &
true = fa lse
operator always
}}
checks both
conditions whether
LOGICAL || AND
BITWISE | class OperatorExample{
□ The logical || operator public static void main(String
doesn't check second args[]){ int a=10;
condition if first int
class OperatorExample{
□ Java Ternary operator is
public static void main(String
used as one liner
replacement for args[]){ int a=10;
if-then-else statement and int b=5;
used a lot in Java int min=(a<b)?a:b;
programming. System.out.println(m
in);
□ it is the only conditional
}}
operator which takes three
operands.
JAVA ASSIGNMENT
OPERATOR
□ Java assignment class OperatorExample{
public static void main(String[]
operator is one of the args){ int a=10;
a+=3;//10+3
most common operator.
System.out.println(
a); a-=4;//13-4
□ It is used to assign the System.out.println(
value on its right to the a); a*=2;//9*2
System.out.println(
operand on its left. a); a/=2;//18/2
System.out.println(
a);
}}
TOPICS TO BE LEARNED FROM
THIS CLASS
Expressions
EXPRESSIO
NS
□ We have understood variables and operators,
statements.
class example
□ An assignment statement or {
public static void main ( String[] args )
expression changes the value
{
that is held in a variable. long x ; //a declaration without an
□ Here is a program that uses an initial value
x = 123; //an assignment statement
assignment statement:
System.out.println("The variable
x contains: " + x );
}
}
ARITHMETIC EXPRESSIONS
□ Java Arithmetic expressions use int x, y, z; // Three integer variables declared at the same time.
arithmetic operators such as +, -, /, x = 10;
*, and %. y = 12;
z = y / x; // z is assigned the value of y divided by x.
□ The % operator is the remainder // Here z will have value 1.
or modulo operator. z = x + y; // z is assigned the value of x+y
□assign
Arithmetic expressions
arithmetic arevariables.
values to used to Here z will have value 22.
z = y % x // z is assigned the value of remainder when y
□ An expression is a
combination of literals,
operators, variables, and
parentheses used to calculate a
value.
TOPICS TO BE LEARNED FROM
THIS CLASS
Mathematical Functions
JAVA OPERATOR ASSOCIATIVITY
Operator Type Category Precedence Associativity
postfix expr++ expr-- Left to Right
Unary
prefix ++expr --expr +expr -expr ~ ! Left to Right
multiplicative */% Left to Right
Arithmetic
additive +- Left to Right
Shift shift << >> >>> Left to Right
comparison < > <= >= instanceof Left to Right
Relational
equality == != Left to Right
bitwise AND & Left to Right
Bitwise bitwise exclusive OR ^ Left to Right
bitwise inclusive OR | Left to Right
logical AND && Left to Right
Logical
logical OR || Left to Right
Ternary ternary ?: Left to Right
= += -= *= /= %= &= ^= |= <<=
Assignment assignment >>= >>>= Left to Right
Java Math
class
Decision Making
Branching
DECISION MAKING
□ Decision making structures
have one
or more conditions to be
evaluated or tested by the
program, along with a statement
or statements that are to be
executed if the condition is
determined to be true, and
optionally, other statements to
Decision
Making
Decision
Making
Java if
Statement
□ The Java if statement is used to test the condition.
It checks boolean condition: true or false. There
are various types of if statement in Java.
if statement
if-else statement
if-else-if ladder
nested if statement
□ The Java if statement tests the condition. It executes the
if block if condition is true.
Syntax:
if(condition){
//code to be executed
}
Example Program
Java if-else
Statement
□ The Java if-else statement also tests
the condition. It executes the if block if
condition is true otherwise else block
is executed.
Syntax:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
Example Program
Using Ternary
Operator
□ We can also use ternary operator (? :) to public class IfElseTernaryExample
perform the task of if...else statement. It { public static void main(String[]
is a shorthand way to check the args) {
condition. If the condition is true, the
result of ? is returned. But, if the int number=13;
condition is false, the result of : is //Using ternary operator
returned. String output=(number
%2==0)?"eve n number":"odd
number";
System.out.println(output);
}
}
Java if-else-if ladder
Statement
□ The if-else-if ladder statement executes one n
conditio from multiple statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Example Program
Java Nested if
statement
□ The nested if statement represents the
if block within another if block. Here,
the inner if block condition executes
only when outer if block condition is
true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
□ Example Program