0% found this document useful (0 votes)
130 views21 pages

Chopra Rajiv Object Oriented Programming Using Scala and Java

Uploaded by

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

Chopra Rajiv Object Oriented Programming Using Scala and Java

Uploaded by

2122ba18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

NEW AGE

A I C T E
OBJECT ORIENTED
PROGRAMMING
( USING SCALA AND JAVA )

RAJIV CHOPRA
Get free access to e-books for 3 Months
Best Selling Textbooks in COMPUTER SCIENCE ENGINEERING
by
Renowned Authors from Prestigious Publishers

PHI
... & many more

Compliments from to all Faculty Members


already LIVE @ Download the App to access e-books:

Indian Institute of Technology Kanpur, Uttar Pradesh

Visvesvaraya Technological University (Consortium), Belgaum, Karnataka

National Institute of Technology, Kurukshetra, Haryana

Punjab Engineering College, Chandigarh

Rajiv Gandhi Proudyogiki Vishwavidyalaya (Consortium), Bhopal, Madhya Pradesh

Bennett University (Times of India Group), Greater Noida, Uttar Pradesh

Jamia Millia Islamia - A Central University, New Delhi Username: COMP2021


.... & MANY OTHER REPUTED UNIVERSITIES Password: COMP2021

CONTACT US TO KEEP LEARNING!


Ms. Atika KhannaMr. Ranjan RoyMr. Manish Gupta
9999709935 93159052989315905295
OBJECT ORIENTED
PROGRAMMING
( USING SCALA AND JAVA )

RAJIV CHOPRA
Associate Professor
Department of Computer Science, GTBIT
GGSIPU, Delhi

AICTE

NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS


LONDON • NEW DELHI

Click Here to Buy the Book Online


Copyright © 2022, New Age International (P) Ltd., Publishers
Published by New Age International (P) Ltd., Publishers
First Edition: 2022

All rights reserved.


No part of this book may be reproduced in any form, by photostat, microfilm, xerography, or any other means, or
incorporated into any information retrieval system, electronic or mechanical, without the written permission of
the publisher.

GLOBAL OFFICES
• New Delhi NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS
7/30 A, Daryaganj, New Delhi-110002, (INDIA), Tel.: (011) 23253472, 23253771
E-mail: [email protected] • Visit us at www.newagepublishers.com

• London NEW AGE INTERNATIONAL (UK) LTD.


27 Old Gloucester Street, London, WC1N 3AX, UK
E-mail: [email protected] • Visit us at www.newacademicscience.co.uk
BRANCHES
• Bangalore 37/10, 8th Cross (Near Hanuman Temple), Azad Nagar, Chamarajpet, Bangalore- 560 018
Tel.: (080) 26756823, E-mail: [email protected]

• Chennai 26, Damodaran Street, T. Nagar, Chennai-600 017, Tel.: (044) 24353401
E-mail: [email protected]

• Cochin CC-39/1016, Carrier Station Road, Ernakulam South, Cochin-682 016


Tel.: (0484) 4051304, E-mail: [email protected]

• Guwahati Hemsen Complex, Mohd. Shah Road, Paltan Bazar, Near Starline Hotel
Guwahati-781 008, Tel.: (0361) 2513881, E-mail: [email protected]

• Hyderabad 105, 1st Floor, Madhiray Kaveri Tower, 3-2-19, Azam Jahi Road, Near Kumar Theater
Nimboliadda Kachiguda, Hyderabad-500 027, Tel.: (040) 24652456
E-mail: [email protected]

• Kolkata RDB Chambers (Formerly Lotus Cinema) 106A, 1st Floor, S N Banerjee Road
Kolkata-700 014, Tel.: (033) 22273773, E-mail: [email protected]

• Mumbai 142C, Victor House, Ground Floor, N.M. Joshi Marg, Lower Parel, Mumbai-400 013
Tel.: (022) 24927869, 24915415, E-mail: [email protected]

• New Delhi 22, Golden House, Daryaganj, New Delhi-110 002, Tel.: (011) 23262368, 23262370
E-mail: [email protected]

ISBN: 978-93-89802-20-7
C-21-09-12826

Printed in India at Glorious Printers, New Delhi.


Typeset at Satyam Art, New Delhi.

NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS


7/30 A, Daryaganj, New Delhi - 110002
Visit us at www.newagepublishers.com
(CIN: U74899DL1966PTC004618)
Preface

I am contended to endorse this preface in favor of my book on “Object Oriented Programming


(using SCALA and JAVA)” as per the new syllabus of AICTE, New Delhi. Being an Associate
Professor in the Department of Computer Science and Engineering at GTBIT, GGSIPU Delhi
had ample opportunities to gauge the potential of such a book on Object-Oriented Programming
(OOP) that has an integrated approach. Through this book, I would like to elucidate a few
facts about OOP that impressed me the most. Right from conceptualizing the idea of the book,
the author demonstrates great articulation and creativity. Through the presentation of the same
he evinced his prowess in research.
In today’s era of instant information gratification, we have ready access to opinions,
rationalizations and superficial descriptions. Much harder to come by is the foundational
knowledge that informs a principled understanding of the world. Essential knowledge books
fill that need. This book synthesizes specialized subject matter for non-specialists and engaging
critical topics through fundamentals. This book is not purely descriptive and definitional but
it also follows a flowchart and algorithmic approach. I have attempted to present the technical
material in an accessible way. From my vast teaching experience of over 20 years, I have found
that for technical topics the most accessible presentation is to explain the fundamentals in a
step-by-step manner.
The hallmark of human intelligence is the capacity to learn. It is no secret that creating
robust and manageable programs is what this book aims at. This can involve anything ranging
from finding the target audience to the identification of consumer needs, the value proposition,
product testing etc. Businesses can harness the transformation capabilities of Object-oriented
programming languages to this end via SCALA and JAVA2.
There are plenty of books on this subject in the market, so thanks again for choosing this
one! Every effort was made to ensure that it is full of much useful information as much as
possible, please enjoy!

Dr. Rajiv Chopra


Contents


Preface v

Acknowledgements vii


Syllabus viii

1. Introduction to Java.........................................................................................................1–25

1.0 Introduction 1
1.1 Abstract Data Types [ADTs] and their Specifications 1
1.2 How to Implement Abstract Data Types [ADTs]? 2
1.3 Concrete State Space and Concrete Invariant 7
1.4 Abstraction Function 7
1.5 Java History 7
1.6 How Java Works? 8
1.7 Java Virtual Machine (JVM) 11
1.8 Just In Time (JIT) Compiler 13
1.9 Java Features 13
1.10Java Tools 15
1.11Using Java with Other Tools 16
1.12Native Codes 21
1.13Java Application Types 22
1.14Java Comparisons with C and C++ 23
Exercise Questions 25
2. Java Fundamentals.......................................................................................................26–192

2.0 Introduction 26
2.1 My First Java Program—An Anatomy 26
2.2 Identifiers, Variables and Keywords 29
2.3 Data Types (in Java) 30
2.4 Literals (Constants) 34
2.5 Escape Sequences 37
2.6 Variables 38
2.7 Operators 42
2.8 String Concatenation 59
2.9 Operator Precedence and Associativity 61
x Contents

2.10 Type Casting (or Coercion) in Java 63


2.11 Control Flow Statements 71
2.12 Arrays 137
2.13 Strings 176
Summary 192
3. Java Classes.................................................................................................................193–276

3.0 Introduction 193


3.1 Objects and Classes 193
3.2 Inheritance 218
3.3 Encapsulation and Polymorphism 227
3.4 Constructors, Finalizers and Destructors 230
3.5 Garbage Collection 247
3.6 Java’s Modifiers—Static and Final 252
3.7 The Final Keyword 256
3.8 Abstract Class 258
3.9 This Reference 268
3.10 Wrapper Classes 270
Summary 276
4. Interfaces and Packages.............................................................................................277–310

4.0 Introduction 277


4.1 Interfaces 277
4.2 Packages 296
4.3 Java API Packages 301
Summary 310
5. Exception Handling.................................................................................................... 311–330

5.0 Introduction 311


5.1 Java’s Common Exceptions 314
5.2 Methods of Exception Handling 315
5.3 Creating your own Exception Classes 323
Summary 330
6. Managing I/O Streams and Stream Files..................................................................331–345

6.0 Introduction 331


6.1 Java Streams 331
6.2 Byte Stream Classes 332
6.3 Character Stream Classes 334
6.4 Reading from Keyboard 336
6.5 File Stream Classes 339
Summary 345
Contents xi

7. Threading and Multithreading.................................................................................. 346–367

7.0 Introduction 346


7.1 Creating and Running a Thread 347
7.2 The Thread Control Methods 351
7.3 The Thread Life Cycle 354
7.4 Thread Priorities 357
7.5 Thread Synchronization 359
Summary 367
8. Applets and Event Handling...................................................................................... 368–405
8.0 Introduction 368
8.1 Java Applets Versus Java Applications 368
8.2 Creating Applets 369
8.3 Life Cycle of an Applet 376
8.4 Java Event Handling 381
8.5 Windows Event 384
8.6 Keyboard and Events 389
8.7 Using AWT in Applications—Containers 391
8.8 AWT Components—Labels, Buttons, Checkboxes 391
Summary 405
9. Animation and Images................................................................................................ 406–437
9.0 Introduction 406
9.1 Java Basic Drawing Tools—The Graphics Class 406
9.2 Drawing Lines 407
9.3 Drawing Rectangles 409
9.4 Drawing Polygons 413
9.5 Drawing Ovals 414
9.6 Drawing Arcs 416
9.7 Color Models 419
9.8 Rendering Text 420
9.9 Java Images 427
9.10 Image Processing 427
9.11 Image Filtering 432
Summary 436
10. JDBC-ODBC...............................................................................................................438–456
10.0 Introduction 438
10.1 DBMS 438
10.2 ODBC 439
10.3 JDBC 439
10.4 ODBC Driver 445
10.5 Connectivity of Java with Database using JDBC: ODBC Bridge 445
10.6 Java.sql Package 449
Summary 456
xii Contents

11. Graphical Programming with SCALA, Swing and SDLC...................................... 457–467


11.0 Introduction 457
11.1 My First Swing Program 457
11.2 Panels and Layouts in Scala 459
11.3 Events Handling in Scala 460
Summary 463
Appendix A: Multiple Choice Questions (with Answers) ............................................. 468–483

Appendix B: Viva Voce Questions.................................................................................484–517
Appendix C: Lab Experiments......................................................................................518–580

Appendix D: Java Projects........................................................................................... 581–704

Appendix E: References .......................................................................................................705

Index.............................................................................................................................. 707–712
1
Chapter

Introduction to Java

1.0 INTRODUCTION

Computers can perform different jobs as they are programmable. This important job of
programming is done by the programmers. It is an art as well as a science. It is also a science
because programs do not work properly the first time as they are written. It needs a lot of
analyzing (its space and time complexity), experimenting (with dry runs), redesigning, if
needed. The programs should be functional, efficient, powerful and easy to use. This adds
to its quality. The solution to any problem is written in form of a program now. A program is a
set of instructions written in a programming language. A programming language provides
a medium for conveying the instructions to the computer. A program transforms the inputs to
the outputs. To solve a problem, either bottom-up approach or top-down approach is followed.
In bottom-up approach or working backward approach the problem can be solved by starting
from the Goal state to the Start state. In top-down approach or the problem decomposition
technique, however, a problem is decomposed into smaller units over and over again until each
smaller unit is manageable. Please note that a complex problem is simplified and solved by
decomposing it into many simple problems. And then finding sub-solutions for all of these
sub-problems and finally combining these sub-solutions to get the solution as a whole.
This is known as divide-and-conquer strategy. This approach of program development is
also known as stepwise refinement or modular decomposition or structured approach or
algorithmic approach.
Ada Lovelace is considered to be the first computer programmer.
JAVA is a new object-oriented programming language that was developed by James Gosling
and his team at Sun Microsystems in California. The language was based on C and C++. The
language was first called as Oak, named after the oak tree outside of Gosling’s office. But
this name was already taken by someone so the team renamed it as JAVA. Also note that JAVA
is known as a web programming language because of its use in writing programs called as
applets (to be discussed later) that run within a web browser. Even complete applications
in Java can be written as we write in other programming languages. Many other object-
oriented programming languages also exist like C++, Simula, SmallTalk, ADA etc.

1.1 ABSTRACT DATA TYPES [ADTS] AND THEIR SPECIFICATIONS

If we have to sort a set of numbers then we need an abstract model (an array, a sorting
algorithm) of a data structure. To develop a new model, we need to define abstract objects
2 Object Oriented Programming—Using Scala and Java

plus operations of them. Before we discuss further just keep the following points in mind
regarding ADTs: -
1. ADT is a mathematical model.
2. A set of values and a collection of operations on those values are defined on the model.
For example, int datatype in C is an implementation of ADT → INTEGER-ADT.
INTEGER-ADT Specifies two things—
(a) Range of numbers that will be represented.
(b) Operations that can be performed on integer numbers like addition, subtraction,
multiplication etc.
3. When we use ADTs, our program divides into two parts—
(a) Application part.
(b) Implementation part.
Where Application part –describes uses of ADT.
Implementation part- implements ADT.
4. ADT is a useful tool for specifying the logical properties of a datatype.
5. Data type =

6. ADT refers to basic mathematical concept that defines the data type.
7. ADTs are generalizations of basic (primitive) data types like integer, float etc. just as
procedures are generalizations of basic operations (additions, subtraction etc.).
8. Almost all programming languages support basic /primitive data types but they are not
sufficient. Java also supports a derived data type called as a class.
9. But by just defining a class type, the operations applicable to the objects of that type
cannot be specified. So, the concept of ADT is necessary.
10. Therefore, we can say that
Data type = permitted data values + operations
Data structure = organized data + allowed operations.

1.2 HOW TO IMPLEMENT ABSTRACT DATA TYPES [ADTS]?

An ADT is a generalized description of a “concrete” data type such as int or string. The
flow diagram of ADT usage is as follows: -
From ADT → interface → class → objects
Introduction to Java 3

For example, for a Bag, its ADT definition will be

A bag is a collection of objects.

void add (Object object)


Post Condition: given object is in bag.

boolean contains (Object object)


Post Condition: this bag is unchanged.

object getFirst ( ) Is an ADT for


Returns: an element of this bag. a bag

Object getNext( )
Returns: some element of this bag.
Post Condition: this bag is unchanged.

boolean remove(Object object)


Returns: true iff this bag was changed.

integer size( )
Post Condition: this bag is unchanged.
Return: number of elements in this bag.

This is an ADT for a Bag. It is different from an abstract class of JAVA as abstract class is
the one that has at least one abstract method. It is like a partial interface.
As per the flow diagram above, this ADT is now translated into a JAVA interface (just as an
algorithm is translated into a JAVA method). Hence, JAVA interface for Bag ADT is as follows:
public interface Bag {
public void add (Object object);
public boolean contains(Object object);
public Object getFirst( );
public Object getNext( );
public boolean remove (object, object);
public int size( );
}
Next we see its implementation in terms of classes.
//ArrayBag class implements Bag interface given above.

public class ArrayBag implements Bag


{
private Object[ ] objects = new Objects[1000];
private int size, i;
public void add (Object object)
{
4 Object Oriented Programming—Using Scala and Java

objects [size ++] = object;


}
public boolean contains (Object object)
{
for (int i = 0; i < size; i++)
if(objects[i] == object)
return true;
return false;
}
public object getFirst( )
{
i = 0;
return objects [i++];
}
………………
………………
public int size( )
{
return size;
}
}
Final step is to create objects of this class like
Bag bag = new ArrayBag( );
bag.add (“MERCURY LEARNING”);
bag.add (“USA”);
…………………….
…………………….

What are Lists?


Java Collection Framework (JCF) is a group of classes and interfaces in java.util.package.
Collection means an object that contains other objects i.e. elements.
Four main types of JCF are-
(a) Lists.
(b) Queue.
(c) Set.
Introduction to Java 5

(d) Map.
Object

Abstract Collection …………………………….Collection


Abstract List………………..List

ArrayList
Vector
Abstract Queue…………….Queue

Abstract Set…………………Set

Abstract Map…………………Map
Fig. 1.1 (a)
Prefix ‘Abstract’ shows that they include abstract methods that are implemented by their
subclasses. A list is a collection of elements that are accessible sequentially. The List interface
in JCF (as shown in fig. 1.1(a), adds 10 methods to 15 methods specified by the Collection
interface that it extends. So, all of the List, Queue, Deque and Set classes implement the List
interface. Now, we can Test a List class.
public class TestStringList
{
public static void main(String [ ] args)
{
List<String> list = new ArrayList <string> ();
Collections. addAll (list, “GB”, “DE”, “FR”, “ES”);
System.out.println (list);
list.add (3, “DE”);
System.out.println(list);
System.out.println (“list.get(3): “ + list.get(3));
System.out.println (“list.indexof: “ + list.indexof(“DE”));
System.out.println (“list.sublist: “ + list.sublist(1,5));
list.remove (“DE”);
System.out.println (list);
}
}

OUTPUTS:
0 1 2 3
[GD, DE, FR, ES]
[GB, DE, FR, DE, ES]
0 1 2 3 4
6 Object Oriented Programming—Using Scala and Java

list.get (3) : DE
list.indexOf (“DE”) : 1 (returns 1st occurrence index)
list.get (IE) : -1 (not in list)
list.sublist (1, 5) =[DE, FR, DE, ES] (starting from 1st index up to 4th position i.e. < 5 elements)
list.remove (“DE”) = [GB, FR, DE, ES] (removes 1st occurrence of DE)
Even iterators can be used on these lists by creating an object (say “it”) of ListIterator class
of JFC like,
it. nextIndex ( ); //returns index of elem that will be returned by subsequent call
it. next ( ); //return next element in list
it. previousIndex ( );
it. previous ( ); //return prev elem in list
it. add (E e ); //inserts specified element into list
it. hasNext ( ); //returns true if list has more elements
it. remove ( ); //removes from list the last element
it. set (E e ); // replaces last element returned by next
We are in a position to solve an example now.
Example 1
Write an ADT to maintain a counter of number of persons arriving at the shopping mall.
Show how all steps are followed to get classes and objects.
Solution:
The following are the steps followed: -
Step-1: Create an ADT-counter, as follows-
void reset (int counter)
Post Condition: counter set to 0
void incre (object counter)
Post Condition: counter = counter + 1
void getval (int counter)
Post Condition: get current value of counter – no change in its value.
Step-2: Transform this ADT to interface now.
public interface count
{
void reset (int counter);
void incre (object counter);
void getval (int counter);
}
Introduction to Java 7

Step-3: Transform this interface to class now.


public counter implements count
{
//body also appears now
}
Step-4: Finally, objects are created.
counter c = new counter ( );
………………..
……………….
c. method-1( );
c. method-2( );
Hence, we can say that first of all ADT is created, then it is transformed to an interface,
which is then transformed into a class and finally the objects are created. This is shown in
steps-1 to step-4 above.

1.3 CONCRETE STATE SPACE AND CONCRETE INVARIANT


A concrete class is a type of class that has methods defined in it. Otherwise, the class is an
empty class/stub. For a given problem we have many solutions. This forms a state space. Now
instead of trying to symbolically analyze the whole state space, such an application could be
executed in concrete mode until a certain condition. It is basically concrete state space exploration
with matching on abstract states. A concrete model may be built which can be further used to
design and test it.

1.4 ABSTRACTION FUNCTION


The following points may be remembered regarding abstraction function: -
1. It separates interface and implementation.
2. It provides access to a specific part of data.
3. It gives coherent picture of what the user wants. High cohesion is achieved by means
of good abstraction.
4. Abstraction is defined as a datatype called as a class which separates interface from
implementation.
5. The ability to encapsulate and isolate design from execution information is known as
abstraction.

1.5 JAVA HISTORY


Several versions of Java exist today ever since Sun Microsystems released the first version of
Java i.e. JAVA 1.0. This was followed by an updated version, JAVA 1.1. When Sun released the
next version, Java 1.2, they changed the name of the language to JAVA 2. Please remember
8 Object Oriented Programming—Using Scala and Java

that 2 is part of the name and not the version number. So that version of the language was
known as Java 2 version 1.2. The next version was Java 2 version 1.3. The current version
is JDK 15, released on 15th Sept. 2020.
Let us show the history of JAVA in a tabular form.
Table 1.1: History of Java
Year Features and Versions of JAVA
1990 Sun Microsystems started Stealth project supporting applications of computers in the
consumer electronics market.
1991 Green project started with the members of Stealth project such as James Gosling, Patrick
Naughton and Mike Sheridan. New programming language called as Oak was created by
Gosling.
1992 An intelligent remote control called StarSeven was delivered. The Green project was
incorporated under the name FirstPerson.
1993 Mosaic web browser was introduced in the field of Internet.
1994 HotJava web browser was introduced by Sun Microsystems.
1995 Oak was renamed as Java.
1996 Sun released the first version 1.0 of JAVA with core features like I/O, utilities, network
programming, and user interfaces like AWT, Applets and multithreading introduced.
1997 Sun released JDK 1.1 with new features like inner classes, JavaBeans, JDBC (Java Database
Connectivity) and RMI (Remote Method Invocation).
1998 JAVA 2 platform, Standard Edition (J2SE) 1.2, code named as Playground, was released. It
replaced JDK and JVM was equipped with JIT (Just-in-Time) compiler.
2000 J2SE 1.3, code named as Kestral, was released with key features like JavaSound APIs like
audio playback, recording etc.
2002 J2SE 1.4, code named as Merlin, was released with key features like integrated XML parser,
image I/O etc.
2004 J2SE 5.0 , code named as Tiger, was released with new features like enhanced for-loop,
autoboxing/unboxing, enums, etc.
2006 JAVA SE 6.0, code named as Mustang, was released with new features like swing, JDBC
4.0 support etc.
2008 Java SE 7.0, code named as Dolphin, with native support for XML with its role in Web services.
2020 JDK 15

Unlike most other software systems that usually settle into a pattern of small, incremental
improvements, JAVA continued to evolve at an explosive space.

1.6 HOW JAVA WORKS?


JAVA is an object-oriented programming language similar to C++. It has been, however, simplified
to eliminate language features that cause common programming errors. The source code is
developed in either a plain text editor like a Notepad or Wordpad or an Integrated Development
Environment (IDE) like Netbeans IDE 7.0, JAVA creator or Gel or jEdit. Please note that
Introduction to Java 9

a java source file has .java extension similar to a word file that has an extension of .doc, a
Pascal file has an extension of .pas and a text file that has an extension of .txt
JAVA source codes (programs/files with .java extension) are compiled into a format
called as bytecode (i.e. files with .class extension) using Java compiler (javac.exe). If there
is any error then the java compiler announces it else its class file is created by the Java compiler.
Finally, this class file can be executed by a JAVA VIRTUAL MACHINE (java.exe) also known
as Java Interpreter. It is shown in Figure 1.1(b).

Editor/IDE

.java file

JAVA COMPILER javac.exe

.class file (byte codes)

JAVA VIRTUAL MACHINE


(INTERPRETER Java.exe

Fig. 1.1(b): JAVA’s Working


Compiled Java code can run on most computers because Java interpreters and runtime
environments (known as Java Virtual Machine (JVM)) exists for most operating systems
(including UNIX and windows). Bytecodes can also be converted directly into machine
language instruction by Just-in-Time (JIT) compiler. In nutshell, the process of compilation
involves two steps: -
Step-1: Source code is converted to bytecode i.e. the instructions for an imaginary machine
called as the JAVA VIRTUAL MACHINE.
Step-2: This virtual machine is emulated by all Java interpreters. This allows us to execute
a compiled java program among different platform.
Notes:
1. Java is a general purpose programming language with a number of features that
make the language well suited for its use on WWW.
2. Small Java applications are called as Java Applets. They run on the system by
a Java-compatible web browser like Netscape Navigator or Microsoft Internet
Explorer.
For example,
Create a java source file named as first.java as follows:
//Program1—first.java
class first
{
public static void main (String args[ ] )
10 Object Oriented Programming—Using Scala and Java

{
System.out.println(“My First Java Program”);
}
}
We save this file, compile it and run it using Java compiler (javac). Say, this program file
(first.java) is stored in a subdirectory—”c:\java”. Now go to the command prompt then go to
subdirectory java and compile this source program as follows: -
C:\java\javac first.java
Now if there is no error in this program then the javac compiler produces a file—first.class
in the same directory. This class file is the bytecode version of the java source file. The concept
of –“Write-once, Run anywhere” is possible in Java. The source file can be compiled on any
platform that has a javac compiler. The resulting class file can run on any type of platform, using
Java interpreter (java), that has a Java Virtual Machine (JVM) as follows: -
C:\java\java first.class
JAVA environment includes a large number of development tools and several classes and
methods. The development tools are the part of the system known as Java Development Kit
(JDK). The classes and methods are the part of Java Standard Library (JSL). They are also known
as Applications that are used to develop and run java programs as shown in Table 1.2 below.
Table 1.2: JDK Contents
TOOL USAGE
Appletviewer Allows us to run java applets without using any java compatible browser.
java Java Interpreter, which runs applets and applications by reading and interpreting byte
code files.
javac It is the java compiler that translates java source code to bytecode files that the
interpreter can understand.
javadoc Creates HTML format documentation from java source code files.
javah Produces header files for use with native methods.
javap Java disassembler, that enables you to convert bytecode files into a program description.
jdb Java debugger that helps you to find errors in your programs.

Please note that with most of the other programming languages, we either compile or
interpret a program so that we can run it on our system. However, in Java, a program is both
compiled as well as interpreted. Also note that with the compiler, we first of all translate a
program into an intermediate language called as Java Bytecodes i.e. platform-independent
codes interpreted by the interpreter on the Java platform. The interpreter parses and runs each
Java bytecode instruction on the computer. Compilation is done just once but interpretation
is done each time the program is executed. The entire concept is shown in Figure 1.2.
Get free access to e-books for 3 Months
Best Selling Textbooks in COMPUTER SCIENCE ENGINEERING
by
Renowned Authors from Prestigious Publishers

PHI
... & many more

Compliments from to all Faculty Members


already LIVE @ Download the App to access e-books:

Indian Institute of Technology Kanpur, Uttar Pradesh

Visvesvaraya Technological University (Consortium), Belgaum, Karnataka

National Institute of Technology, Kurukshetra, Haryana

Punjab Engineering College, Chandigarh

Rajiv Gandhi Proudyogiki Vishwavidyalaya (Consortium), Bhopal, Madhya Pradesh

Bennett University (Times of India Group), Greater Noida, Uttar Pradesh

Jamia Millia Islamia - A Central University, New Delhi Username: COMP2021


.... & MANY OTHER REPUTED UNIVERSITIES Password: COMP2021

CONTACT US TO KEEP LEARNING!


Ms. Atika KhannaMr. Ranjan RoyMr. Manish Gupta
9999709935 93159052989315905295
Click Here to Buy the Book Online

You might also like