Uni1 PPT-1
Uni1 PPT-1
Uni1 PPT-1
PROGRAMMING THROUGH
JAVA
Course Outcomes
Understand the syntax and semantics of
JAVA programming language to solve a
given problem.
Apply the knowledge of Object Oriented
Programming principles to develop
applications.
Apply the concepts of packages, I/O and
Exception handling mechanisms to develop
efficient programming.
Analyze the concepts of Multithreading and
Collection Framework to develop
applications which mimic the real-world
scenarios.
UNIT1
Object Oriented Programming: Basic concepts, Principles,
Program Structure in JAVA: Introduction, Writing Simple JAVA Programs,
Elements or Tokens in JAVA Programs, JAVA Statements, Command Line
Arguments, User Input to Programs, Escape Sequences, Comments,
Programming Style.
Data Types, Variables, and Operators :Introduction, Declaration of
Variables, Data Types, Type Casting, Scope of Variable, Identifier, Literal
Constants, Symbolic Constants, Formatted Output with printf() Method,
Static Variables and Methods, Attribute Final,
Introduction to Operators, Precedence and Associativity of
Operators, Assignment Operator ( = ), Basic Arithmetic Operators,
Increment (++) and Decrement (- -) Operators, Ternary Operator,
Relational Operators, Boolean Logical Operators, Bitwise Logical
Operators.
Control Statements :Introduction, if Expression, Nested if
Expressions, if–else Expressions, Ternary Operator?:, Switch Statement,
Iteration Statements, while Expression, do–while Loop, for Loop, Nested
for Loop, For–Each for Loop, Break Statement, Continue Statement.
UNIT 2
Classes and Objects: Introduction, Class Declaration
and Modifiers, Class Members, Declaration of Class
Objects, Assigning One Object to Another.
Constructor and Methods: Introduction, Defining
Methods, Overloaded Methods, Constructors,
Overloaded Constructor Methods , Class Objects as
Parameters in Methods, Access Control, Accessing
Private Members of Class, Recursive Methods, final
method, Passing Arguments by Value and by Reference,
Keyword this, final and static, Nested classes.
String Handling in JAVA: Introduction, Interface Char
Sequence, Class String, Methods for Extracting
Characters from Strings, Comparison, Modifying,
Searching; Class StringBuffer.
UNIT 3
Arrays: Introduction, Declaration and Initialization of Arrays,
Storage of Array in Computer Memory, Accessing Elements of
Arrays, Operations on Array Elements, Assigning Array to
Another Array, Dynamic Change of Array Size, Sorting of Arrays,
Search for Values in Arrays, Class Arrays, Two-dimensional
Arrays, Arrays of Varying Lengths, Three-dimensional Arrays,
Arrays as Vectors.
Inheritance: Introduction, Process of Inheritance, Types of
Inheritances, Universal Super Class-Object Class, Inhibiting
Inheritance of Class Using Final, Access Control and Inheritance,
Multilevel Inheritance, Application of Keyword Super, Constructor
Method and Inheritance, Method Overriding, Dynamic Method
Dispatch, Abstract Classes.
Interfaces: Introduction, Declaration of Interface,
Implementation of Interface, Multiple Interfaces, Nested
Interfaces, Inheritance of Interfaces, Default Methods in
Interfaces, Static Methods in Interface.
UNIT 4
Packages and JAVA Library: Introduction, Defining
Package, Importing Packages and Classes into Programs,
Path and Class Path, Access Control, Packages in JAVA
SE, java.lang Package and its Classes, Class Object, class
Math, Wrapper Classes, Auto-boxing and Auto-unboxing.
Exception Handling: Introduction, Hierarchy of
Standard Exception Classes, Keywords throws and throw,
try, catch, and finally Blocks, Multiple Catch Clauses,
Class Throwable, Unchecked Exceptions, Checked
Exceptions, custom exceptions.
Java I/O and File: Java I/O API, standard I/O streams,
types, Byte streams, Character streams, Scanner class,
Files in Java: File, FileInputStream and FileOutputStream
Classes(Text Book 2)
UNIT 5
Multithreaded Programming:
Introduction, Need for Multiple Threads
Multithreaded Programming for Multi-core
Processor, Thread Class, Main Thread-
Creation of New Threads, Thread States,
Thread Priority
Collections Framework : Introduction,
Purpose of Collection Framework,
Application of Collection Framework,
Hierarchy of collection Interfaces / classes,
Methods defined in Collection Interface,
Interface Iterator, Collection
classes/Interfaces –List,Queue,Set,
Text Books
JAVA one step ahead, Anitha Seth,
B.L.Juneja, Oxford.
Joy with JAVA, Fundamentals of Object
Oriented Programming, Debasis Samanta,
Monalisa Sarma, Cambridge, 2023.
History
1990 – Sun Microsystems – Stealth Project -
Green Project.
1991 – James Gosling, Patric Naughton, Mike
Sheradin, Bill joy
James Gosling – Development of programming
language – C/C++ - platform independent.
Initial name is Oak – Later named as Java.
In 1994 – Patric Naughton - WebRunner- a Java
based Web Browser – Applets - HotJava.
In 1995, Sun Microsystems formally announced
Java and HotJava .
9
Processor Developers
Intel
AMD
Qualcomm
NVIDIA
IBM
Samsung
Motorola
Hewlett-Packard (hp)
Dell
Acer
VIA
Marvell
Global Foundries
Media Tek
Atmel
Freescale
MSNCC-Microcell
Transmeta
Cyrix
Rockchip
10
History of Java
JDK Alpha and Beta (1995)
Why Java populated
JDK 1.0 (23rd Jan, 1996)
Portability
JDK 1.1 (19th Feb, 1997) Security
J2SE 1.2 (8th Dec, 1998) Applets
J2SE 1.3 (8th May, 2000)
J2SE 1.4 (6th Feb, 2002)
J2SE 5.0 (30th Sep, 2004)
Java SE 6 (11th Dec, 2006)
Java SE 7 (28th July, 2011)
Java SE 8 (18th March, 2014) - 217 Packages and 4240 Classes.
Java SE 9 (September 2017)
Java SE 10 (March 2018)
Java SE 11 (September 2018)
Java SE 12 (March 2019)
Java SE 13 (September 2019)
11
About Java
J2SE – Java 2 Platform, Standard edition -
for development and deployment
of portable applications.
J2EE -Java 2 Platform, Enterprise edition -
web based enterprise applications
J2ME - Java 2 Platform, Micro edition - Java
applications for mobile technology.
12
J2SE
As per SE22
Total Java Classes Total Classes 5,900
o 4240 in java 8 Total Packages 235
o 4024 in java 7
o 3793 in java 6
o 3279 in java 5.0
Total Java Packages
o 217
13
Java’s Magic : Bytecode
14
Java Features
15
Java Features
Simple
It is free from pointer due to this execution time of application
is improve.
It have rich set of APIs (application programming interface).
It have Garbage Collector which is always used to collect un-
referenced (unused) memory locations for improving performance
of a Java program.
It contains user friendly syntax for developing any applications
Object Oriented
Java supports OOP features and everything is an Object in Java.
The object model in Java is simple and easy to extend.
Robust
Java is robust or strong programming language because of its
capability to handle run-time errors, such as exception handling.
16
Java Features
Multi Threading:
Java was designed to meet the real-world requirement of creating interactive,
networked programs.
To accomplish this, Java supports multithreaded programming, which allows
Architecture-Neutral:
17
Java Features
Interpreted:
High Performance:
The problem with interpreter inside JVM is that it is slow. Because
of this, Java programs used to run slow. To overcome this, along
with interpreter, Java people have introduced JIT (Just-in-Time)
compiler, which enhances the speed of execution.
Distributed: Java is designed for the distributed environment
of the Internet because it handles TCP/IP protocols. Java
also supports Remote Method Invocation (RMI). This
feature enables a program to invoke methods across a network
Dynamic: Java support dynamic memory allocation. Due to
this memory wastage is reduced. This is done with the help of
new operator.
18
Java Features
Secure:
It is more secured language compare to other languages;
In this, program is covered into byte code after
compilation which is not readable by human
Restricted to access other areas in the system
Portable:
If any language supports platform independent and
architectural neutral features, then that language is
portable.
Portable = Platform independent + Architecture-neutral
19
Procedural/Structured Programming
Huge programs are divided into smaller
programs as functions. These functions can
call one another.
No importance is given to security of data
and importance is laid on doing things.
Data passes globally from function to
function
Most functions access global data
Follows top-to-bottom approach
20 Suresh Yadlapati
Procedural/Structured Programming
21 Suresh Yadlapati
Object Oriented
Programming
OOP has more importance to data rather than
doing things
Programs are divided into classes .
OOP allows bottom-up approach
New data items and member functions can be
comfortably added whenever essential.
Data is private and prevented from external
functions
Objects can communicate with each other
through methods
22 Suresh Yadlapati
Object Oriented
Programming
23 Suresh Yadlapati
Need of OOP
• Though procedural-oriented programs are
extremely powerful, they have some of the
following limitations:
It is difficult to add any new functionality or to upgrade a
program.
Functions have unrestricted access to global data.
Importance is given to the operation on data rather than the
data itself.
Need of OOP
• An object-oriented approach to the application
development enables us to design programs.
OOP has more importance to data rather than
doing things
Programs are divided into classes - Modularity .
OOP allows bottom-up approach
New data items and member functions can be
comfortably added whenever essential
Data is private and prevented from external
functions
Objects can communicate with each other
through member functions
Advantages of OOP
• Modular structure - provides a clear and modular
structure for program development.
• Easy to maintain and upgrade- Object-oriented
systems can be easily upgraded.
• Inheritance - Allows us to eliminate redundant code
and use existing classes.
• Reusability - functions and modules can be reused by
other users without any modification.
• Data hiding -The programmer can hide the data and
functions in a class from other classes.
Reusability
- functions Easy to Data
and maintain hiding -The
modules and programmer
can be upgrade- can hide the
reused by Object- data and
other users oriented functions in
without any systems can a class from
modification be easily other
. upgraded. classes.
Modular Inheritanc
structure - e - Allows us
provides a to eliminate
clear and redundant
modular code and
structure for use existing
program classes.
developmen
t.
Characteristics of Object-Oriented
Programming
Class
Object
Data Abstraction
Encapsulation and Data hiding
Inheritance
Polymorphism
Class
Object Properties Methods
car.name = car.start(
Verna )
car.model = car.drive(
2017 )
car.weight = car.brake
1250kg ()
• In object-oriented
programming, a class
car.color is a
= white car.stop()
basic building block.
• A class is a user-defined data type.
• It consists of data members and member
functions, which can be accessed and used by
creating an instance of that class
Object
It is a basic unit of OOP and represents the
real-world entities.
An Object is an instance of a Class. When a
class is defined, no memory is allocated but
when it is instantiated (i.e. an object is
created) memory is allocated.
An object has an identity(name),
state/properties, and behavior.
Each object contains data and code to
manipulate the data.
Data Abstraction
Coupling is the degree of inter-dependence between the
pairs of modules.
- It is a measure of the strength of association established
by a connection from one entity to another.
Mainly used for C++ is mainly used for system Java is mainly used for application
programming. programming. It is widely used in
Windows-based, web-based, enterprise,
and mobile applications.
Goto C++ supports the goto statement. Java doesn't support the goto statement.
Multiple inheritance C++ supports multiple Java doesn't support multiple
inheritance. inheritance through class. It can be
achieved by using interfaces in java.
Synchronized statement
Guarding statement – Exceptions - try, catch block and finally
Command line arguments
A Java application can accept any number
of arguments from the command line.
These arguments can be passed at the
time of running the program.
length property array is used to find
number of arguments
User input to programs
There are various ways to read input from the
keyboard, the java.util.Scanner class is one of
them.
The Java Scanner class breaks the input into
tokens using a delimiter that is whitespace by
default.
It provides many methods to read and parse
various primitive values.
Part of util package.
Programs discussed in text book:
Performing arithmetic operations on two numbers
Finding Area of a circle
Method Description
public String next() it returns the next token from the scanner.
public String nextLine() it reads the given input as one line irrespective
of white space and returns the value as a string.
public byte nextByte() it scans the next token as a byte.
Suresh Yadlapati
Escape sequences
These characters consists of a backslash
followed by a letter such as “\t”, “\n” and so
on and have special meaning to the
compiler.
These are used in formatting.
\u0041 (hexadecimal)- A
\ddd (Octal) - \132 - Z
scape Characters Description
\t It is used to insert a tab in the text at this point.
Ref: https://www.javatpoint.com/java-comments
•There is a non-data type called
Data Types in Java void.
• This type is used for methods that
Java defines eight primitive (basic) types of data do not return any value.
1. Integral types—byte, short, int, long
2. Floating point types—float, double
3. Character type—char
4. Boolean type—boolean
•
Declaration of Variables
• A variable is declared by first declaring its type
followed by its identifier or name.
• The name or identifier should not be a keyword of
Java language.
• It can be of any length and may consist of
characters and digits.
Non-primitive Types
These are the class and interface types. The name
of a class or interface is the name of type. A class
object is declared as
Integers
Integers are whole numbers, that is, they
represent numbers that do not have a fractional
part.
In Java, these numbers may be positive or
negative.
Characters
• A variable may have value in terms of a character.
• These characters represent integer values.
• Java supports Unicode for the representation of characters.
• Unicode supports all the character sets of all the major languages
of the world.
• The initial version of Unicode allocated 2 bytes for storing
characters.
• The range of values for characters in the initial version of Unicode
comprised from ‘\u0000’ to ‘\uffff ’ that is from 0 to 65535 both
end values inclusive.
‘A’ - \u0041 – 65
‘1’ - \u0031 - 49
Floating-point numbers
• The numbers that are not whole numbers, or those that have
fractional part, Examples are 3.141, 476.6, and so on.
• Java supports two types of such numbers.
Float: This type is used for single-precision decimal floating point
numbers, that is, 7(or 8) digits after the decimal point.
These numbers are stored on 4 bytes .
Double This type is used for double-precision decimal floating point
numbers, that is, 15 digits after the decimal point.
* Multiplication
Left to
4 / Division
right
% Modulus
+ Addition Left to
5
- Subtraction right
<< Bitwise left shift
>> Bitwise right shift with sign
Left to
6 >>> extension
right
Bitwise right shift with zero
extension
Precedence Operator Type Associativit
y
< less than
<= less than or equal
> greater than
7 Left to right
>= greater than equal
instanceof Type comparison
(objects only)
== Relational is equal to
8 != Relational is not equal Left to right
to
9 & Bitwise AND Left to right
10 ^ Bitwise exclusive OR Left to right
11 | Bitwise inclusive OR Left to right
12 && Logical AND Left to right
13 || Logical OR Left to right
14 ?: Ternary conditional Right to left
= Assignment
+= Addition assignment
-= Subtraction assignment
15 (lowest) *= Multiplication Right to left
/= assignment
%= Division assignment
Instance of Example –
Modulus assignment
UseofInstanceOf.java
Assignment Operator
• The associativity of = is from right to left.
• On the left of =, we write the name of the
variable to which a value is to be assigned, and
on its right, we write the value to be assigned.
• The name or identifier on the left is also called l-
value and the value on the right of = is called r-
value.
l-value(name of the identifier) = r-
value(identifier or value)
Chain of Assignments:
int a,b,c,d;
A=b=c=d=50;
Basic Arithmetic Operators
Ex:ArithmeticOperators.java
Combinational Assignment Operators
• In a compound assignment operator, the operations by an operator
and assignment are combined in a single operator. As for e.g.
int n = 8, m = 10;
n + = m;
Increment (++) and Decrement (−−) Operators
Ex: Bitwise_Operators.java
Bitwise Shift Operators
Syntax:
value <operator> <number_of_times>
The left side of the expression is the integer that is
shifted, and the right side of the expression denotes
the number of times that it has to be shifted.
Bitwise shift operators are further classified as
bitwise left and bitwise right shift operators.
Formatted output with printf() method
In JAVA, formatting of an output can be done in two
ways:
Formatter Class
printf() method
Formatting means:
Right and left justification
Procession of floating point numbers
Representing integers in octal and hexadecimal form
Representing time and data in different formats
Syntax:
System.out.printf(“Formatting String”, variables
separated with comma);
System.out.printf(“Price :%d”,22,”Rupees”);
Conversion characters for diff. types of
variables:
Type Variable Conversion Formatting String
letter
Integers d “%d”
• This group contains statements : while, do–while, for, and for– each.
The if statements are executed from the top down. As soon as one of
the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the ladder is bypassed. If none
of the conditions is true, then the final else statement will be executed.
Ternary Operator ?:
• Its code is as follows:
Expression1 ?Expression2 :Expression3
• The first expression is the test condition.
• If it evaluates true, the Expression2 is executed;
otherwise Expession3 is executed
Switch Statement
switch (expression) 1. For versions of Java prior to JDK
{ 7, expression must be of type
byte, short, int, char, or an
case value1:
enumeration.
// statement 2. Beginning with JDK 7,
sequence •The switch expression is
expression
break; can also be once.
evaluated of type String.
case value2: •The value of the expression
// statement
is compared with the values
sequence
break;
of each case.
•If there is a match, the
.
case valueN : associated block of code is
// statement executed.
sequence •The break and default key
break; words are optional
default:
// default statement
Nested Switch Statement
switch(count)
{
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is one");
break;
}
break;
case 2:
.
.
.
class WhileSt {
while public static void main(String
args[])
while(condition) {
int n = 10;
{
while(n > 0) {
// body of loop System.out.println("tick "
} + n);
n--;
}
}
}class DoWhileSt
{
do { public static void main(String args[]) {
int n = 10;
// body of loop do {
System.out.println("tick " + n);
}while(condition); n--;
} while(n > 0);
}
}
10
8
while Expression
• The while loop may be coded as:
while (test expression)
statement;
• If the while test expression evaluates true, the statement
following the expression is executed.
do while Loop
• This loop is similar to while loop except for the fact that the test
expression is placed at the end.
• The statement to be executed is placed between do and while
expression.
Code is:
do
Statement;
while (test expression);
do while Loop
for(initialization; condition;
incr/decr)
{
//code to be executed
}
11
4
Nested for loop
The for-each loop
The for-each loop is used to traverse array or collection in java.
It is easier to use than simple for loop because we don't need
to increment value and use subscript notation.
It works on elements basis not index. It returns element one by
one in the defined variable.
Syntax: public class ForEachExample
{
for(data_type var:array)
public static void main(String[]
{ args) {
//code to be executed
int arr[]={12,23,44,56,78};
}
for(int i:arr)
{
System.out.println(i);
}
}
}
11
6
Nested for-each for loop
public class NestedForEach
{
public static void main(String[] args)
{
String[][] str = {{"Amaravati","Vij","Gnt"},
{"Hyd","WL","Khammam"}};