Unit E28093 1 - Introduction of Java
Unit E28093 1 - Introduction of Java
INTRODUCTION TO JAVA
TOPICS TO BE COVERED
1.1 Basics of Java, Background/History of Java,
Java and the Internet, Advantages of Java
1.2 Java Virtual Machine & Byte Code
1.3 Java Environment Setup
1.4 Java Program Structure
1.5 Procedure-Oriented Vs. Object oriented
programming concept
1.6 Basics of OOP: Abstraction, Inheritance,
Encapsulation, Classes, subclasses and super
classes, Polymorphism, message communication
1.7Compiling and running a simple "Hello World"
program: Setting Up Your Computer, Writing a
Program, Compiling, Interpreting and Running the
program, Common Errors
1.1 JAVA EVOLUTION
Java is a general purpose, class based, and robust,
secure, safe, object oriented programming language.
It was developed by sun micro system in 1991 by
James Gosling its name was Oak. In 1995 its name
has been changed to Java because of some legal
issues.
Java was designed for development of software for
consumer electronic devices like TVs, VCRs, toaster
etc.
Java removes limitation like portability and
reliability of C and C++.
Java program would run in fundamentally different
execution environments.
Java is strongly typed language.
Compile time contains translating programs into a
machine independent byte code representation.
Runtime activities include loading and linking of
classes needed to execute a program, optional
machine code generation, dynamic optimization of
program.
Most striking feature of java is platform
independence. Java is not tied to any particular
hardware or OS. Java programs can be executed
anywhere on any system.
JAVA FEATURES
There are various features described by sun micro system.
Some of them are given here:
1. Compiled and interpreted
2. Platform independent
3. Portable
4. Object oriented
5. Robust and secure
6. Distributed
7. Familiar , simple and small
8. Multithreaded and interactive
9. High performance
10. Dynamic and extensible
1. Compiled and interpreted:
Computer language is either compiled or interpreted.
Java combines both so it is two stage system
First java compiler translates source code into byte
code.
Java interpreter generates machine code that can be
directly executing by machine that is running the
program.
Source code
javac
Byte code
java
Machine code
2. Platform independent:
Programs written by user are called source code
and executable program is called object code.
Object can be executed by computer and object is
specific to a particular CPU. So it cannot be executed
on different platform. Java removes this limitation.
When java program is compiled java compiler
produces on object file which contains bytecode.This
byte codes are not machine/CPU specific, it can be
interpreted by JVM (Java Virtual Machine).so that
code can be run on any JVM of computer system.
The Same byte codes can be executed by any JVM on
any platform.
Thus java is platform independent.
3. Portable :
Java’s important feature is portability.
Java program can be easily moved from one computer
system to another, anywhere and anytime. Changes
/upgrade in OS .processors and system resources will
not force any changes in java program.
We can also download applet from a remote computer
onto our local system via internet and execute it locally.
Java provides Write Once Run Anywhere (WORA),
which makes the java language portable provided that
the system must have interpreter for the JVM .
Java also has the standard data size irrespective of
operating system or the processor .These features make
the java as a portable language.
Java provides portability in two ways:
First, java compiler generates byte code instructions
that can be implemented on any machine.
Secondly, the size of primitive data types is machine-
independent.
4. Object oriented
Almost everything in java is an object.
All program code and data reside within object and
classes. Object model in java is simple and easy to
extend.
Java comes with extensive set of classes, arranged in
packages that we can use in our program by
inheritance.
Java is true (pure) object oriented language because
object is the outer most level of data structure in
java.
Everything in java is object even the primitive data
types can also be converted into object by using the
wrapper class.
5. Robust and secure
It provides safe guards to ensure reliable code.
Java has strict compile time and runtime checking for
data types.
Java also incorporates concept of exception handling,
which captures series errors and eliminate any risk
crashing system. So, java is robust.
Java system verify not only all memory access but also
ensures no viruses are communicated with applet.
Absence of pointer in java ensures that program cannot
gain access to memory location without proper
authorization.
6. Distributed
Java is designed as distributed language for creating
applications on networks.
Java applications can open and access remote objects
on internet .These multiple programmers at multiple
remote locations to collaborate and work together on
a single project.
7. Simple ,small and familiar
Java is small and simple.
Many features of C and C++ those are redundant or
sources of unreliable code are not part of java.
It doesn’t support pointer,pre-processor , header
files,goto statement and other
It eliminates operator overloads, multiple
inheritance, thus java is simplified version of C++.
It includes automatic storage management,
typically using a garbage collector, to avoid safety
problems of explicit de-allocation of memory
references.
8. Multithreaded and interactive
Java is multithreaded programming language.
Multithreading means a single program having
different threads executing independently at the same
time. Multiple threads execute instructions according to
the program code in process or a program.
Java supports multithreaded programs. This means we
need not wait for the application to finish one task
before beginning other.
Java runtime comes with tools that support
microprocessor synchronization and construct smoothly
running interactive system.
9. High performance
High performance ,by intermediate byte code
Multithreading enhances the overall execution
speed of java program
10. Dynamic and extensible
Java is dynamic language
It is capable of dynamically linking in new class
libraries, method and object.
Java program support function written in other
language such as C and C++.This function known
as native methods.
Native methods are linked dynamically at
runtime.
Note :
Java does not run on Windows 95 and MS-Dos
because on DOS and Windows 95 file names have
extension having 3(.htm or .txt) character while
java file has extension having 4 characters (.java).
1.1 ADVANTAGES OF JAVA
Java syntaxes are inherited from C/C++.In C++ many
concepts are confused which are left out or more
cleared in java.
Java is easy to program because it eliminates the
Figure :1
Before java, WWW was limited to display of still (means
motionless ) images and texts.
But incorporation of java into web pages ,web pages are
capable of supporting animation, graphics, games and a
wide range of special effects.
Using java, the web has become more interactive and
dynamic
Using web, we can run java program on someone else’s
computer across the internet.
Java communicates with a web page through special
tag<APPLET> by following way:
User’s Computer Remote Computer
Applet
Byte code source code
Byte code
Java
Web browser Applet tag
HTML document
output
Web browser
Sends a request for
an HTML
Documents and
web server
user Figure :2
STEPS:
1. User sends a request for an HTML Document’s web
server. (Web server is a program that accepts a
request , process the request and sends the required
document)
2. HTML document is returned to the user’s browser
document contains APPLET tag which identifies
applet.
3. Corresponding applet byte code is transferred to
user’s computer by java compiler
4. Java enabled web browser on user’s computer
interprets the byte code and provides output.
5. User may have further interaction with applet but
with no further downloading from providers web
server.
WEB BROWSERS
Web browsers are used to navigate through the
information found on the net.
Browsers allows users to retrieve information spread
across internet and display it using HTML.
Web browse in java:
Hot Java
Netscape Navigator
Internet Explorer
1) HOT JAVA
Browser from sun micro system.
It enables the display of interactive content on the web
using java language.
Hot java written completely in java .
Operating system
compiler interpreter
Figure :5
user
s Layer of interactions for java program
JIT (JUST IN TIME COMPILER)
JVM includes optional JIT.
JIT dynamically compiles byte code into executable
code.
It takes byte code ,compiles them into machine
code(native code) for machine.
It compiles program on a method by method basis just
before they are called.
JIT is faster than JVM.
JRE (JAVA RUNTIME ENVIRONMENT)
JRE consists of JVM and the program on the other .
It runs code compiled for JVM by:
(Source code)
interpreter
JIT code
generator
Run time
.class
Java Virtual Machine
(byte code)
Hard ware
JAVA SUPPORT SYSTEM
Internet connection
Web server
Web browser
HTML
Applet tag
Java code
Byte code
1.3 JAVA ENVIRONMENT SETUP
JE (JAVA ENVIRONMENT)
JE includes large no. of development tools and
hundred of classes and methods.
Tools are part of JDK (java development toolkit) and
classes and methods are part of JSL
(API)
JE = JSL + JDK
javac
Java Header
javah
class files files
java jdb
Java
Program
output Figure :3
IMPLEMENTING JAVA PROGRAM
optional
optional
optional
optional
Essential
POP does not have any access OOP has access specifier named
4 specifier. Public, Private, Protected
In POP, Data can move freely from In OOP, objects can move and
function to function in the system. communicate with each other through
5
member functions.
To add new data and function in OOP provides an easy way to add new
6 POP is not so easy. data and function.
In POP, Most function uses Global In OOP, data cannot move easily from
data for sharing that can be function to function, it can be kept
7
accessed freely from function to public or private so we can control the
function in the system. access of data.
POP does not have any proper way OOP provides Data Hiding so
8 for hiding data so it is less secure. provides more security.
In POP, Overloading is not possible. Overloading is possible in the form of
9 Function Overloading and Operator
Overloading.
Examples of POP are: C, VB, Example of OOP are : C++, JAVA,
10 FORTRAN, and Pascal. VB.NET, C#.NET.
JAVA ,C AND C++
Java and C
Java is an object oriented language has mechanism
to define class and objects.
To build up a simple and safe language, java team
did not include some of C features in java.
Java does not include
C unique statement goto ,sizeof ,typedef
Java adds,
Java functions with no arguments must be
declared with empty parameters and not with the
void keyword
It adds new operator ,instanceof and >>>
It uses break and continue
JAVA AND C++
Java is true object oriented language while C++ is
basically C with object oriented extension.
Java does not support
Operator overloading
Multiple inheritance
Global variable
Explicit pointer
Header files
C
JAVA
1.6 BASICS OF OOP: ABSTRACTION,
INHERITANCE, ENCAPSULATION, CLASSES,
SUBCLASSES AND SUPER CLASSES,
POLYMORPHISM, MESSAGE COMMUNICATION
BASICS OF OOP’S
OOP is an organization and development, which
attempts to eliminate some of the pitfall of
converting programming method.
OOP treats data at a critical element in the
program development and doesn’t allow it to flow
freely around the system .
It ties data more closely to the function that operate
on it and protect it from unintentional modification
by other function.
OOP allows us to decompose a problem into a
number of entities called object. And build data and
functions around these entities.
Object= Data + Method
Data of object can be accessed only by the method
associated with that project.
data
BASIC CONCEPT OF OBJECT ORIENTED
PROGRAMMING
1) Object and classes
Objects are basic runtime entities in object
oriented system.
Object may represent a person, place, chair, or
any item.
Object can interact without having to know the
details of each other’s data or code.
Object is a basically a set of data and code to
perform operation on data
object
person Parrot
legs
Name data
eyes
Basic pay feather
peak
salary( ) methods
eating ( )
tax ( )
flying ( )
2) CLASS
Class is prototype that defines the variables and
the methods common to all object of similar type.
Classes are user defined data types and behave
like built in type of programming language.
Simply, Class is a collection of logically related
data items which includes data (variable) and
function (methods) for data.
Birds
7) Message Communication
Object oriented program consist of a set Object
communicates with one another by sending and
receiving information.
1.7 COMPILING AND RUNNING A
SIMPLE "HELLO WORLD" PROGRAM:
SETTING UP YOUR COMPUTER,
WRITING A PROGRAM, COMPILING,
INTERPRETING AND RUNNING THE
PROGRAM, COMMON ERRORS
SIMPLE JAVA PROGRAM
class Test
{
public static void main(String args[ ])
{
System.out.println( “welcome to Java”);
}
}
1) First line : class Test
declares a class which is an object oriented
construct.
java is true object oriented language and therefore
everything must be placed inside a class.
class is keyword and it declares that a new class
definition follows.Test java identifier(name)
specifies name of the class to be defined.
2) Second line : Opening brace : {
3) Third Line : public static void main (String
args[ ])
Every java program must include main( ) method. It
is the starting point for interpreter to begin the
execution of program.
MAIN ( ) METHOD OF JAVA
public: The keyword public is an access modifier. It
declares main method as unprotected .it makes
method accessible to all other classes.
static : It is a keyword, which declare method is for
entire class and not a part of any object of class. The
main must always be declared as static since the
interpreter uses this method before any objects are
created.
void does not return any value.
String args declares parameter names args which
contain an array of object of class type string.
4) Output line:
System.out.println (“Hello”);
class
object method
This line is similar to printf ( ) statement of C or
cout<< construct of C++.
Since java is true object oriented, every method must
be part of an object.
println method is a member of the out object, which
is a data member of a System class.println method
always append a new line character to the end of the
string. This means subsequent output will start on a
new line.
Semicolon at the end of each statement is used. Every
java statement must end with semicolon.
5) Fifth line :closing brace : }
It is the ending of program.
Note: java program must name same as in class which
includes main( ) method otherwise interpreter
cannot run that class.
DIFFERENCE BETWEEN
PRINT( ) AND PRINTLN( ) METHOD
print ( ) method: print and wait
This method sends information into a buffer. This
buffer is not flushed until a newline (or end of line)
character sent. As a result print( ) method prints out
on one line until a new character is encountered.
System.out.print ( “hello”);
We can also use \n to force the display to be brought
to the next line by printing a newline character as
follows:
System.out.print ( “ hello \n” ); //this would work
same as println ( ) method
println ( ) method : prints a line and move to next line
Output :
total no. Of arguments passed= 6
1 : Java is simple !!!
2 : Java is robust !!!
3 : Java is secure !!!
4 : Java is portable !!!
5 : Java is dynamic !!!
6: Java is object_oriented !!!
READ STATEMENT
We may also give values to variables interactively
through the keyboard using readLine( ) method.
readline( ) is invoked using an object of the class
DataInputStream.
readLine( ) reads the input from the keyboard as a
string which is then converted to the corresponding
data type using the data wrapper classes.
We have used the keywords try and catch to handle
any errors that might occur during the reading
process.
Example :
import java. io .DataInputStream;
class Reading
{ public static void main (String args [ ])
{
DataInputStream obj=new DataInputStream(System.in);
int i=0;
float f=0.0 F;
try
{
System.out.println (“enter an integer no “);
i = Integer.parseInt ( obj. readLine( ) );
System.out.println (“enter float no “);
f = Float.valueOf (obj.readLine ( ) ).floatValue( );
}
catch(Exception e)
{
System.out.println ( “enter proper value” );
}
System.out.println( “int no i= “+ i );
System.out.println( “float no f= “ +f);
}
}
OUTPUT:
E :\ java program\ javac Reading.java
E :\ java program\ java Reading
enter an integer no
56
enter float no
23.455
int no i= 56
float no f=23.455
Thank you