1 of 85

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

2 of 85

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

3 of 85

Introduction to Java

  • Programming language and a platform
  • High level, robust, object-oriented and secure programming language
  • Developed by Sun Microsystems (now Oracle) in 1995
  • James Gosling is known as the father of Java
  • Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the Oak name to Java

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

4 of 85

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

5 of 85

Features of Java

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

6 of 85

JAVA Compiler and Interpreter

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

7 of 85

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

8 of 85

Java is Object Oriented

  • Since it is an object-oriented language, it will support the following features:
    • Class
    • Object
    • Encapsulation
    • Abstraction
    • Inheritance
    • Polymorphism

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

9 of 85

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

10 of 85

Java is Platform Independent

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

11 of 85

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

12 of 85

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

13 of 85

Java is Secure

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

14 of 85

Java is Secure

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

15 of 85

Java is Robust

  • Capable of handling run-time errors,
  • Supports automatic garbage collection
  • Exception handling, and
  • Avoids explicit pointer concept.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

16 of 85

  • Multithreaded means handling multiple tasks simultaneously or executing multiple portions (functions) of the same program in parallel.
  • The code of java is divided into smaller parts and Java executes them in a sequential and timely manner.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

17 of 85

Java

is

Distributed

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

18 of 85

Java is Distributed

  • Multiple programmers at many locations to work together on a single project.
  • Support RMI (Remote Method Invocation) and EJB (Enterprise JavaBeans).
  • Extensive library of classes for interacting, using TCP/IP protocols such as HTTP and FTP, which makes creating network connections much easier than in C/C++.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

19 of 85

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

20 of 85

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

21 of 85

JAVA: Introduction to Array Data Type

  • Arrays in Java are homogeneous data structures implemented in Java as objects.
  • Arrays store one or more values of a specific data type and provide indexed access to store the same.
  • A specific element in an array is accessed by its index.
  • Arrays offer a convenient means of grouping related information.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

22 of 85

JAVA: Introduction to Array Data Type

  • Obtaining an array is a two-step process.
    • First, you must declare a variable of the desired array type
    • Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

23 of 85

JAVA: General Form of Java Array Initialization

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

24 of 85

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

25 of 85

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

26 of 85

JAVA: Multidimensional Array

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

27 of 85

JAVA: Multidimensional Array -Conceptually

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

28 of 85

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

29 of 85

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

30 of 85

String Handling in Java

Creating String in Java

There are two ways to create a String in Java

  • String literal
  • Using “new” keyword

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

31 of 85

java.lang.String API – Important methods

31

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

32 of 85

java.lang.String API – Important methods

32

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

33 of 85

java.lang.String API – Examples

33

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

34 of 85

java.lang.String API – Examples

34

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

35 of 85

Converting String to numbers and vice versa

Converting String to numbers and vice versa

  • String to Number
    • int i = Integer.parseInt(str);
    • Integer i = Integer.valueOf(str);
    • double d = Double.parseDouble(str);
    • Double d = Double.valueOf(str);

Note: Both throw NumberFormatException If the String is not valid for conversion

  • String to Boolean
    • boolean b = Boolean.parseBoolean(str);
  • Any Type to String
    • String s = String.valueOf(value);

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

36 of 85

Classes and Method

  • Core of Java.
  • Logical construct upon which the entire Java language is built
  • Defines the shape and nature of and object.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

37 of 85

Class Fundamentals

  • A class is that it defines a new data type.
  • Once defined, this new type can be used to create objects of that type.
  • A class is a template for an object, and an object is an instance of a class. Because an object is an instance of a class
  • Two word object and instance used interchangeably.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

38 of 85

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

39 of 85

A Simple Class

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

40 of 85

Declaring Objects

  • Obtaining objects of a class is a two-step process.
  • First, you must declare a variable of the class type. This variable does not define an object. Instead,it is simply a variable that can refer to an object.
  • Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

41 of 85

Declaring Objects

  • The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it.
  • This reference is, more or less, the address in memory of the object allocated by new
  • This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

42 of 85

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

43 of 85

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

44 of 85

A Closer Look at new

  • The new operator dynamically allocates (that is, allocates at run time) memory for an object.
  • It has this general form:

class-var = new classname ( );

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

45 of 85

Assigning Object Reference Variables

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

46 of 85

Introducing Methods

general form of a method:

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

47 of 85

Adding a Method to the Class

  • In fact, methods define the interface to most classes. This allows the class implementor to hide the specific layout of internal data structures behind cleaner method abstractions.
  • In addition to defining methods that provide access to data,you can also define methods that are used internally by the class itself.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

48 of 85

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

49 of 85

Returning a Value

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

50 of 85

Constructor

  • A constructor initializes an object immediately upon creation.
  • It has the same name as the class in which it resides and is syntactically similar to a method.
  • Once defined, the constructor is automatically called when the object is created, before the new operator completes.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

51 of 85

Constructor

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

52 of 85

Constructor

  • The default constructor automatically initializes all instance variables to their default values,
    • which are zero for numeric types,
    • null for reference types ,
    • and false for boolean
  • The default constructor is often sufficient for simple classes, but it usually won’t do for more sophisticated ones.
  • Once you define your own constructor, the default constructor is no longer used.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

53 of 85

Parameterized Constructor

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

54 of 85

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

55 of 85

Garbage Collection

  • It is automatic deallocation.
  • when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.
  • There is no need to explicitly destroy objects in Java as done in C++.
  • It will not occur simply because one or more objects exist that are no longer used.
  • Different Java run-time implementations will take varying approaches to garbage collection (but for the most part, you should not have to think about it while writing your programs)

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

56 of 85

The finalize( ) Method

  • Sometimes an object will need to perform some action when it is destroyed.
  • For example, if an object is holding some non-Java resource such as a file handle or character font, then you might want to make sure these resources are freed before an object is destroyed.
  • To handle such situations, Java provides a mechanism called finalization
  • You can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

57 of 85

The finalize( ) Method

  • To add a finalizer to a class, you simply define the finalize( ) Method.
  • The Java runtime calls that method whenever it is about to recycle an object of that class.
  • Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

58 of 85

Polymorphism in Java

  • The process of representing one form in multiple forms is known as Polymorphism.
  • Polymorphism is derived from 2 greek words: poly and morphs.
  • The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

59 of 85

Polymorphism in Java

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

60 of 85

Polymorphism in Java

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

61 of 85

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

62 of 85

Method Overloading

  • In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different
  • When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call
  • The return type alone is insufficient to distinguish two versions of a method.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

63 of 85

Method Overloading

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

64 of 85

Method Overloading

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

65 of 85

Constructor Overloading

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

66 of 85

Constructor Overloading

  • Java Constructor overloading is a technique in which a class can have any number of constructors that differ in parameter list.
  • The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

67 of 85

Copy constructor

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

68 of 85

A closer look at argument Passing

There are two ways that a computer language can pass an argument to a subroutine

  • Call by value
  • Call by reference

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

69 of 85

A closer look at argument Passing

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

70 of 85

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

71 of 85

Introducing Access Control

  • Java access specifiers are public, private, and protected
  • protected applies only when inheritance is involved
  • When a member of a class is modified by the public specifier, then that member can be accessed by any other code
  • When a member of a class is specified as private, then that member can only be accessed by other members of its class

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

72 of 85

Introducing Access Control

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

73 of 85

Understanding static

  • When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object
  • The most common example of a static member is main( )
  • main( ) is declared as static because it must be called before any objects exist
  • Instance variables declared as static are, essentially, global variables

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

74 of 85

Methods declared as static have several restrictions:

  • They can only call other static methods
  • They must only access static data
  • They cannot refer to this or super in any way
  • We can declare a static block which gets executed exactly once, when the class is first loaded

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

75 of 85

Introducing final

  • A variable can be declared as final
  • Doing so prevents its contents from being modified
  • We must initialize a final variable when it is declared
  • final int FILE_NEW = 1;
  • final int FILE_OPEN = 2;

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

76 of 85

Introducing final

  • Variables declared as final do not occupy memory on a per-instance basis
  • The keyword final can also be applied to methods, but its meaning is substantially different than when it is applied to variables

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

77 of 85

Introducing Nested and Inner Classes

  • It is possible to define a class within another class
  • The scope of a nested class is bounded by the scope of its enclosing class
  • If class B is defined within class A, then B is known to A, but not outside of A
  • A nested class has access to the members, including private members, of the class in which it is nested

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

78 of 85

Introducing Nested and Inner Classes

  • However, the enclosing class does not have access to the members of the nested class
  • There are two types of nested classes: static and non-static
  • A static nested class is one which has the static modifier applied
  • static innerclass must access its enclosing class by creating an object.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

79 of 85

Introducing Nested and Inner Classes

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

80 of 85

Introducing Nested and Inner Classes

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

81 of 85

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

82 of 85

Command Line Argument

  • The java command-line argument is an argument i.e. passed at the time of running the java program.
  • The arguments passed from the console can be received in the java program and it can be used as an input.
  • So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

83 of 85

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

84 of 85

Command Line Argument

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

85 of 85

End of Unit

Thank you!!!

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering