0% found this document useful (0 votes)
20 views52 pages

1 Introduction

Java, created by Sun Microsystems in 1991, is a high-level, platform-independent programming language derived from C and C++. It features Object-Oriented Programming principles such as encapsulation, inheritance, and polymorphism, and includes a Java Development Kit (JDK) for development. Java's bytecode is executed by the Java Virtual Machine (JVM), enabling its portability across different platforms.

Uploaded by

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

1 Introduction

Java, created by Sun Microsystems in 1991, is a high-level, platform-independent programming language derived from C and C++. It features Object-Oriented Programming principles such as encapsulation, inheritance, and polymorphism, and includes a Java Development Kit (JDK) for development. Java's bytecode is executed by the Java Virtual Machine (JVM), enabling its portability across different platforms.

Uploaded by

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

1

Module1
Java’s Lineage
• The syntax of Java comes from C, a structured efficient
high level language that was designed to replace
assembly code for system programming.
• Syntax of Java are derived from ‘C’.
• Many of Java’s Object Oriented features were derived
from C++.
• Object Oriented Programming is intended to
address the problem of complexity through the use of
inheritance, encapsulation and polymorphism.

2
THE CREATION OF JAVA….
• Java was conceived by James Gosling, Patrick Naughton,
Chris Warth, Ed Frank and Mike Sheridan at Sun
Microsystems in 1991.
• It took 18 months to develop the first working version.
• Initially called as “Oak” and was renamed as “Java” in
1995.
• The primary motivation was the need for a platform –
independent (architectural neutral) language that could
be used to create software to be embedded in various
consumer electronic devices like microwave ovens,
remote controls…..
3
JAVA Environment….
• Java Environment includes a large number of
development tools, hundred of classes and methods.
• The development tools are part of the system known as
Java Development Kit (JDK).
• The classes and methods are part of the Java Standard
Library (JSL) also called as Application Programming
Interface (API).
• The Java Development Kit comes with a collection of
tools that are used for developing and running Java
Programs.

4
….They Include
• appletviewer – enables us to run Java applets.
• java – Java interpreter which runs applets and applications
by reading and interpreting byte code files.
• javac – The Java compiler, translates Java source code to
byte code 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, which enables us to convert
bytecode files into a program description.
• jdb – Java debugger, helps us to find errors in programs.

5
Java’s Magic: Bytecode
• A set of instructions designed to be executed by the java
runtime system.
• The Java Virtual Machine (JVM) is the execution engine
for Java byte code. The byte code is interpreted and
executed by the JVM.
• JVM must be implemented for each platform, byte code is
platform independent.
• When the JVM interprets the byte code it makes security
checks.

6
……
• The JVM can use a Just-in-time compiler to compile
byte code into native code on the fly.
• JIT compiler is a part of the JVM, selected portion of the
byte code are compiled into executable code in real time,
on a piece-by-piece, demand basis.
• JIT compiler compiles code as it is needed during
execution.

7
JVM (Java Virtual Machine)

GRNICA 8
Java Buzzwords……….
• Simple – Java syntax is similar to C, Java Object
Oriented concepts are taken from C++, no pointers, and
have Garbage collection.
• Secure – Java programs downloaded from the internet
will not be able to infect a computer with viruses.
• Portable – Java byte code is executable on any platform
that has the JVM ported to it.
• Object Oriented – Java is true O-O language, everything
is object, code and data reside within objects and classes.

9
……
• Robust – compile time error checking and runtime error
checking help to eliminate errors before they make their
way into a program. Memory Management and Exception
handling.
• Multi-threaded – Java supports multi-threaded
programming allows you to write programs that do many
thing simultaneously. The Object class has several
facilities built in to support multi-threading.
• Architecture-neutral – Java is designed to be “write once
run anywhere”. This is accomplished because the
execution environment of the programs is the JVM.

10
……
• Interpreted and High-performance – Java source code is
compiled into byte code which is interpreted by the JVM.
Just in time compilers compile Java byte code into native
code for high performance.
• Distributed – Java programs handle TCP/IP protocols.
Java also supports distributed programming through
applets, servlets and Remote Method Invocation (RMI).
• Dynamic – Java performs many runtime checks on code
that it is loading. This makes it safe to load code
dynamically. Examples of this are applets.

11
Object Oriented Programming…..
• There are two paradigms for a program construction:
Process Oriented Model
• Organizes program around its code(what’s happening)
• This model can be thought of as code acting on data
• Procedural languages, such as C, characterize a series
of linear steps (that is, code).
Object- Oriented Programming
• Aims to manage increasing complexity.
• Organizes a program around its data (that is object)
and a set of well-defined interfaces to that data.
• This program can be characterized as data
controlling access to code.
12
Abstraction….
• The essential element of OOP is abstraction & humans
manage complexity through abstraction.
• It is the process of focusing on those features of
something that are essentials for the task at hand and
ignoring those that are not.
• For ex. People do not think of a car as a set of tens of
thousands of individual parts. They think of it as a well
defined object with its own unique behavior.

13
……
• The powerful way to manage abstraction is through the
use of hierarchical classifications.
• This allows us to layer the semantic of complex systems,
breaking them into more manageable pieces.
• From the outside, the car is a single object. Once inside,
we see that car consists of several subsystems: steering,
clutch pedal, brakes, sound system, seat belts and so on.
• Each subsystem is made of more specialized units.
• The sound system consists of a radio, CD player, a tape
player.
14
Building Blocks of OOP: Objects
1
& Classes 5
• Object: models a
• Real world object (ex. computer, book, box)
• Concept (ex. meeting, interview)
• Process (ex. sorting a stack of papers or
comparing two computers to measure their
performance)

• Class: prototype or blueprint from which objects


are created
Building Blocks of OOP: Objects & Classes

• Class has
• Set of attributes or properties that describes
every object
• Set of behavior or actions that every object can
perform

• Object has
• Set of data (value for each of its attribute)
• Set of actions that it can perform
• An identity
Real World Example of Objects & Classes
Another Real World Example..
Objects:

 Objects are basic run time entities in Object Oriented

System.
 Object is an instance of a class that holds data

(values) in its variables.


 Data can be accessed by its functions

 It may represent a person, a bank account, a place,

a table of data, vectors, etc.


Classes:

 A class is a collection of objects of similar type.

 A class is a set of variables (to represent its attributes) and

functions (to describe its behavior) that act on its variables


 The entire set of data and code of an object can be made a user defined

data type with the help of a class.


 Once a class is defined, any number of objects belonging to that class

can be created.
What is OOP?
• Paradigm for problem solving by interaction
among objects
• It follows a natural way of solving problems
The Three Object Oriented Principles……

Encapsulation

Inheritance

Polymorphism

23
Encapsulation…..
• Encapsulation is the mechanism that binds together the
code and the data it manipulates and keeps both safe from
outside interference and misuse.
• Encapsulation acts as a protective wrapper that prevents
the code and data from being arbitrarily accessed by other
code defined outside the wrapper.
• Access to code and data inside the wrapper is tightly
controlled through a well-defined interface.

24
Inheritance…...
• The process by which one object acquires the properties
of another object.
• By the use of inheritance, an object need only define
those qualities that make it unique within its class.
• It can inherit its general attributes from its parent.

25
Contd…
Polymorphism……
• Polymorphism is a feature that allows one interface to be
used for a general class of actions.
• One might have a program that requires three types of
stacks. One stack is used for integer values, one for
floating-point values and one for character.
• The algorithm that implements each stack is the same,
even though the data being stored differs.

27
Contd…

Addition Operation

add(int, add(float, add(string,


int) float) string)
Simple program……
/* the first program.*/
class Example
{
public static void main(String args[])
{
System.out.println(“Hello, World!”);
}
}

30
Compiling the Example program……

• Compile the program using the java compiler. The


compiler will produce an output file Example.class

To compile the program:

C:\> javac Example.java

31
Running the Example program…..
• Run the example program by:
C:\> java Example

• This will run the Example program and generate

Hello, World!
as the output

32
A closer look at the Example program…….
• Java supports 3 types of comments
• //  This is a single line comment.
• /* This is a multi-line comment. */
•/** documentation comment*/

• Class Example {
This line states that the lines between the {} pair
defines a class named Example

33
……..
public static void main(String args[]) {
• This defines the main method.
• This method will be called by the JVM when the class
Example is specified on the command line.
• This is the starting point for the interpreter to begin the
execution of the program.
• A java application can have any number of classes but
only one of them must include a main method to initiate
the execution.

34
………
Public :
• The keyword public is an access specifier that declares
the main method as unprotected and therefore making it
accessible to all other classes.

Static :
• The keyword static declares this method as one that
belongs to the entire class and not a part of any objects
of the class.
• The main must always be declared as static since the
the interpreter uses this method before any objects are
created.
35
…..
void :
• The type modifier void states that the main method does
not return any value (but simply prints some text to the
screen).

main :
• main() is the method called when a java application
begins.
• main is different from Main.
• Java compiler also compiles the class that do not contain
main() method.
36
…..
String args[]
• Only one parameter in main(), but complicated one.
• String args[] declares a parameters named args, which is
an array of instances of the class String.
• args receive any command-line arguments information,
when the program is executed.

37
……
System.out.println(“Hello, World!”);
• This calls the println() method in the System class.
• It is used to output text on the console.
• We can use println() to output text and have a new line
appended or use print() to output text without the new
line.
• The println method is a member of the out object, which
is a static data member of System class.
• The statement ends with a semicolon.

38
A second Short Example………
class Example2 {
public static void main(String args[]) {
int num; // This declares an integer variable
num = 100; // now initialize num to 100
System.out.println(“num = “ + num);
num = num * 2; // multiply num by 2
System.out.println(“Now num = “ + num);
}
}
39
Control Statements
If Statement………..
• if (condition) statement;

Example:
if (num < 100)
System.out.println(“num is less than 100”);

40
A few relational operators………
•< Less Than
• > Greater Than
• == (double equal signs) Is Equal to

41
if example program….
class IfSample {
public static void main(String args[])
{
int x = 10, y = 20; // declare and initialize x and y
if (x < y)
System.out.println("x is less than y");
if(x == 10)
System.out.println("x is equal to 10");
if ( y < x )
System.out.println(“y is less than x");
if(x == y)
System.out.println("you won't see this");
}
} 42
The for loop……
• Control construct that allows you to perform an action
repeatedly.
• for (initialization ; condition ; iteration)
• The for loop has an initialization portion, a conditional
clause to control how many times the loop iterates and
an iteration clause that gets the loop ready for the next
iteration.

43
for example….

class ForDemo
{
public static void main(String args[])
{
int count;
for(count = 0; count < 5; count = count+1)
System.out.println("This is count: " + count);
System.out.println("Done!");
}
}

44
Using blocks of code…
•Two or more statements to be grouped into blocks of
code or code blocks .
•In places where you can use a single statement a block of
code is treated as a statement.
• Code blocks are enclosed in {} curly braces.

if (w < h)
{
v = w * h;
w = 0;
}
45
Lexical Issues…
Whitespace
• Java is a free-form language. This means we do not need
to follow any special indentation rules.
• For ex. A program can be written in a single line.
• Whitespace characters are space, tab or newline.

46
Identifiers
• Identifiers are used for class-names, method names and
variables.
• An identifier may be any descriptive sequence of
uppercase and lowercase letters, numbers, or the
underscore and dollar-sign characters.
• Identifiers cannot begin with a number.
• Identifiers are case sensitive, so VALUE is different
from Value.
• Valid identifiers : AvgTemp, count, a4, $test….
• Invalid identifiers : 2count, high-temp, Not/ok…
47
Literals…
• There are many different types of literals or constants.
• Integer – numbers like 45.
• Floating point – numbers like 98.6
• Character – character literals are represented with
opening and closing single quote characters.
For example: ‘a’ or ‘Z’.
• String – String literals are represented with opening and
closing double quotes.
For example: “This is a String!”

48
Comments…
• // indicates a single line comment.
• /* begins a comment that must be terminated with */
• Documentation comments begin with /** and end with */
• Documentation comments are used to insert
documentation into code.
• These comments are then used to produce documentation
by javadoc .

49
Separators….
• ( ) – parenthesis, used to enclose parameters for
methods or to define precedence.
• { } – braces, used to contain blocks of code such as
classes, methods and any group of statements that must
act a single unit.
• [ ] – brackets, declare and operate on arrays.
• ; - used to terminate statements.
• , - used to separate identifiers in a variable declaration.
• . – used to separate package names, used to separate a
variable or method name from its class.
50
The Java Keywords…..

51
Java Class Libraries…
• Java has a wide array of standard classes that are
available for every programmer to use.
• We have already seen the use of the System class.
• The Java class libraries provide facilities for IO, string
handling, networking and graphics.
• The class Libraries save you from having to write base
functionality yourself.

52

You might also like