AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Introduction to JAVA
Programming
CSIT751 (PG)
(CORE JAVA MODULE I)
1
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Topics Covered:
Java Introduction
JDK, JRE and JVM
Features of Java
Java program structure
Identifiers, keywords and variables
Data Types
Control structures and loops
Arrays
Command line arguments
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Introduction to Java
• Object oriented programming language.
• Although Java is not pure object-oriented programming but
almost everything is object in Java except its primitive
datatypes
• It supports all object-oriented programming features:
– Class and Object
– Encapsulation
– Abstraction
– Inheritance
– Polymorphism etc.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
History of Java Click here to get latest file
Version Date Java SE 9 September 21, 2017
JDK Beta 1995 Java SE 10 March 20, 2018
JDK 1.0 January 23, 1996 Java SE 11 (LTS) September 25, 2018
JDK 1.1 February 19, 1997 Java SE 12 March 19, 2019
J2SE 1.2 December 8, 1998 Java SE 13 September 17, 2019
J2SE 1.3 May 8, 2000 Java SE 14 March 17, 2020
J2SE 1.4 February 6, 2002 Java SE 15 September 15, 2020
September 30, Java SE 16 March 16, 2021
J2SE 5.0
2004
Java SE 17 (LTS) September 14, 2021
December 11,
Java SE 6 Java SE 18 March 22, 2022
2006
Java SE 7 July 28, 2011 Java SE 19 September 20, 2022
Java SE 8 (LTS) March 18, 2014 Java SE 20 March 21, 2023
Java SE 21 (LTS) September 19, 2023
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
JDK, JRE and JVM
JDK: Java Development Kit is a software development
environment
- used for developing Java applications and applets.
JRE: JRE stands for Java Runtime Environment
- It provides an environment to run only the Java program
onto the system.
JVM: JVM stands for Java Virtual Machine
- It is responsible for executing the Java program.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
JDK, JRE and JVM
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
JDK is Java Development Kit. The Java
Development Kit (JDK) is a software
development environment which is used to
develop Java applications.
JRE (Java Runtime Environment) is an
installation package that provides an
environment to only run(not develop) the java
program (or application) onto your machine. JRE
is only used by those who only want to run Java
programs.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
JVM (Java Virtual Machine) is a very
important part of both JDK and JRE because it is
contained or inbuilt in both. Whatever Java
program you run using JRE or JDK goes into
JVM and JVM is responsible for executing the
java program line by line, hence it is also known
as an interpreter.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Features of Java Language
1. Easy to develop applications
2. Platform Independent
3. Portable
4. Multithreaded
5. Distributed
6. Object Oriented
7. Community Support
8. Scalability
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
1. Easy to develop applications
As compared to C and C++ , no pointer concept
Atomatic memory allocation and deallocation which
makes easy to develop applications.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
2. Platform Independent
Java's platform independence is a key feature is achieved through the
concept of "Write Once, Run Anywhere" (WORA).
Java source code is compiled into an intermediate form known as bytecode.
Once compiled to bytecode, Java programs can be executed on any device
that has a JVM installed.
Developers can write code on one platform (e.g., Windows) and run it on
another platform (e.g., Linux) without modification.
Java programs are executed by the Java Virtual Machine (JVM).
JVM is platform-specific, but the bytecode is interpreted by the JVM, making
the program platform-independent.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
3. Portable
Java programs can be easily moved from one computer
system to another, providing portability and flexibility.
Platform independent and architectural neutral features
makes its portable.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
4. Multithreaded
You can perform multiple operations at the same time.
Threads are independent, so it doesn't affect other
threads.
Typing MS Word document while
listening to music.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
5. Distributed
Java includes features for distributed computing, making it suitable for building
networked applications.
Same application can run in different different server.
Advantage: problem occurred in one server will never be reflected on any client
system.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
6. Object Oriented
Java is object-oriented, promoting the use of classes and
objects in programming. Everything in Java is treated
as an object.
7. Community Support AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Java has a large and active community of developers, providing extensive
resources, libraries, and frameworks.
Numerous online forums and communities provide platforms for Java
developers to seek help, share their expertise, and discuss various topics
related to Java programming. Examples include Stack Overflow, Reddit
(r/java), and the Oracle Community forums.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
8. Scalability
Java applications can scale easily, making them
suitable for both small-scale and large-scale
projects.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Java program structure
Basic structure of Java Program:
• [ Documentation ]
• [package declarations]
• [import statements]
• [class declaration]
• [Main Method]
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Basic structure of Java Program:
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Writing simple Java class and main() method
Simple Java Cla Java Main Meth
ss od
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Identifiers
• A name which is used for identifying any entity.
• Identifier can be a class name, object name, method name,
variable name, property name etc..
• Identifier name should be meaningful not meaningless and
always it follow naming convention rule.
• Now a days industry are following camel case/ Pascal Case
style to define identifier name.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Identifiers Example
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
keywords
Reserve words in java is called java keywords. As
we know keywords are not allow to use for any
identifiers name.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
variables
A name of storage location is called variable. Variable can be
defined their own scope. So, we can categorize the variable
with the help of its type and its scope.
According to types:
syntax:
varibaleType variableName;
eg: int registrationNumber;
According to scope:
it can be local, instance and static
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Primitive Data Types
Data types in Java: Data types means
type of your data or user input. To define
our data which will be use in our
application we need to define with its type
so, that our compiler can understand what
operation or task we want to perform.
In java we can categorize data types in
two ways : one is primitive and another
one is non primitive
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Primitive Data Types
Numeric Types: byte myByte = 127;
Integral Types: short myShort = 32000;
int myInt = 2000000000;
byte: 8-bit signed integer long myLong = 9223372036854775807L;
short: 16-bit signed integer float myFloat = 3.14f;
int: 32-bit signed integer double myDouble = 2.71828;
char myChar = 'A';
long: 64-bit signed integer
Floating-Point Types:
float: 32-bit floating-point number
double: 64-bit floating-point number
Character Type:
char: 16-bit Unicode character
Boolean Type:
boolean: Represents true or false values
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Non-Primitive Data Types
Reference Data Types:
Object Types:
String: Represents a sequence of characters.
Arrays: Collection of elements of the same type.
Custom Objects: User-defined classes and interfaces.
// String is a reference data type
String name = "Kumar";
// Arrays are reference data types
int[] numbers = new int[] { 1, 2, 3, 4 };
// Custom classes are reference data types
class Person {
String name;
int age;
}
Person personObj = new Person();
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Program to get Size of Data Types
Click here to get Code
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Introduction to Java
Operators
Operators in Java are special symbols or
keywords that are used to perform operations
on operands. Operands can be variables,
literals, or expressions. Here are some
common types of operators in Java:
Arithmetic operators
Assignment operator
Relational operators
Logical operators
Bitwise operators
Unary operators
Ternary operators
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Arithmetic Operators
– Scenario-Based Examples
• Calculating the total cost
of items in a shopping car
t
• Computing the average of
exam scores
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Assignment Operators
Scenario-Based Examples
• Accumulating points in a game with +
=
• Adjusting inventory levels with -= and
+=
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Relational Operators
Scenario-Based Examples
• Checking if a user-provided pass
word matches the stored passwor
d
• Determining eligibility based on a
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Logical Operators
Scenario-Based Examples
• Implementing access
control with logical AN
D
• Validating multiple co
nditions using logical
OR
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Bitwise Operators
Scenario-Based Examples
Let's consider a scenario
where you have a set of u
ser roles, and you want to
use bitwise operations to
manage and check these
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Unary Operators
Scenario-Based Examples:
A model for the progress of a
task
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Ternary Operators
Scenario-Based Example:
Grading System
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Operator precedence
Operator precedence in Java refers to the order in which operators are evaluated when an
expression contains multiple operators. Operators with higher precedence are evaluated
first. If two operators have the same precedence, their associativity determines the order of
evaluation (left-to-right or right-to-left).
Precedence Operators Associativity 8 ==, != Left-to-right
1 () Left-to-right
9 & Left-to-right
2 ++, -- (postfix) Left-to-right
10 ^ Left-to-right
+, - (unary) Right-to-left
~, ! Right-to-left 11 ` `
3 ++, -- (prefix) Right-to-left 12 && Left-to-right
+, - (binary) Left-to-right
13 `
4 *, /, % Left-to-right
5 +, - (binary) Left-to-right 14 ?: Right-to-left
6 <<, >>, >>> Left-to-right =, +=, -=, *=,
15 Right-to-left
<, <=, >, >=, /=, %=
7 Left-to-right
instanceof
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Introduction to Control Statements
Definition of control Importance of control Overview of if/else and
statements: statements in switch-case constructs
programming
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
CONTROL STATEMENTS:
Control or conditional statements, also known as
decision-making statements, are fundamental constructs in
programming that allow the execution of certain code
blocks based on specified conditions.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
IMPORTANCE OF CONTROL STATEMENTS IN
PROGRAMMING
Decision-making: enable programs to make
decisions based on specific conditions.
C o n t ro l f l o w : d e t e r m i n e t h e f l o w o f e x e c u t i o n
within a program.
Flexibility: allow programs to react
d y n a m i c a l l y t o d i ff e r e n t i n p u t s o r s c e n a r i o s .
User interaction: enable programs to interact
with users and respond to user input or events.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
The if Statement
The if statement is a fundamental
control flow statement in Java used
for decision-making
It allows a program to execute
a certain block of code only if a
specified condition is true
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
if (condition)
Syntax {
// code block to be executed if the condition is true
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example
• int number = 10;
• if (number > 0) {
• [Link]("The number is
positive.");
• }
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
The if-else Statement
The if-else statement
It allows a program to
extends the functionality of
choose between two
the if statement by
different courses of action
providing an alternative
based on the result of a
block of code to execute
condition
when the condition is false
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
if (condition) {
Syntax // code block to be executed if the condition is true
} else {
// code block to be executed if the condition is false
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example
• int number = 7;
• if (number % 2 == 0) {
• [Link]("The number is
even.");
• } else {
• [Link]("The number is
odd.");
• }
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Nested if-else Statement
Nested if-else statements are if-else statements that are
nested within other if-else statements
They allow for more complex decision-making by
evaluating multiple conditions in a hierarchical manner
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
if (condition1) {
// code block to be executed if condition1 is true
if (condition2) {
// code block to be executed if both condition1 and
Syntax condition2 are true
} else {
// code block to be executed if condition1 is true but
condition2 is false
}
} else {
// code block to be executed if condition1 is false
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example
Clic
k He Consider a scenario where you want to
re determine the eligibility of a candidate for
a job based on their qualifications and
years of experience
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Multiple if-else Statement
Multiple if-else They allow for handling
statements are a series multiple conditions
of if-else statements independently, with
that are executed each condition being
sequentially evaluated in order
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
if (condition1) {
// code block to be executed if condition1 is true
}
Syntax if (condition2) {
// code block to be executed if condition2 is true
}
// Additional if-else statements as needed
else {
// code block to be executed if none of the conditions
are true
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example
int score = 85;
if (score >= 90) {
[Link]("Grade: A");
}
if (score >= 80 && score < 90) {
[Link]("Grade: B");
}
if (score >= 70 && score < 80) {
[Link]("Grade: C");
}
if (score < 70) {
[Link]("Grade: D");
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
The if-else-if Ladder
The if-else-if ladder is a
It provides a more
series of if-else
structured approach to
statements chained
decision-making
together, allowing for
compared to multiple if-
multiple conditions to be
else statements
evaluated sequentially
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
if (condition1) {
// code block to be executed if condition1 is true
} else if (condition2) {
// code block to be executed if condition2 is true and
Syntax condition1 is false
} else if (condition3) {
// code block to be executed if condition3 is true and
conditions 1 and 2 are false
}
// Additional else-if statements as needed
else {
// code block to be executed if none of the conditions
are true
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example
int month = 9;
if (month == 1) {
[Link]("January");
} else if (month == 2) {
[Link]("February");
} else if (month == 3) {
[Link]("March");
}
// Additional else-if statements for the remaining
months
else {
[Link]("Invalid month");
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
switch-case
Switch-caseStatements
statement
A switch-case statement is a control flow construct in
programming that allows the program to evaluate a variable or
expression against a list of possible values and execute
specific code blocks based on the matched value.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
switch (expression) {
case value1:
// code block to execute if expression matches
value1
Syntax break;
case value2:
// code block to execute if expression matches
value2
break;
// additional case labels as needed
default:
// code block to execute if expression does not match any case
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example
Clic
Consider a scenario where you want to
k
determine week days
Her
e
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Enhanced switch-case feature introduced in Java 12.
Enhanced switch-case simplifies the syntax and allows
for more expressive code.
Syntax switch (expression) {
case value1 -> expression1;
case value2 -> expression2;
// ...
default -> defaultExpression;
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example
int dayOfWeek = 2;
String dayName = switch (dayOfWeek) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6, 7 -> "Weekend";
default -> "Invalid day";
};
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Looping Controls in Java
• While loop
• Do while loop
• For loop
• Enhanced or advanced for loop
• Nested loops means one loop inside
another loop.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
while loop:
A while loop is a control flow statement that allows code to be
executed repeatedly based on a given Boolean condition. The
while loop can be thought of as a repeating if statement.
Syntax :
while (boolean condition)
{
loop statements...
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
While Loop Example
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
for loop:
for loop provides a concise way of writing the loop structure. Unlike a
while loop, a for statement consumes the initialization, condition and
increment/decrement in one line thereby providing a shorter, easy to
debug structure of looping.
Syntax:
for (initialization condition; testing condition;increment/decrement)
{
statement(s)
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
For Loop Example
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
do while:
do while loop is similar to while loop with only difference that it
checks for condition after executing the statements, and therefore
is an example of Exit Control Loop.
Syntax:
do
{
statements..
}
while (condition);
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Do while Loop Example
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
When to use which loop?
While loop:
Use when you don’t know in advance how many times the
loop will run, but the condition must be checked before each
iteration.
Example click here
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
When to use which loop?
Do While loop:
Use when you must execute the body at least once,
regardless of the condition. Condition is checked after the
loop body.
Example click here
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
When to use which loop?
For loop:
Use when you know exactly how many times you want to
run the loop (fixed iterations).
Example click here
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Enhanced For loop
for(data_type variable : array | collection)
{
//body of for-each loop
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Enhanced For loop Example:
int myArray[]=new int[]{11,12,13,14,15};
for(int num : myArray)
{
[Link](num);
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example: Sum of array elements
class Main {
public static void main(String[] args) {
// an array of numbers
int[] numbers = {3, 4, 5, -5, 0, 12};
int sum = 0;
// iterating through each element of the array
for (int number: numbers) {
sum += number;
}
[Link]("Sum = " + sum);
}
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Nested loops
A Nested loop means a loop statement inside another loop
statement. That is why nested loops are also called “loop
inside loop“.
Nested For loop
for ( initialization; condition; increment ) {
for ( initialization; condition; increment ) {
// statement of inside loop
}
// statement of outer loop
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Java Arrays
• Array is a collection of similar type of elements
(Homogeneous)
• Array have contiguous memory location.
• In java, array is an object the contains elements of similar
data type.
• We can store only fixed elements in an array (Fixed
Size).
• Array is index-based, first element of the array is stored
at 0 index.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Advantage of Array
Code Optimization: It makes the code-optimized, we can retrieve or sort
the data easily.
Random access: We can get any data located at any index position.
Disadvantage of Array
Size Limit: We can store only fixed size of elements in the array.
It doesn't grow its size at runtime. 76
To solve this problem; collection framework is used in java.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Types of Array: There are two types of array
• Single Dimensional Array
• 1D array
• Multidimensional Array
• 2D array
• 3D array etc.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Single Dimensional Array
The syntax for declaring and instantiating an array:
There are two ways to declare an array:
type[] arrayName;
type arrayName[];
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
How to instantiate an array
arrayName = new type[length];
How to declare and instantiate an array in one statement
type[] arrayName = new type[length];
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Array length
The syntax for getting the length of an array
[Link]
Output: Length of Array is: 10
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Two-dimensional arrays
The syntax for creating a rectangular array-
type[][] arrayName = new type[rowCount][columnCount];
• A statement that creates a 3x2 array
int[][] numbers = new int[3][2];
• 3x2 array and initializes it in one statement
int[][] numbers =new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
Two-dimensional arrays
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Enhanced for loop for 2D array
for (int[] num: arr)
{
for(int data: num)
{
[Link](data);
}
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Command Line Arguments
The java command-line argument is an argument i.e.
passed at the time of running the java program.
• The arguments passed from the console can be received
in the java program and it can be used as an input.
• You can pass N(1,2,3 and so on) numbers of arguments
from the command prompt.
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example CLA
Prog to get sum of arguments passed from the CLA
Class CLine1
{
public static void main (String args[])
{
int a = [Link](args[0]);
int b = [Link](args[1]);
int c = a + b;
System .out. println(“ sum of the ”+ a + “ and ” + b+ “ is :”+ c);
}
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
How to run
• Compile this prog as
– javac [Link]
• Run this prog as
– java CLine1 20 40
Output : 60
AMITY INSTITUTE OF INFORMATION TECHNOLOGY
Example CLA
Prog to print all arguments passed from the CLA
Class CLine2
{
public static void main (String args[])
{
for(int i=0; i<[Link]; i++)
{
[Link](args[i]);
}
}
}
AMITY INSTITUTE OF INFORMATION TECHNOLOGY