SlideShare a Scribd company logo
By,
Prof. Basavaraj M. Hunshal
Prof. Mahadev Immannavar
Dept. of Computer Science &
Engg.
JAVA & Advanced Java :
A Crash Course
KLE Society’s
KLE College of Engineering & Technology,
Chikodi.
Contents
"Write Once, Run Everywhere"
 Motivation
 Introduction to Object oriented Programming
Paradigm.
 How Java is different from C++?
 Java Program structure with simple examples.
 Tokens of Java Language
 Control statements.
 Arrays
 Classes and Objects
 Pillars of OOPs : Encapsulation, Polymorphism &
Inheritance.
2
Motivation
&
Applications of Java
"Write Once, Run Everywhere"3
"Write Once, Run Everywhere"
 Java is one of the most popular programming languages used to
create Web applications and platforms. It was designed for flexibility,
allowing developers to write code that would run on any machine,
regardless of architecture or platform.
 According to Sun, more than 3 billion devices run java. There are
many devices where java is currently used. Some of them are as
follows:
 Desktop Applications such as acrobat reader, media player, antivirus
etc.
 Web Applications such as irctc.co.in, javatpoint.com etc.
 Enterprise Applications such as banking applications.
 Mobile
 Embedded System
 Smart Card
 Robotics
Reasons to Learn Java Programming
Language &
Why Java is Best ?
4
History of Java
"Write Once, Run Everywhere"
 Computer language innovation and development occurs
for two fundamental reasons:
1) To adapt to changing environments and uses.
2) To implement improvements in the art of
programming.
 Many Java features are inherited from the earlier
languages:
B  C  C++  Java
 Designed by James Gosling, Patrick Naughton, Chris
Warth, Ed Frank and Mike Sheridan at Sun
Microsystems in 1991.
 The original motivation is not Internet: platform-
independent software embedded in consumer
electronics appliances.
5
Features of Java
"Write Once, Run Everywhere"
 Simple - Syntax is based on C++.
 Object-Oriented - is a methodology that simplify software
development and maintenance by providing some rules.
 Platform Independent - Java code can be run on multiple
platforms e.g. Windows, Linux, Sun Solaris, Mac/OS etc. Java
code is compiled by the compiler and converted into bytecode.
This bytecode is a platform-independent code because it can be
run on multiple platforms i.e. Write Once and Run
Everywhere(WORE).
 Secured - Java is secured because:
 No explicit pointer
 Java Programs run inside virtual machine sandbox
6
"Write Once, Run Everywhere"
 Robust- Robust simply means strong. Java uses strong
memory management. There are lack of pointers that avoids
security problem. There is automatic garbage collection in
java. There is exception handling and type checking
mechanism in java.
 Architecture Neutral- There is no implementation
dependent features e.g. size of primitive types is fixed.
 Portable - We may carry the java bytecode to any platform.
 High Performance - Java is faster than traditional
interpretation since byte code is "close" to native code
 Distributed - We may access files by calling the
methods from any machine on the internet.
 Multi-threaded - A thread is like a separate program,
executing concurrently. We can write Java programs that
deal with many tasks at once by defining multiple threads.
7
Generation of Computer
Languages
"Write Once, Run Everywhere"8
"Write Once, Run Everywhere"
 OOP was introduced to overcome flaws in the
procedural approach to programming.
 Such as lack of reusability & maintainability.
 Fundamental idea behind object-oriented languages
is to combine into a single unit both data and the
functions that operate on that data. Such a unit is
called an object.
Introduction to OOPs
9
OOP -Concepts
"Write Once, Run Everywhere"
 Data Abstraction — it is the act of representing the
essential features without including the background
details. Data Hiding- it is a related concept of data
abstraction. Unessential features are hidden from the
world.
 Data Encapsulation — it is the wrapping of data and
associated functions in one single unit.
 Inheritance — it is the capability of one class to
inherit properties from other class.
 Polymorphism — it is the ability for data to be
processed in more than one form.
 Modularity — it is the concept of breaking down the
program into several module. Erg. : Classes,
Structure, etc.
10
How Java is differ from C++ ?
"Write Once, Run Everywhere"11
"Write Once, Run Everywhere"
What is Java?
• Java is a high level, robust, secured and object-oriented
programming language.
• Platform: Any hardware or software environment in which
a program runs, is known as a platform. Since Java has its
own runtime environment (JRE) and API, it is called
platform.
Simple Java Program -
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
12
"Write Once, Run Everywhere"
Execution of a Java
Program -
13
"Write Once, Run Everywhere"
 Tokens
 Keywords
 Identifiers
 Constants
 Operators
 Data types
Programming Elements in Java
Language
14
Keywords
"Write Once, Run Everywhere"
15
"Write Once, Run Everywhere"
 Java identifier names:
— first character must a letter or or $
— following characters can be any of those characters
or a number
— identifiers are case-sensitive; name is different from
Name
 Example Java identifiers:
olivia second_place _myName
belles’s 2%milk
kelly@yahoo.com
TheCure ANSWER IS 42 $variable
me+u :-) question?
side-swipe hi there ph.d
Identifiers
16
Operators in Java
"Write Once, Run Everywhere"17
"Write Once, Run Everywhere"
• Program to get the data from user
• Java program to add two numbers
• Swapping using temporary or third variable
18
 Java defines eight simple types:
1) byte – 8-bit integer type
2) short – 16-bit integer type
3) int – 32-bit integer type
4) long – 64-bit integer type
5) float – 32-bit floating-point type
6) double – 64-bit floating-point type
7) char – symbols in a character set
8) boolean – logical values true and false
Data Types
19 "Write Once, Run Everywhere"
Java control statements cause the flow of execution to
advance and branch based on the changes to the
state of the program.
Control statements are divided into three groups:
1. selection statements allow the program to choose
different parts of the execution based on the
outcome of an expression
2. iteration statements enable program execution to
repeat one or more statements
3. jump statements enable your program to execute
in a non-linear fashion
Control Statements
20 "Write Once, Run Everywhere"
 Java selection statements allow to control the
flow of program’s execution based upon
conditions known only during run-time.
 Java provides four selection statements:
1) if
2) if-else
3) if-else-if
4) switch
Selection Statements
21 "Write Once, Run Everywhere"
 Java iteration statements enable repeated execution of
part of a program until a certain termination condition
becomes true.
 Java provides three iteration statements:
1) while
2) do-while
3) for
4) for each
Iteration Statements
22 "Write Once, Run Everywhere"
 Java jump statements enable transfer of control to
other parts of program.
 Java provides three jump statements:
1) break
2) continue
3) return
 In addition, Java supports exception handling that
can also alter the control flow of a program.
"Write Once, Run Everywhere"
Jump Statements
23
"Write Once, Run Everywhere"
 Java program to check whether the given
number palindrome or not
 Java program to find the factorial of a given
number
24
"Write Once, Run Everywhere"
Type Conversion
• Size Direction of Data Type
– Widening Type Conversion (Casting down)
• Smaller Data Type  Larger Data Type
– Narrowing Type Conversion (Casting up)
• Larger Data Type  Smaller Data Type
• Conversion done in two ways
– Implicit type conversion
• Carried out by compiler automatically
– Explicit type conversion
• Carried out by programmer using casting
25
• Widening Type Conversion
– Implicit conversion by compiler automatically
byte -> short, int, long, float, double
short -> int, long, float, double
char -> int, long, float, double
int -> long, float, double
long -> float, double
float -> double
26 "Write Once, Run Everywhere"
• Narrowing Type Conversion
– Programmer should describe the conversion
explicitly
byte -> char
short -> byte, char
char -> byte, short
int -> byte, short, char
long -> byte, short, char, int
float -> byte, short, char, int, long
double -> byte, short, char, int, long, float
27 "Write Once, Run Everywhere"
 byte and short are always promoted to int
 if one operand is long, the whole expression is
promoted to long
 if one operand is float, the entire expression is
promoted to float
 if any operand is double, the result is double
Type Conversion
28 "Write Once, Run Everywhere"
 General form: (targetType) value
 Examples:
1) integer value will be reduced module bytes
range:
int i=10;
byte b = (byte) i;
2) floating-point value will be truncated to
integer
value:
float f=15.5;
int i = (int) f;
Type Casting
29 "Write Once, Run Everywhere"
Arrays
"Write Once, Run Everywhere"
 An array is a group of same-typed objects
referred by a common name, with individual
objects accessed by their index.
 Arrays are:
1) declared
2) created
3) initialized
4) used
 Also, arrays can have one or several dimensions.
30
 Array declaration involves:
1) declaring an array identifier
2) declaring the number of dimensions
3) declaring the data type of the array elements
 Two styles of array declaration:
type array-variable[];
or
type [] array-variable;
Array Declaration
31 "Write Once, Run Everywhere"
 After declaration, no array actually exists.
 In order to create an array, we use the new
operator:
type array-variable[];
array-variable = new type[size];
 This creates a new array to hold size elements of
type type, which reference will be kept in the
variable array-variable.
Array Creation
32 "Write Once, Run Everywhere"
 We can refer to the elements of this array through
their indexes:
 array-variable[index]
 The array index always starts with zero!
 The Java run-time system makes sure that all array
indexes are in the correct range, otherwise raises a
run-time error.
Array Indexing
Array
Initialization Arrays can be initialized when they are declared:
 int monthDays[] =
{31,28,31,30,31,30,31,31,30,31,30,31};
 Note:
1) there is no need to use the new operator
2) the array is created large enough to hold all specified
elements33 "Write Once, Run Everywhere"
 Multidimensional arrays are arrays of arrays:
1) declaration: int array[][];
2) creation: int array = new int[2][3];
3) initialization
int array[][] = { {1, 2, 3}, {4, 5, 6} };
Examples:
 Find Largest and Smallest Number in an Array
 Java program for linear search
 Java program for Binary search
 Java program to add two matrices
Multidimensional Arrays
34 "Write Once, Run Everywhere"
 Real world objects are things that have:
1) state
2) behavior
Example: your dog:
 state – name, color, breed, sits?, barks?, wages
tail?, runs?
 behavior – sitting, barking, waging tail, running
 A software object is a bundle of variables (state)
and methods (operations).
What is an Object?
35 "Write Once, Run Everywhere"
 A class is a blueprint that defines the variables and
methods common to all objects of a certain kind.
 Example: ‘your dog’ is a object of the class Dog.
 An object holds values for the variables defines in the
class.
 An object is called an instance of the Class
What is a Class?
36 "Write Once, Run Everywhere"
 A variable is declared to refer to the objects of
type/class String:
String s;
 The value of s is null; it does not yet refer to any
object.
 A new String object is created in memory with
initial “abc” value:
 String s = new String(“abc”);
 Now s contains the address of this new object.
Object Creation
37 "Write Once, Run Everywhere"
 A program accumulates memory through its
execution.
 Two mechanism to free memory that is no longer
need by the program:
1) manual – done in C/C++
2) automatic – done in Java
 In Java, when an object is no longer accessible
through any variable, it is eventually removed from
the memory by the garbage collector.
 Garbage collector is parts of the Java Run-Time
Environment.
Object Destruction
38 "Write Once, Run Everywhere"
 A basis for the Java language.
 Each concept we wish to describe in Java must be
included inside a class.
 A class defines a new data type, whose values are
objects:
 A class is a template for objects
 An object is an instance of a class
Class
39 "Write Once, Run Everywhere"
 A class contains a name, several variable declarations
(instance variables) and several method declarations. All
are called members of the class.
 General form of a class:
class classname
{
type instance-variable-1;
…
type instance-variable-n;
type method-name-1(parameter-list) { … }
type method-name-2(parameter-list) { … }
…
type method-name-m(parameter-list) { … }
}
Class Definition
40 "Write Once, Run Everywhere"
class Box {
double width;
double height;
double depth;
}
class BoxDemo
{
public static void main(String args[])
{
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println ("Volume is " + vol);
}
}
Example: Class Usage
41 "Write Once, Run Everywhere"
 A constructor initializes the instance variables of an
object.
 It is called immediately after the object is created but
before the new operator completes.
1) it is syntactically similar to a method:
2) it has the same name as the name of its class
3) it is written without return type; the default
return type of a class
 constructor is the same class
 When the class has no constructor, the default
constructor automatically initializes all its instance
variables with zero.
"Write Once, Run Everywhere"
Constructor
42
class Box
{
double width;
double height;
double depth;
Box()
{
System.out.println("Constructing Box");
width = 10; height = 10; depth = 10;
}
double volume()
{
return width * height * depth;
}
}
Example: Constructor
43 "Write Once, Run Everywhere"
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d)
{
width = w; height = h; depth = d;
}
double volume()
{
return width * height * depth;
}
}
Parameterized Constructor
44 "Write Once, Run Everywhere"
 General form of a method definition:
type name(parameter-list)
{
… return value;
…
}
 Components:
1. type - type of values returned by the method. If a
method does not return any value, its return type
must be void.
2. name is the name of the method
3. parameter-list is a sequence of type-identifier lists
separated by commas
4. return value indicates what value is returned by the
method.
Methods
45 "Write Once, Run Everywhere"
 Classes declare methods to hide their internal data
structures, as well as for their own internal use:
Within a class, we can refer directly to its member
variables:
class Box
{
double width, height, depth;
void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
Example: Method
46 "Write Once, Run Everywhere"
 Parameters increase generality and applicability
of a method:
1) method without parameters
int square() { return 10*10; }
2) method with parameters
int square(int i) { return i*i; }
 Parameter: a variable receiving value at the time
the method is invoked.
 Argument: a value passed to the method when it
is invoked.
Parameterized Method
47 "Write Once, Run Everywhere"
Access Control: Data Hiding and
Encapsulation
• Java provides control over the visibility of variables and
methods.
• Encapsulation, safely sealing data within the capsule of the
class Prevents programmers from relying on details of class
implementation, so you can update without worry
• Helps in protecting against accidental or wrong usage.
• Keeps code elegant and clean (easier to maintain)
48 "Write Once, Run Everywhere"
Access Modifiers: Public, Private, Protected
• Public: keyword applied to a class, makes it
available/visible everywhere. Applied to a method
or variable, completely visible.
• Private: fields or methods for a class only visible
within that class. Private members are not visible
within subclasses, and are not inherited.
• Protected: members of a class are visible within
the class, subclasses and also within all classes
that are in the same package as that class.
49 "Write Once, Run Everywhere"
Visibility
public class Circle {
private double x,y,r;
// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r;}
public double area() { return 3.14 * r * r; }
}
50 "Write Once, Run Everywhere"
Keyword “this”
• Can be used by any object to refer to itself in any
class method
• Typically used to
– Avoid variable name collisions
– Pass the receiver as an argument
– Chain constructors
51 "Write Once, Run Everywhere"
 Keyword this allows a method to refer to the
object that invoked it.
 It can be used inside any method to refer to the
current object:
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
52 "Write Once, Run Everywhere"
 Garbage collection is a mechanism to remove objects
from memory when they are no longer needed.
 Garbage collection is carried out by the garbage
collector:
1) The garbage collector keeps track of how many references
an object has.
2) It removes an object from memory when it has no longer
any references.
3) Thereafter, the memory occupied by the object can be
allocated again.
4) The garbage collector invokes the finalize method.
Garbage Collection
53 "Write Once, Run Everywhere"
 A constructor helps to initialize an object just after
it has been created.
 In contrast, the finalize method is invoked just
before the object is destroyed:
 1) implemented inside a class as:
protected void finalize() { … }
 2) implemented when the usual way of removing
objects from memory is insufficient, and some
special actions has to be carried out
finalize() Method
54 "Write Once, Run Everywhere"
Ad

Recommended

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Java Tokens
Java Tokens
Madishetty Prathibha
 
Abstract class in java
Abstract class in java
Lovely Professional University
 
OOPS In JAVA.pptx
OOPS In JAVA.pptx
Sachin33417
 
Arrays in Java
Arrays in Java
Naz Abdalla
 
I/O Streams
I/O Streams
Ravi Chythanya
 
Data Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in java
HrithikShinde
 
Java
Java
Tony Nguyen
 
Introduction to java
Introduction to java
Java Lover
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
JAVA PPT Part-1 BY ADI.pdf
JAVA PPT Part-1 BY ADI.pdf
Prof. Dr. K. Adisesha
 
Control statements in java
Control statements in java
Madishetty Prathibha
 
Method overriding
Method overriding
Azaz Maverick
 
Static Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
JVM
JVM
baabtra.com - No. 1 supplier of quality freshers
 
Multithreading in java
Multithreading in java
Monika Mishra
 
Structure of java program diff c- cpp and java
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
Java program structure
Java program structure
shalinikarunakaran1
 
Java tutorial PPT
Java tutorial PPT
Intelligo Technologies
 
Dynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
 
JAVA ENVIRONMENT
JAVA ENVIRONMENT
josemachoco
 
Java tokens
Java tokens
shalinikarunakaran1
 
Super Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Oop java
Oop java
Minal Maniar
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
NITHISG1
 
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
Edureka!
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
prat0ham
 
Java1
Java1
computertuitions
 

More Related Content

What's hot (20)

Java
Java
Tony Nguyen
 
Introduction to java
Introduction to java
Java Lover
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
JAVA PPT Part-1 BY ADI.pdf
JAVA PPT Part-1 BY ADI.pdf
Prof. Dr. K. Adisesha
 
Control statements in java
Control statements in java
Madishetty Prathibha
 
Method overriding
Method overriding
Azaz Maverick
 
Static Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
JVM
JVM
baabtra.com - No. 1 supplier of quality freshers
 
Multithreading in java
Multithreading in java
Monika Mishra
 
Structure of java program diff c- cpp and java
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
Java program structure
Java program structure
shalinikarunakaran1
 
Java tutorial PPT
Java tutorial PPT
Intelligo Technologies
 
Dynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
 
JAVA ENVIRONMENT
JAVA ENVIRONMENT
josemachoco
 
Java tokens
Java tokens
shalinikarunakaran1
 
Super Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Oop java
Oop java
Minal Maniar
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
NITHISG1
 
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
Edureka!
 
Introduction to java
Introduction to java
Java Lover
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Multithreading in java
Multithreading in java
Monika Mishra
 
Structure of java program diff c- cpp and java
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
JAVA ENVIRONMENT
JAVA ENVIRONMENT
josemachoco
 
Super Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
NITHISG1
 
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
Edureka!
 

Similar to Java & advanced java (20)

Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
prat0ham
 
Java1
Java1
computertuitions
 
Java
Java
computertuitions
 
M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)
hossamghareb681
 
Modern_2.pptx for java
Modern_2.pptx for java
MayaTofik
 
Assignmentjsnsnshshusjdnsnshhzudjdndndjd
Assignmentjsnsnshshusjdnsnshhzudjdndndjd
tusharjain613841
 
Introduction to java
Introduction to java
Sujit Majety
 
Java_presesntation.ppt
Java_presesntation.ppt
VGaneshKarthikeyan
 
Introduction to Java Basics Programming Java Basics-I.pptx
Introduction to Java Basics Programming Java Basics-I.pptx
SANDHYAP32
 
Java 3 rd sem. 2012 aug.ASSIGNMENT
Java 3 rd sem. 2012 aug.ASSIGNMENT
mayank's it solution pvt.ltd
 
Comp102 lec 3
Comp102 lec 3
Fraz Bakhsh
 
Functional Programming In Jdk8
Functional Programming In Jdk8
Bansilal Haudakari
 
Java For beginners to build a strong foundation
Java For beginners to build a strong foundation
Satheesh Chandran
 
Introduction java programming
Introduction java programming
Nanthini Kempaiyan
 
JAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptx
SuganthiDPSGRKCW
 
Lecture 1.1 - Introducing Java.pptx3eeeee
Lecture 1.1 - Introducing Java.pptx3eeeee
SaziaSharmin2
 
java notes.pdf
java notes.pdf
JitendraYadav351971
 
Unit-1_GHD.pptxguguigihihihihihihoihihhi
Unit-1_GHD.pptxguguigihihihihihihoihihhi
40NehaPagariya
 
Top 10 Java Interview Questions and Answers 2014
Top 10 Java Interview Questions and Answers 2014
iimjobs and hirist
 
Core Java
Core Java
christ university
 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
prat0ham
 
M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)
hossamghareb681
 
Modern_2.pptx for java
Modern_2.pptx for java
MayaTofik
 
Assignmentjsnsnshshusjdnsnshhzudjdndndjd
Assignmentjsnsnshshusjdnsnshhzudjdndndjd
tusharjain613841
 
Introduction to java
Introduction to java
Sujit Majety
 
Introduction to Java Basics Programming Java Basics-I.pptx
Introduction to Java Basics Programming Java Basics-I.pptx
SANDHYAP32
 
Functional Programming In Jdk8
Functional Programming In Jdk8
Bansilal Haudakari
 
Java For beginners to build a strong foundation
Java For beginners to build a strong foundation
Satheesh Chandran
 
JAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptx
SuganthiDPSGRKCW
 
Lecture 1.1 - Introducing Java.pptx3eeeee
Lecture 1.1 - Introducing Java.pptx3eeeee
SaziaSharmin2
 
Unit-1_GHD.pptxguguigihihihihihihoihihhi
Unit-1_GHD.pptxguguigihihihihihihoihihhi
40NehaPagariya
 
Top 10 Java Interview Questions and Answers 2014
Top 10 Java Interview Questions and Answers 2014
iimjobs and hirist
 
Ad

Recently uploaded (20)

Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
nnnnnnnnnnnn7777777777777777777777777777777.pptx
nnnnnnnnnnnn7777777777777777777777777777777.pptx
gayathri venkataramani
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
nnnnnnnnnnnn7777777777777777777777777777777.pptx
nnnnnnnnnnnn7777777777777777777777777777777.pptx
gayathri venkataramani
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Ad

Java & advanced java

  • 1. By, Prof. Basavaraj M. Hunshal Prof. Mahadev Immannavar Dept. of Computer Science & Engg. JAVA & Advanced Java : A Crash Course KLE Society’s KLE College of Engineering & Technology, Chikodi.
  • 2. Contents "Write Once, Run Everywhere"  Motivation  Introduction to Object oriented Programming Paradigm.  How Java is different from C++?  Java Program structure with simple examples.  Tokens of Java Language  Control statements.  Arrays  Classes and Objects  Pillars of OOPs : Encapsulation, Polymorphism & Inheritance. 2
  • 4. "Write Once, Run Everywhere"  Java is one of the most popular programming languages used to create Web applications and platforms. It was designed for flexibility, allowing developers to write code that would run on any machine, regardless of architecture or platform.  According to Sun, more than 3 billion devices run java. There are many devices where java is currently used. Some of them are as follows:  Desktop Applications such as acrobat reader, media player, antivirus etc.  Web Applications such as irctc.co.in, javatpoint.com etc.  Enterprise Applications such as banking applications.  Mobile  Embedded System  Smart Card  Robotics Reasons to Learn Java Programming Language & Why Java is Best ? 4
  • 5. History of Java "Write Once, Run Everywhere"  Computer language innovation and development occurs for two fundamental reasons: 1) To adapt to changing environments and uses. 2) To implement improvements in the art of programming.  Many Java features are inherited from the earlier languages: B  C  C++  Java  Designed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan at Sun Microsystems in 1991.  The original motivation is not Internet: platform- independent software embedded in consumer electronics appliances. 5
  • 6. Features of Java "Write Once, Run Everywhere"  Simple - Syntax is based on C++.  Object-Oriented - is a methodology that simplify software development and maintenance by providing some rules.  Platform Independent - Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris, Mac/OS etc. Java code is compiled by the compiler and converted into bytecode. This bytecode is a platform-independent code because it can be run on multiple platforms i.e. Write Once and Run Everywhere(WORE).  Secured - Java is secured because:  No explicit pointer  Java Programs run inside virtual machine sandbox 6
  • 7. "Write Once, Run Everywhere"  Robust- Robust simply means strong. Java uses strong memory management. There are lack of pointers that avoids security problem. There is automatic garbage collection in java. There is exception handling and type checking mechanism in java.  Architecture Neutral- There is no implementation dependent features e.g. size of primitive types is fixed.  Portable - We may carry the java bytecode to any platform.  High Performance - Java is faster than traditional interpretation since byte code is "close" to native code  Distributed - We may access files by calling the methods from any machine on the internet.  Multi-threaded - A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. 7
  • 9. "Write Once, Run Everywhere"  OOP was introduced to overcome flaws in the procedural approach to programming.  Such as lack of reusability & maintainability.  Fundamental idea behind object-oriented languages is to combine into a single unit both data and the functions that operate on that data. Such a unit is called an object. Introduction to OOPs 9
  • 10. OOP -Concepts "Write Once, Run Everywhere"  Data Abstraction — it is the act of representing the essential features without including the background details. Data Hiding- it is a related concept of data abstraction. Unessential features are hidden from the world.  Data Encapsulation — it is the wrapping of data and associated functions in one single unit.  Inheritance — it is the capability of one class to inherit properties from other class.  Polymorphism — it is the ability for data to be processed in more than one form.  Modularity — it is the concept of breaking down the program into several module. Erg. : Classes, Structure, etc. 10
  • 11. How Java is differ from C++ ? "Write Once, Run Everywhere"11
  • 12. "Write Once, Run Everywhere" What is Java? • Java is a high level, robust, secured and object-oriented programming language. • Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has its own runtime environment (JRE) and API, it is called platform. Simple Java Program - class Simple { public static void main(String args[]) { System.out.println("Hello Java"); } } 12
  • 13. "Write Once, Run Everywhere" Execution of a Java Program - 13
  • 14. "Write Once, Run Everywhere"  Tokens  Keywords  Identifiers  Constants  Operators  Data types Programming Elements in Java Language 14
  • 15. Keywords "Write Once, Run Everywhere" 15
  • 16. "Write Once, Run Everywhere"  Java identifier names: — first character must a letter or or $ — following characters can be any of those characters or a number — identifiers are case-sensitive; name is different from Name  Example Java identifiers: olivia second_place _myName belles’s 2%milk [email protected] TheCure ANSWER IS 42 $variable me+u :-) question? side-swipe hi there ph.d Identifiers 16
  • 17. Operators in Java "Write Once, Run Everywhere"17
  • 18. "Write Once, Run Everywhere" • Program to get the data from user • Java program to add two numbers • Swapping using temporary or third variable 18
  • 19.  Java defines eight simple types: 1) byte – 8-bit integer type 2) short – 16-bit integer type 3) int – 32-bit integer type 4) long – 64-bit integer type 5) float – 32-bit floating-point type 6) double – 64-bit floating-point type 7) char – symbols in a character set 8) boolean – logical values true and false Data Types 19 "Write Once, Run Everywhere"
  • 20. Java control statements cause the flow of execution to advance and branch based on the changes to the state of the program. Control statements are divided into three groups: 1. selection statements allow the program to choose different parts of the execution based on the outcome of an expression 2. iteration statements enable program execution to repeat one or more statements 3. jump statements enable your program to execute in a non-linear fashion Control Statements 20 "Write Once, Run Everywhere"
  • 21.  Java selection statements allow to control the flow of program’s execution based upon conditions known only during run-time.  Java provides four selection statements: 1) if 2) if-else 3) if-else-if 4) switch Selection Statements 21 "Write Once, Run Everywhere"
  • 22.  Java iteration statements enable repeated execution of part of a program until a certain termination condition becomes true.  Java provides three iteration statements: 1) while 2) do-while 3) for 4) for each Iteration Statements 22 "Write Once, Run Everywhere"
  • 23.  Java jump statements enable transfer of control to other parts of program.  Java provides three jump statements: 1) break 2) continue 3) return  In addition, Java supports exception handling that can also alter the control flow of a program. "Write Once, Run Everywhere" Jump Statements 23
  • 24. "Write Once, Run Everywhere"  Java program to check whether the given number palindrome or not  Java program to find the factorial of a given number 24
  • 25. "Write Once, Run Everywhere" Type Conversion • Size Direction of Data Type – Widening Type Conversion (Casting down) • Smaller Data Type  Larger Data Type – Narrowing Type Conversion (Casting up) • Larger Data Type  Smaller Data Type • Conversion done in two ways – Implicit type conversion • Carried out by compiler automatically – Explicit type conversion • Carried out by programmer using casting 25
  • 26. • Widening Type Conversion – Implicit conversion by compiler automatically byte -> short, int, long, float, double short -> int, long, float, double char -> int, long, float, double int -> long, float, double long -> float, double float -> double 26 "Write Once, Run Everywhere"
  • 27. • Narrowing Type Conversion – Programmer should describe the conversion explicitly byte -> char short -> byte, char char -> byte, short int -> byte, short, char long -> byte, short, char, int float -> byte, short, char, int, long double -> byte, short, char, int, long, float 27 "Write Once, Run Everywhere"
  • 28.  byte and short are always promoted to int  if one operand is long, the whole expression is promoted to long  if one operand is float, the entire expression is promoted to float  if any operand is double, the result is double Type Conversion 28 "Write Once, Run Everywhere"
  • 29.  General form: (targetType) value  Examples: 1) integer value will be reduced module bytes range: int i=10; byte b = (byte) i; 2) floating-point value will be truncated to integer value: float f=15.5; int i = (int) f; Type Casting 29 "Write Once, Run Everywhere"
  • 30. Arrays "Write Once, Run Everywhere"  An array is a group of same-typed objects referred by a common name, with individual objects accessed by their index.  Arrays are: 1) declared 2) created 3) initialized 4) used  Also, arrays can have one or several dimensions. 30
  • 31.  Array declaration involves: 1) declaring an array identifier 2) declaring the number of dimensions 3) declaring the data type of the array elements  Two styles of array declaration: type array-variable[]; or type [] array-variable; Array Declaration 31 "Write Once, Run Everywhere"
  • 32.  After declaration, no array actually exists.  In order to create an array, we use the new operator: type array-variable[]; array-variable = new type[size];  This creates a new array to hold size elements of type type, which reference will be kept in the variable array-variable. Array Creation 32 "Write Once, Run Everywhere"
  • 33.  We can refer to the elements of this array through their indexes:  array-variable[index]  The array index always starts with zero!  The Java run-time system makes sure that all array indexes are in the correct range, otherwise raises a run-time error. Array Indexing Array Initialization Arrays can be initialized when they are declared:  int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};  Note: 1) there is no need to use the new operator 2) the array is created large enough to hold all specified elements33 "Write Once, Run Everywhere"
  • 34.  Multidimensional arrays are arrays of arrays: 1) declaration: int array[][]; 2) creation: int array = new int[2][3]; 3) initialization int array[][] = { {1, 2, 3}, {4, 5, 6} }; Examples:  Find Largest and Smallest Number in an Array  Java program for linear search  Java program for Binary search  Java program to add two matrices Multidimensional Arrays 34 "Write Once, Run Everywhere"
  • 35.  Real world objects are things that have: 1) state 2) behavior Example: your dog:  state – name, color, breed, sits?, barks?, wages tail?, runs?  behavior – sitting, barking, waging tail, running  A software object is a bundle of variables (state) and methods (operations). What is an Object? 35 "Write Once, Run Everywhere"
  • 36.  A class is a blueprint that defines the variables and methods common to all objects of a certain kind.  Example: ‘your dog’ is a object of the class Dog.  An object holds values for the variables defines in the class.  An object is called an instance of the Class What is a Class? 36 "Write Once, Run Everywhere"
  • 37.  A variable is declared to refer to the objects of type/class String: String s;  The value of s is null; it does not yet refer to any object.  A new String object is created in memory with initial “abc” value:  String s = new String(“abc”);  Now s contains the address of this new object. Object Creation 37 "Write Once, Run Everywhere"
  • 38.  A program accumulates memory through its execution.  Two mechanism to free memory that is no longer need by the program: 1) manual – done in C/C++ 2) automatic – done in Java  In Java, when an object is no longer accessible through any variable, it is eventually removed from the memory by the garbage collector.  Garbage collector is parts of the Java Run-Time Environment. Object Destruction 38 "Write Once, Run Everywhere"
  • 39.  A basis for the Java language.  Each concept we wish to describe in Java must be included inside a class.  A class defines a new data type, whose values are objects:  A class is a template for objects  An object is an instance of a class Class 39 "Write Once, Run Everywhere"
  • 40.  A class contains a name, several variable declarations (instance variables) and several method declarations. All are called members of the class.  General form of a class: class classname { type instance-variable-1; … type instance-variable-n; type method-name-1(parameter-list) { … } type method-name-2(parameter-list) { … } … type method-name-m(parameter-list) { … } } Class Definition 40 "Write Once, Run Everywhere"
  • 41. class Box { double width; double height; double depth; } class BoxDemo { public static void main(String args[]) { Box mybox = new Box(); double vol; mybox.width = 10; mybox.height = 20; mybox.depth = 15; vol = mybox.width * mybox.height * mybox.depth; System.out.println ("Volume is " + vol); } } Example: Class Usage 41 "Write Once, Run Everywhere"
  • 42.  A constructor initializes the instance variables of an object.  It is called immediately after the object is created but before the new operator completes. 1) it is syntactically similar to a method: 2) it has the same name as the name of its class 3) it is written without return type; the default return type of a class  constructor is the same class  When the class has no constructor, the default constructor automatically initializes all its instance variables with zero. "Write Once, Run Everywhere" Constructor 42
  • 43. class Box { double width; double height; double depth; Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } double volume() { return width * height * depth; } } Example: Constructor 43 "Write Once, Run Everywhere"
  • 44. class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } double volume() { return width * height * depth; } } Parameterized Constructor 44 "Write Once, Run Everywhere"
  • 45.  General form of a method definition: type name(parameter-list) { … return value; … }  Components: 1. type - type of values returned by the method. If a method does not return any value, its return type must be void. 2. name is the name of the method 3. parameter-list is a sequence of type-identifier lists separated by commas 4. return value indicates what value is returned by the method. Methods 45 "Write Once, Run Everywhere"
  • 46.  Classes declare methods to hide their internal data structures, as well as for their own internal use: Within a class, we can refer directly to its member variables: class Box { double width, height, depth; void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); } } Example: Method 46 "Write Once, Run Everywhere"
  • 47.  Parameters increase generality and applicability of a method: 1) method without parameters int square() { return 10*10; } 2) method with parameters int square(int i) { return i*i; }  Parameter: a variable receiving value at the time the method is invoked.  Argument: a value passed to the method when it is invoked. Parameterized Method 47 "Write Once, Run Everywhere"
  • 48. Access Control: Data Hiding and Encapsulation • Java provides control over the visibility of variables and methods. • Encapsulation, safely sealing data within the capsule of the class Prevents programmers from relying on details of class implementation, so you can update without worry • Helps in protecting against accidental or wrong usage. • Keeps code elegant and clean (easier to maintain) 48 "Write Once, Run Everywhere"
  • 49. Access Modifiers: Public, Private, Protected • Public: keyword applied to a class, makes it available/visible everywhere. Applied to a method or variable, completely visible. • Private: fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited. • Protected: members of a class are visible within the class, subclasses and also within all classes that are in the same package as that class. 49 "Write Once, Run Everywhere"
  • 50. Visibility public class Circle { private double x,y,r; // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; } //Methods to return circumference and area public double circumference() { return 2*3.14*r;} public double area() { return 3.14 * r * r; } } 50 "Write Once, Run Everywhere"
  • 51. Keyword “this” • Can be used by any object to refer to itself in any class method • Typically used to – Avoid variable name collisions – Pass the receiver as an argument – Chain constructors 51 "Write Once, Run Everywhere"
  • 52.  Keyword this allows a method to refer to the object that invoked it.  It can be used inside any method to refer to the current object: Box(double width, double height, double depth) { this.width = width; this.height = height; this.depth = depth; } 52 "Write Once, Run Everywhere"
  • 53.  Garbage collection is a mechanism to remove objects from memory when they are no longer needed.  Garbage collection is carried out by the garbage collector: 1) The garbage collector keeps track of how many references an object has. 2) It removes an object from memory when it has no longer any references. 3) Thereafter, the memory occupied by the object can be allocated again. 4) The garbage collector invokes the finalize method. Garbage Collection 53 "Write Once, Run Everywhere"
  • 54.  A constructor helps to initialize an object just after it has been created.  In contrast, the finalize method is invoked just before the object is destroyed:  1) implemented inside a class as: protected void finalize() { … }  2) implemented when the usual way of removing objects from memory is insufficient, and some special actions has to be carried out finalize() Method 54 "Write Once, Run Everywhere"