You are on page 1of 68

K L University

Department of Computer Science and Engineering


BCA PROGRAM

 Object Oriented Programming


(through Java)
Introduction
Dr G.Krishna Mohan
Professor, Dept. CSE, KL University.
You will learn …
2

Programming languages
Program Execution
Why java?
Java History-versions-editions
Java characteristics
Anatomy of java program
Programs
3

Computer programs, known as software, are


instructions to the computer.
 Computers do not understand human languages, so
you need to use computer languages to
communicate with them.
Programs are written using programming
languages.
Programming Languages
4

Machine Language Assembly Language High-Level Language


• Machine language is a set of primitive instructions
• The instructions are in the form of binary code.
• Program with native machine language is a
tedious process.
• The programs are highly difficult to read and
modify.
• To add two numbers,
you might write an instruction in binary like
• 1101101010011010
Programming Languages
Machine Language Assembly Language
5 High-Level Language

• Assembly languages were developed to make


programming easy.
• The computer cannot understand assembly
language too.
• A program called Assembler is used to convert
assembly language programs into machine code.
• To add two numbers, you might write an
instruction in assembly code ADD R1, R2, R3
Assembly Source File
Machine Code File


ADDF3 R1, R2, R3
Assembler …
1101101010011010


Programming Languages
Machine Language Assembly Language
6 High-Level Language

• The high-level languages are English-like and


easy to learn and program.
• The following is a high-level language statement
that computes the area of a circle with radius 5:
• area = 5 * 5 * 3.1415;
7

Programmer
 Writes a program in source code.
Compiler
 Converts source code to machine language code.
Linker
 Combines machine language with libraries and converts them
to an executable file.
Interpreter
 Converts source code to machine language and executes one
line at a time.
8
Popular High-Level Languages
9

COBOL (COmmon Business Oriented Language)


FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional Code)
Pascal (named for Blaise Pascal)
Ada (named for Ada Lovelace)
C (whose developer designed B first)
Visual Basic (Basic-like visual language developed by
Microsoft)
Delphi (Pascal-like visual language developed by Borland)
C++ (an object-oriented language, based on C)
Java
Compiling Source Code
10

• A program written in a high-level language is called


a source program.
• Since a computer cannot understand a source
program. Program called a compiler is used to
translate the source program into a machine
language program called an object program.
• The object program is often then linked with other
supporting library code before the object can be
executed on the machine.

Source File Compiler Object File Excutable File


Linker
• Open the ‘Notepad’ application.
• Type the program and save the file with the name of the class
having the main method and with the extension ‘.java’
• Invoke the command prompt by typing ‘cmd’ in Run.
• Change the path of the current directory to the directory where
the source file is saved.
• Set the ‘path’ envrironment variable to the path of the file
‘javac’ is stored.
• For temporarily setting the path, type the following at the command
prompt. set path=“c:\program files\java\jdk\bin”.
• For permanently setting the path,
open ‘system properties’ , in
the ‘startup and recovery’ of ‘advanced’ tab, click
‘environment variables’, choose
‘system variables’, choose the
‘path’ and press ‘edit’ option, set the path to the
desired directory.
Compiling Java Source Code
12

 You can port a source program to any machine with


appropriate compilers. The source program must be
recompiled, however, because the object program can only run
on a specific machine.
 Java was designed to run object programs on any platform.
 With Java, you write the program once, and compile the
source program into a special type of object code, known as
bytecode.
 The bytecode can then run on any computer with a Java
Java Bytecode

Virtual Machine. Java Virtual


Machine
 Java Virtual Machine is a software that interprets Java Any
Computer
bytecode.
Creating, Compiling, and Running
Programs
Create/Modify Source Code
13

Source code (developed by the programmer)


Saved on the disk
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Source Code
}
}

Compile Source Code


Byte code (generated by the compiler for JVM
i.e., javac Welcome.java
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

If runtime errors or incorrect result


Java Environment / Life Cycle of Java Code

Runtime
Compile-time Environment Class
Environment Java
Loader
Class
Bytecode Libraries
Java Verifier
Source
(.java)

Just in
Java
Time
Interpreter Java
Java Compiler
Bytecodes Virtual
Java move locally machine
Compiler or through
network
Runtime System

Java
Bytecod Operating System
e
(.class )
Hardware
Why Java?
15

• Java enables users to develop and deploy applications


on the
• desktop computers,
• Internet for servers,
• small hand-held devices.
Standalone application
16

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

Source
IMPORTANT NOTE: To run the program from the Run button,
set path=“c:\Program Files\java\jdk1.5.0\bin”
Run
Web application
17
PDA and Cell Phone
18
Java’s History
19
James Gosling and Sun Microsystems
Oak
 Java, May 20, 1995, Sun World Official release as Java –
Internet
 1999 split into J2SE and J2EE
 2000 J2ME

HotJava
 The first Java-enabled Web browser
JDK Versions
20
Version Release date
JDK Beta 1995
JDK 1.0 January 1996
JDK 1.1 February 1997
J2SE 1.2 December 1998
J2SE 1.3 May 2000
J2SE 1.4 February 2002
J2SE 5.0 September 2004
Java SE 6 December 2006
Java SE 7 July 2011
Java SE 8 (LTS) March 2014
Java SE 9 September 2017
Java SE 10 March 2018
Java SE 11 (LTS) September 2018
Java SE 12 March 2019
Java SE 13 17 September 2019
JDK Editions
21

Java Standard Edition (J2SE)


 J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
 J2EE can beThe Java platform (Enterprise Edition) differs from the Java
used to develop server-side applications
Standard Edition Platform (Java SE) in that it adds libraries
such as Javawhich
servlets and functionality
provide Java ServerPages.
to deploy fault-tolerant,
distributed, multi-tier Java software, based largely on
Java Micro Edition (J2ME).
modular components running on an application server.
 J2ME can beThis
used toplatform
is the develop for applications for mobile
developing applications for mobile
devices
devices such andphones.
as cell embedded systems such as set-top boxes. Java
ME provides a subset of the functionality of Java SE, but also
introduces libraries specific to mobile devices
Java IDE Tools
22

JBuilder Borland
NetBeans Open Source by Sun
Sun ONE Studio by Sun MicroSystems
Eclipse Open Source by IBM
Characteristics of Java
23

 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java Is Multithreaded
 Java Is Dynamic
Characteristics of Java
24

 Java Is Simple • Java is partially modeled on C++, but


greatly simplified and improved.
 Java Is Object-Oriented
• it is like C++ but with more functionality
 Java Is Distributed
and fewer negative aspects.
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java Is Multithreaded
 Java Is Dynamic
Characteristics of Java
25
 Java Is Simple
 Java Is Object-Oriented • One of the central issues in software
 Java Is Distributed development is how to reuse code.
• Object-oriented programming provides
 Java Is Interpreted great flexibility, modularity, clarity, and
 Java Is Robust reusability through encapsulation,
inheritance, and polymorphism.
 Java Is Secure
 Java Is Architecture-
Neutral
 Java Is Portable
 Java Is Multithreaded
 Java Is Dynamic
Characteristics of Java
26
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed • Distributed computing involves several
 Java Is Interpreted computers working together on a
network.
 Java Is Robust • Java is designed to make distributed
 Java Is Secure computing easy. Since networking
capability is inherently integrated into
 Java Is Architecture-
Java, writing network programs is like
Neutral sending and receiving data to and from
 Java Is Portable a file.
 Java Is Multithreaded
 Java Is Dynamic
Characteristics of Java
27
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted • You need an interpreter to run Java
 Java Is Robust programs. The programs are compiled
into the Java Virtual Machine code
 Java Is Secure called bytecode.
 Java Is Architecture- • The bytecode is machine-independent
and can run on any machine that has a
Neutral Java interpreter, which is part of the
 Java Is Portable Java Virtual Machine (JVM).
 Java Is Multithreaded
 Java Is Dynamic
Characteristics of Java
28
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust • Java compilers can detect many
problems that would first show up at
 Java Is Secure execution time in other languages.
 Java Is Architecture- • Java has eliminated certain types of
error-prone programming constructs
Neutral
found in other languages.
 Java Is Portable • Java has a runtime exception-handling
 Java Is Multithreaded feature to provide programming
support for robustness.
 Java Is Dynamic
Characteristics of Java
29
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted Java implements several security mechanisms to
protect your system against harm caused by stray
 Java Is Robust programs.
 Java Is Secure • No Pointer!
• Extensive compile-time checking
 Java Is Architecture- • Runtime checking
Neutral • Automatic memory management – GC
 Java Is Portable
 Java Is Multithreaded
 Java Is Dynamic
Characteristics of Java
30
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral • Write once, run anywhere
 Java Is Portable
• With a Java Virtual Machine (JVM),
 Java Is Multithreaded you can write one program that
 Java Is Dynamic will run on any platform.
Total Platform Independence

JAVA COMPILER
(translator)

JAVA BYTE CODE


(same for all platforms)

JAVA INTERPRETER
(one for each different system)

Windows 95 Macintosh Solaris Windows NT


Characteristics of Java
32

 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable • They can be run on any platform
 Java Is Multithreaded without being recompiled.

 Java Is Dynamic
33
Characteristics of Java
34

 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-
Neutral
Multithread programming is smoothly
 Java Is Portable
integrated in Java, whereas in other
 Java Is Multithreaded languages you have to call procedures
specific to the operating system to enable
 Java Is Dynamic
multithreading.
Characteristics of Java
35

 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-
Neutral
 Java Is Portable
 Java Is Multithreaded • Java was designed to adapt to an evolving
 Java Is Dynamic environment.
• New code can be loaded on the fly without
recompilation.
Anatomy of a Java Program
36
• Comments
• Indentation and spacing lines
• Reserved words
• Naming conventions
• Modifiers
• Statements
• Blocks
• Classes
• Methods
• The main method
• Package
Comments
37

In Java, comments are


1. Single line commenting
1. Preceded by two slashes (//) in a line.
2. Multiline commenting
1. Enclosed between /* and */ in one or multiple lines.
Comments

• Include a summary at the beginning of the program to explain


what the program does, its key features, its supporting data
structures, and any unique techniques it uses.

• Include your name, class section, instructor, date, and a brief


description at the beginning of the program.
Indentation and Spacing

• Indentation
• Indent two spaces.

• Spacing
• Use blank line to separate segments of the code.
Reserved Words
40

Reserved words or keywords are words that have a


specific meaning to the compiler and cannot be
used for other purposes in the program.
when the compiler sees the word class, it
understands that the word after class is the name
for the class. Other reserved words…
Reserved Words
abstract assert boolean break byte

case catch char class const

continue default do double else

extends final finally float for

goto if implements import instanceof

int interface long native new

package private protected public return


short static strictfp super switch
synchronized this throw throws Transient

try void violate while


Naming conventionsme

• Choose meaningful and descriptive names.


• Variables and method names:
• Use lowercase. If the name consists of several words,
concatenate all in one, use lowercase for the first word,
and capitalize the first letter of each subsequent word
in the name.
• Example:
• variables radius and area
• method computeArea.
Naming conventions…

Welcome

• Class names:
• Capitalize the first letter of each word in the name.
Example: ComputeArea.

• Constants:
• Capitalize all letters in constants, and use underscores
to connect words. Example: PI and
MAX_VALUE
Modifiers
44

Java uses certain reserved words called


modifiers that specify the properties of the data,
methods, and classes and how they can be used.
Examples of modifiers are public and static.
Other modifiers are private, final, abstract,
and protected.
A public datum, method, or class can be accessed by
other programs.
 A private datum or method cannot be accessed by
other programs.
Statements
45

A statement represents an action or a sequence of


actions.
The statement
System.out.println("Welcome to Java!")
in the program is a statement to display the
greeting "Welcome to Java!"
Every statement in Java ends with a semicolon
(;).
Blocks
46

• A pair of braces in a program forms a block that


groups components of a program.

public class Test {


Class block
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Method block
}
}
Block styles

Welcome

Next-line public class Test


style {
public static void main(String[] args)
{
System.out.println("Block Styles");
} PROFESSIONAL ACTIVITIES
}

End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
Classes
48

The class is the essential Java construct.


A class is a template or blueprint for objects.
To program in Java, you must understand classes
and be able to write and use them.
A program is defined by using one or more
classes.
Class

[“public”]
[“abstract”|”final”] “class”
ClassName [“extends”
SuperClassName] [“implements”
InterfaceName]
{

//properties declarations (i.e variables)


//behavior declarations (i.e methods)

}
Method

[ “public” | “private” | “protected” ]


[”final”]
[ “static | “abstract” | “native” ]
return_data_type methodName(parameter_list)
{
//body to define some actions
}
main Method
51

The main method provides the control of program


flow. The Java interpreter executes the application
by invoking the main method.
 
The main method looks like this:
 public static void main(String[] args)
{
// Statements;
}
Package
52

java.lang
java.util
java.io
java.awt
java.awt.image
java.applet
java.net
53

 What is the use of final keyword in Java?


1. When a class is made final, a sublcass of it can not be
created.
2. When a method is final, it can not be overridden.
3. When a variable is final, it can be assigned value only once.
4. All of the above 
54

 Java Source Code is compiled into ______________.


A Bytecode 
B Source Code
C .Exe
D .Obj
55

 Which of the tool is used to compile java code ?


A javac 
B jar
C javadoc
D java
56

 Which of the following tool used to execute java


code. 
A java
B rmic
C javac
D javadoc
57

 Variable 'path' in Windows Operating System is called as


__________.
 A Global Variable
 B Local Variable
 C Environment Variable 
 D Instance Variable
58

 Class file must have extension _______________.


A .java
B .sample.class
C .class 
D .cls
59

 Java Programming was designed by


_________________.
A Sun Microsystems 
B Mozilla Corporation
C Microsoft
D Amazon Inc
60

 Earlier name of Java Programming language was -


A OAK 
B Eclipse
C Netbean
D D
61

 Which of the following personality is called as


father of Java Programming language -
A None of these
B Larry Page
C Bjarne Stroustrup
D James Gosling 
62

 Java Programming promises programmer - "Write Once,


Run Anywhere".
A False
B True 
63

Which of the following converts human readable file into


platform independent code file in Java?
A Applet
B JRE
C Compiler 
D JVM
64

Which of these classes is not included in java.lang?


a) Byte
b) Integer
c) Array 
d) Class
65

Platform independent code file created from Source


file is understandable by _______________.
A JRE
B JVM 
C Compiler
D Applet
66

JDK in Java stands for ___________.


A Java Debugging Kit
B Java Developer's Kit 
C Java Decompilation Kit
D None of these
67

JVM is a _____________ .
A Compiler
B Debugger
C Assembler
D Interpreter 
68

 We can set class path of java by setting following


variable.
A javapath
B java
C class
D path 

You might also like