SE Computer - Division A
Course Name : Principles of Programming Language
Course Code: 210256
Course InCharge: Parag Achaliya
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Unit 3
Java as Object Oriented Programming Language-Overview
Fundamentals of JAVA, Arrays: one dimensional array, multi-dimensional array, alternative array declaration statements, String Handling: String class methods
Classes and Methods: class fundamentals, declaring objects, assigning object reference variables, adding methods to a class, returning a value, constructors, this keyword, garbage collection, finalize() method,
overloading methods, argument passing, object as parameter, returning objects, access control, static, final, nested and inner classes, command line arguments, variable -length arguments.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introduction to Java
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java Version History
Evolution
Of
Java
JDK Alpha & Beta
JDK 1.0
JDK 1.1
J2SE 1.2
J2SE 1.3
J2SE 1.4
J2SE 5.0
Java SE 6
Java SE 7
Java SE 8
Java SE 9
1995
1996
1997
1998
2000
2002
2004
2006
2011
2014
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Features of Java
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA Compiler and Interpreter
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java Life Cycle
Java Programs Normally Undergo Four Phases
Edit
Compile
Load
Execute
Programmer
Writes program
Compiler creates
Byte-codes from program
Class loader stores
Byte-codes in memory
Translate byte codes
Into machine language
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java is Object Oriented
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Is Java Purely
Object Oriented?
Yes Then why?
No Then Why?
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java is Platform Independent
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Difference between JDK, JRE and JVM
Java Development Kit
It physically exists. It contains JRE + development tools.
Java Runtime Environment
JRE is used to provide runtime environment. It is the implementation of JVM. It physically exists.
Java Virtual Machine
JVM is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
JDK
JRE
JVM
JDK is a software development kit whereas JRE is a software bundle that allows Java program to run, whereas JVM is an environment for executing bytecode.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java is Secure
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java is Secure
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java is Robust
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java
is
Distributed
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java is Distributed
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Java Editions
Java 2 Standard Edition
Java standard edition is use to develop client-side standalone applications or applets
Java 2 Micro Edition
Java micro edition is use to develop applications for mobile devices such as cell phones
Java 2 Enterprise Edition
Java enterprise edition is use to develop server-side applications such as Java servlets and Java Server Pages
J2SE
J2ME
J2EE
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
First program using JAVA
public class HelloSNJBians
{
Public static void main(String[] args)
{
System.out.print(“Hello SNJBians”);
}
}
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA: Introduction to Array Data Type
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA: Introduction to Array Data Type
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA: General Form of Java Array Initialization
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA: Implementing an Array
class MyArray{
public static void main(String args[]){
int month_days[ ] = {31,28,31,30,31,30,31,30,31,30,31};
System.out.println("April has " + month_days[3] + days.");
}
}
OUTPUT
April has 30days
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA: Multidimensional Array
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA: Multidimensional Array -Conceptually
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
class TwoDArray
{
//-----------------------------------------------------------------
// Creates a 2D array of integers, fills it with increasing
// integer values, then prints them out.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int[][] multarry = new int[4][5];
int i,j,k=0;
// Load the table with values
for (i=0; i < 4;i++)
for (j=0; j < 5; j++)
{
multarry[i][j]=k;
k++;
}
// Print the table
for (i=0; i < 4;i++)
{
for (j=0; j < 5; j++)
{
System.out.print( multarry[i][j]+" ");
}
System.out.println();
}
}
}
OUTPUT
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
JAVA: Passing Java Array to a Method
class PMethods{
public static void display(int y[])
{
System.out.println(y[0]);
System.out.println(y[1]);
System.out.println(y[2]);
}
public static void main(String args[])
{
int x[] = { 1, 2, 3 };
display(x); //Passed array x to method display
}
}
OUTPUT
1
2
3
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
String Handling in Java
Creating String in Java
There are two ways to create a String in Java
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
java.lang.String API – Important methods
31
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
java.lang.String API – Important methods
32
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
java.lang.String API – Examples
33
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
java.lang.String API – Examples
34
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Converting String to numbers and vice versa
Converting String to numbers and vice versa
Note: Both throw NumberFormatException If the String is not valid for conversion
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Classes and Method
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Class Fundamentals
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
General Form of a Class
The data that it contains and the code that operates on that data
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
A Simple Class
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Declaring Objects
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Declaring Objects
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
A Closer Look at new
class-var = new classname ( );
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Assigning Object Reference Variables
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing Methods
general form of a method:
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Adding a Method to the Class
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Returning a Value
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Constructor
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Constructor
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Constructor
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Parameterized Constructor
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
The this Keyword
The this Keyword and Instance Variable Hiding
This can be used inside any method to refer to the current object.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Garbage Collection
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
The finalize( ) Method
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
The finalize( ) Method
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Polymorphism in Java
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Polymorphism in Java
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Polymorphism in Java
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Method Overloading
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Method Overloading
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Method Overloading
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Constructor Overloading
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Constructor Overloading
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Copy constructor
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
A closer look at argument Passing
There are two ways that a computer language can pass an argument to a subroutine
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
A closer look at argument Passing
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
A closer look at argument Passing
Call by Value : When a primitive type is passed to a method
Call by Reference : objects are implicitly passed to a method
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing Access Control
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing Access Control
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Understanding static
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Methods declared as static have several restrictions:
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing final
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing final
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing Nested and Inner Classes
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing Nested and Inner Classes
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing Nested and Inner Classes
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introducing Nested and Inner Classes
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Example of Nested Class
class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){System.out.println("data is "+data);} /)msg() complete
} // Inner class Complete
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
TestMemberOuter1.Inner in=obj.new Inner();
in.msg();
}
}
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Command Line Argument
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Steps to Run Command Line Argument
Save the program as Hello.java
Open the command prompt window and compile the program- javac Hello.java
After a successful compilation of the program, run the following command by writing the arguments- java Hello
For example – java Hello Geeks at GeeksforGeeks
Press Enter and you will get the desired output.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Command Line Argument
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
End of Unit
Thank you!!!
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering