0% found this document useful (0 votes)
2 views104 pages

Introduction to Java Programming Basics

Introduction to Java

Uploaded by

Kabiraj Shrestha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views104 pages

Introduction to Java Programming Basics

Introduction to Java

Uploaded by

Kabiraj Shrestha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Introduction to Java

Objectives
Introduction to Java

Data Handling and Functions

Object Oriented Programming in Java

Packages and Multithreading

Collections

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Objectives
After completing this module, you should be able to:
 Understand the advantages of Java

 Understand where Java is used

 Understand how Memory Management is handled in Java

 Create a Java project in Eclipse and execute it

 Implement if - else construct in Java

 Develop Codes using various Data Types in Java

 Implement various Loops

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Introduction to Java

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Meet John
John is a student of Computer Science in California University.

John dreams of becoming a “Programmer” like his elder brother.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John Learns about Programming Languages
 One day in a class!!

There are many


programming
languages. Java is
one of them. It’s
very important, we ?!!
will learn about it
now.
!

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John is Confused!

Programming Language
Java Sir, if there are
many
programming
languages, why
learn only Java?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John gets a Bookish Answer

Programming Language
Because Java is
highly secured,
Java
object oriented and
most widely used
language in the
Industry.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John is still Confused!

Programming Language
But where
Java do we use
it? Why
should I
learn Java?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John Heads Home with a Doubt
John was not able to relate anything on what was taught in the class

Programming Language
Java

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John meets his Brother
On returning home, John met his elder brother. John’s brother realized that John was upset.

Hello John! How


was your day?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John Expresses his Concerns

My teacher started
programming
languages today. He
started with Java and
said it’s very useful
and important. I am
not able to
understand why?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John gets an Answer!

Well.. He is
right. Java is
very important.
It’s used
everywhere.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John wants to know more..

Can you tell me


where it is used?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John gets Help!

Yes. Sure. Let


me take you to
the places
where Java is
used.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Uses of Java - 1

Look there. Do
you see the
set-top box
near the TV? It
works on
Java..

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Uses of Java - 2
Our car uses GPS
system to guide
us and it’s a very
useful navigation
tool. It works on
Java.
Lets see some
more places.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Uses of Java - 3
John’s brother took him to the groceries shop.

The billing
machine
also works on
Java.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John is Convinced to Learn Java
While returning, John had understood that Java is almost everywhere.

So John, now that you


know, Java is used in so
many fields, you
should start learning it
if you wish to be a
good programmer.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


A Gift for John

Look John. I
have bought
you a gift.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


An Android Phone

Wow! An
android
phone!!
Thank you

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


John is all set to Learn Java

Well. That works


on Java too..

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


What is Java?
Java is a computer programming language that is -

Object-Oriented
.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


What is Java?
Java is a computer programming language that is -
Class-based

Object-Oriented

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


What is Java?
Java is a computer programming language that is -
Class-based
Concurrent

Object-Oriented
.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


What is Java?
Java is a computer programming language that is -

Concurrent Class-based

Low
Object-Oriented
Dependencies

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Where is Java Used?
3 billion devices use Java Runtime Environment

To develop Android Applications

To develop Hadoop Applications

To develop business process management (BPM)


tools

To develop web servers

To develop application servers etc.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Where is Java Used?
Java has been used to develop below frameworks:

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Where is Java Used?
Java has been used to develop below frameworks:

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Where is Java Used?
Java has been used to develop below frameworks:

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Where is Java Used?
Java has been used to develop below frameworks:

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Java-Job, Demand and Experience Trends

[Link]

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Languages before Java
Before Java emerged as a programming language, there were many other programming languages:

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Why not C++?
C++ does not provide efficient means for
garbage
collection

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Why not C++?
C++ does not provide efficient means for
garbage
collection

No built in support for threads

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Why not C++?
C++ does not provide efficient means for
garbage
collection

No built in support for threads

It gets complex when u want to develop a


graphics rich application

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Why not C++?
C++ does not provide efficient means for
garbage
collection

No built in support for threads

It gets complex when u want to develop a


graphics rich application

C++ is not platform independent

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Why not C++?
C++ does not provide efficient means for
garbage
collection

No built in support for threads

It gets complex when u want to develop a


graphics rich application

C++ is not platform independent

In C++ memory allocation and de-allocation has to be done by the


developer

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


History of Java

 Java was developed by James Gosling in Sun


Microsystems

 It is a platform independent programming


language
 This language was initially named as OAK and later
renamed as Java

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Java Versions

Java 1.0 Java 1.2 Java 1.4 Java 6


Playground Mustan Java 8
Oak Merlin
g

February 1997 May 2000 September 2004 July 2011 Sep 2017

January 1996 December 1998 February 2002 December 2006 March 2014

Java1. Java 5 Java 7 Java 9


Java 1.1 Dolphi
3 Tiger Dolphi
Kestrel n n

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

{OOPS
}
Simple Portable Object-oriented Secure

Distributed Dynamic Robust High Performance

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Simple
Portable

Object-oriented
Secure  Java was designed to be easy for professional
Distributed programmer to learn and use effectively

Dynamic Simple

Robust

High
Performan
ce

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Simple

Portable
Object-oriented  Applications written using Java are portable
Secure in the sense that they can be executed on
Distribute any kind of computer containing any CPU or

d Dynamic any operating system


Portable

Robust

High
Performan
ce

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Simple

Portable

Object-oriented  Java is true object oriented language. {OOPS


Secure  Everything is considered to be an “object” }
Distributed and all operations are performed using these
Object-oriented
objects
Dynami

Robust

High
Perform
ance Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.
Features of Java

Simple

Portable

Object-oriented  Java Programs run inside virtual machine

Secure sandbox to prevent any activity from


untrusted sources.
Distributed
 No use of explicit pointer Secure
Dynamic

Robust

High
Performan
ce

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Simple

Portable

Object-oriented  Java has a feature called Remote Method


Secure Invocation (RMI) using which a program can

Distributed invoke method of another program across


a network and get the output Distributed
Dynamic

Robust

High
Performance

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Simple

Portable

Object-oriented  Java programs carry with them substantial


Secure amounts of run-time type information that is
Distributed used to verify and resolve accesses to objects
at run time Dynamic
Dynamic
Robust

High
Performance

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Simple

Portable
 Java checks the code during the compilation
Object-oriented
time and run time also
Secure
 Java completely takes care of memory
Distributed
allocation and releasing, which makes the
Robust
Dynamic
Java program more robust
Robust

High
Performance

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Features of Java

Simple

Portable

Object-oriented
 Java achieves high performance through the
Secure
use of bytecode which can be easily
Distributed
translated into native machine code
High Performance
Dynami

Robust

High
Performanc
e Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.
C++: Platform Dependent

Source Code Compiler [Link]


Window
s

Source Code Compiler [Link]

Source Code Compiler [Link]


Ma
c

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Java: Platform Independent

Window
s

Java Code (.Java) JavaC Byte Code (.class) JVM


Compiler

Ma
c

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


In- Class Questions Q
1. What is bytecode?

2. What is the advantage of executing parallel threads/tasks at a time?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


A
In Class Question - Solutions
1. What is bytecode?

Solution: Bytecode is an intermediate code which gets generated when a Java file is compiled using a Javac
compiler. After compilation .class file is generated which contains the byte code. This code is platform
independent.

2. What is the advantage of executing parallel threads/tasks at a time?

Solution: When many tasks/threads run at the same time, performance of the system increases.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Bytecode and Java Virtual Machine (JVM)
 Java bytecode is the form of instructions that the JVM Development
Tools
executes

 A Java programmer need not understand bytecode at


all

Java Virtual Machine (JVM) Java Class


Libraries
 Runs the bytecode JVM
 Makes Java platform independent

 Handles memory management


Other Files

JR
JDK
E
Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.
Java Memory Management
 C/C++ has pointers. User can allocate memory to

these pointers

 If the pointer is accessed without allocating memory

or invalid pointer is accessed then program crashes

 These issues are removed from Java as Java does

not have pointers. Complete memory management

is handled by Java itself

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Heap and Garbage Collector
The memory area in JVM
where objects are created
is called Heap

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Heap and Garbage Collector
The memory area in JVM
where objects are created
is called Heap

Heap is divided into


two parts. Young
space and old space

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Heap and Garbage Collector
The memory area in JVM
where objects are created
is called Heap

Heap is divided into


two parts. Young
space and old space

The memory is freed during


runtime by a special thread
called Garbage Collector

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Heap and Garbage Collector
The memory area in JVM
where objects are created
is called Heap

Heap is divided into


two parts. Young
space and old space

The memory is freed during


runtime by a special thread
called Garbage Collector

The
Garbage
Collector
looks for© 2020, Dursikshya and/or its affiliates. All rights reserved.
Copyright
Heap and Garbage Collector
The memory area in JVM
where objects are created
is called Heap

Heap is divided into


two parts. Young
space and old space

All the newly allocated objects are


created in young space. Once the
young space is full then garbage The memory is freed during
collector is called so that memory runtime by a special thread
can be released called Garbage Collector

The Garbage Collector looks for objects which are no


longer needed by the program and destroys them

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Heap and Garbage Collector
The memory area in JVM
where objects are created
is called Heap

If the object has lived for long in Heap is divided into


young space then they will be two parts. Young
moved to old space. Once the old space and old space
space is full, garbage collector is
called to release the space in heap

All the newly allocated objects are


created in young space. Once the
young space is full then garbage The memory is freed during
collector is called so that memory runtime by a special thread
can be released called Garbage Collector

The Garbage Collector looks for objects which are no


longer needed by the program and destroys them

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


How Java work?

Java C Compiler
Java Source Java Bytecode
(.java)

JVM

Java
Java is easy Interpret
er

JIT
Compiler
Object Code

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


How Java Works?
Compile-time Environment Run-time Environment
Class
Loader

Java Source (.java) Bytecode Java Class


Verifier Libraries

Java Compiler Java Bytecodes move Java


locally or through Java Just in Time Virtual
network Interpreter Compiler Machine

Runtime System

Operating System

Java Bytecode (.class)

Hardware

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


JAVA Installation

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Java Installation
Go to
1 [Link]
technetwork/java/javas
e/downloads/[Link]
l

2 Download Java Platform(JDK)


version 8 update 121 and install
on your Windows system

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Java Installation
To set the permanent path of JDK in your
system:
1. Begin by going to My Computer 1
Properties.
2
2. Click on Advanced system setting.
3. Click on Environment Variables
4
option

4. Select Path variable to edit


5
5. Enter the path to bin folder
inside JDK installed on your
3
system. path is C:\Program
6. (Default
Files\Java\jdk1.8.0_121\bin )

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Data Types
Primitive Data Type

Integer Float Character Boolean

double float char boolean

short long byte int

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Data Operations
Data Operations

Arithmetic Operators Unary Operators Relational Operators Logical Operators

+  Addition <  Less than &  And


++  Increment <=  Less than or &
equal to ||  Or
-  Subtraction operator >  Greater than
>=  Greater than !  Not
*  --  Decrement or equal to
Multiplication !=  Not equal to
operator ==  Double Equals
/  Division

%  Modulus
Q
In Class Question
1. Why do you think we need byte, short and long datatypes when int can be used?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


A
In Class Question - Solution
1. Why do you think we need byte, short and long datatypes when int can be used?

Solution: ‘byte’ takes 1 byte, ‘short’ takes 2 bytes and ‘int’ takes 4 bytes in memory. If we know that the variable
need not be very big then ‘short’ is fine as it reduces the memory consumption which in turn increases the
performance of the project.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Required Software
Required software for Java Programming

 JDK 1.8 64 bit or 32 bit according to your machine

[Link]

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Required Software (Contd.)
Required software for Java Programming

 Eclipse J2EE version for 64 bit or 32 bit according to your machine

[Link]

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Select a Workspace

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Create a Project
Select the version you want to
work in

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Create a Class

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


My First Java Program

class FirstProgram
{
public static void main
(String[] args)
{
[Link]
n ("Hello
World");
}
}
Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.
Execute java Program
 Short-cut  ctrl + F11

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Main Method
 Every program of Java should be inside a class with a class name

public means this


function is accessible
globally across all the
programs.

public static void main (String[] args)

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Main Method
 Every program of Java should be inside a class with a class name

void is the return type.


This means main does
not return any value.

public static void main (String[] args)

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Main Method
 Every program of Java should be inside a class with a class name

public static void main (String[] args)

static means without


creating the object of a
class, you can access this
method. You will study more
about static in 3rd module of
Java

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Main Method
 Every program of Java should be inside a class with a class name

public static void main (String[] args)

main is the function


which will be called by
JVM and String[] args
is the command line
arguments.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Programs for Data Types and Operations
 Program to add two numbers

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Choices in Real life
Control
Statements

Selection Iteration Jump


Statemen Statemen Statemen
ts ts ts

Switch..
If.. else break continue
Case

while do .. While for

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Choices in Real life

Honey can we go for a


movie today evening?

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


If-else Condition
Only, If I come back
from office at 5 or else
we shall go tomorrow.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Control Statement
1. If statement STAR
T

Selection
Statements Syntax:
If it is raining
if (condition):
Iteration TRUE
Statements statements 1 …
else: Take an Umbrella
Jump statements 2 … FALSE
Statements

Go Play Football

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Program on if Condition

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Control Statement
2. Switch statement STAR
T
Syntax:
Selection Outcom
Statements switch (expression) { e of
Toss
case value1: Statement1
Iteration TRU
Statements Hea E Tom Wins
break; d
FALS
case value2: Statement2 E TRU
Jump Tail E Adam Wins
Statements s
break; FALS
E Invalid
. defaul Input
. t

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Control Statement
3. do while statement START

Selection
Statements
Syntax:
EXECUTE BLOCK
do
Iteration
Statements {
repeat
statements…
Jump } while (condition is True) CHECK FALSE
Statements CONDITION

TRUE
EXIT LOOP

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Control Statement
4. while statement

STAR
T
Selection
Statements
Syntax: FALSE
CHECK
while (condition is True) CONDITIO
Iteration
N
Statements {
statements… TRUE
repeat
Jump EXIT
} LOO
Statements P

EXECUTE BLOCK

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


while-Loop : Program
 Program to print numbers from 1 to 10

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Control Statement
5. for statement STAR
T

Selection Syntax: Initialization


Statements for(initialization; condition;
FALSE
iteration) Check
Iteration condition
Statements {
statements… TRUE Exit loop

Jump }
Statements repeat Execute Statements

Iteration

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Repetitive Tasks in Real life

This is annoying. I
need to take 100
printouts in 10 mins
and I have to wait to
press the button
everytime it prints.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Repetitive Tasks in Real life(contd.)

This is annoying. I
need to take 100
printouts in 10 mins
and I have to wait to
press the button
everytime it prints.

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


for-Loop : Program
 Program to print numbers from 1 to 10

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Control Statement
6. break STAR
T

Selection
Statements
Synta FALSE
x: brea Check Loop
Iteration k;
Condition

Statements
TRUE EXIT LOOP
repeat
Jump TRUE
Check Break
Statements Condition

FALSE EXIT LOOP

Execute Block

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Control Statement
7. continue STAR
T

Selection
Statements
FALSE
Syntax: Check Loop
Condition
Iteration continue;
TRUE
Statements EXIT LOOP
repeat Execute Block 1

Jump
Check
Statements Continue
Conditio
TRU
n
E
FALSE

Execute Block 2

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Assignment – Data Types and Operations
 Write programs to use all the data types and given arithmetic operations

 Write program to perform all the arithmetic operations given in the table

Arithmetic Operators
+  Addition
-  Subtraction
*  Multiplication
/  Division
++  Increment operator
--  Decrement operator

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Assignment – if Condition
if condition
 Write a program to check if the candidate is eligible for voting or not. (Hint: Check age)

 Write a program to check if the number is positive or negative


Extend the previous program to check whether the given number is positive, zero or negative

(Hint: use if-else conditions)

 Write a program to find largest of two numbers

 Write a program to check given number is even or


odd (Hint: use % operator)

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Assignment – for-loop
 Write a program to print 10 even numbers and 10 odd numbers

 Write a program to find factorial of a number.

 Write a program to generate tables of 10

 Write a program to add the digits of a number

 Write a program to reverse the digits of a number

 Write a program to generate 10 Fibonacci numbers

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Assignment – while-loop
 Write a program to print 10 even numbers and 10 odd numbers

 Write a program to find factorial of a number

 Write a program to generate tables of 10

 Write a program to add the digits of a number

 Write a program to reverse the digits of a number

 Write a program to generate 10 Fibonacci numbers

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.


Assignment – do-while loop
 Write a program to print 10 even numbers and 10 odd numbers

 Write a program to find factorial of a number

 Write a program to generate tables of 10

 Write a program to add the digits of a number

 Write a program to reverse the digits of a number

 Write a program to generate 10 Fibonacci numbers

Copyright © 2020, Dursikshya and/or its affiliates. All rights reserved.

You might also like