UNIT – I
INTRODUCTION TO JAVA
1.1 Basics of Java
• 1.1.1 History of Java
• Java was developed by James Gosling, Patrick Naughton, Chris Warth, Ed
Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18
months to develop the first working version.
• This language was initially called “Oak” but was renamed “Java” in 1995.
• The primary motivation was the need for a platform-independent
language. The trouble with C and C++ (and most other languages) is that
they are designed to be compiled for a specific target.
• Java inherits the syntax of C and many of the object-oriented features of
C++.
1.1 Basics of Java
• 1.1.2 Java and Internet
• Java can be used to create two types of programs: applications and
applets.
• An application is a program that runs on your computer, under the
operating system of that computer.
• An applet is an application designed to be transmitted over the
Internet and executed by a Java-compatible Web browser.
• An applet is actually a small Java program, dynamically downloaded
across the network, just like an image, sound file, or video clip.
• The important difference is that an applet is an intelligent program,
not just an animation or media file. In other words, an applet is a
program that can react to user input and dynamically change—not
just run the same animation or sound over and over.
1.1 Basics of Java
• 1.1.2 Java and Internet
1.1 Basics of Java
• 1.1.3 Advantages of Java
• Advantages of Java are as below:
Simple
Secure
Portable
Object-oriented
Robust
Multithreaded
Architecture-neutral
Interpreted
Distributed
Dynamic
1.2 Java Virtual Machine & Bytecode
• The Java compiler translates Java source code into a special representation
called bytecode.
• Java bytecode is not the machine language for any traditional CPU.
• An interpreter (JVM), translates bytecode into machine language and
executes it.
1.2 Java Virtual Machine & Bytecode
Java source
Code Java
(.java file) Bytecode
(.class file)
Java
compiler Bytecode
Interpreter
(JVM)
Machine
code
1.3 Java Environment Setup
• http://www.oracle.com/technetwork/java/javase/downloads/index.html
1.3 Java Environment Setup
1.3 Java Environment Setup
1.3 Java Environment Setup
1.3 Java Environment Setup
1.3 Java Environment Setup
1.3 Java Environment Setup
1.3 Java Environment Setup
1.4 Java Program Structure
1.4 Java Program Structure
• DOCUMENTATION SECTION: It is a set of comment lines giving the details
about the program, details about the author and the other details,which
the programmer would like to include.
• PACKAGE STATEMENT: This statement declares a package and informs the
compiler that the classes defined here belong to this package.
Example : package student;
• IMPORT STATEMENT: Using import statement ,we can access to classes
that part of other named packages.
Example : import student.test;
– This statement instructs the interpreter to load the test class
contained in package student.
1.4 Java Program Structure
• INTERFACE STATEMENT: An interface is like a class but includes group of
methods declaration.
• CLASS DEFINITION: Java program may contain multiple class definition.
• MAIN METHOD CLASS: Java application program requires main method as
starting point. There must be one class with main method in java program.
1.5 POP Vs OOP
• 1.5.1 POP
• POP, the problem is viewed as a set of tasks. A set of functions are written
to accomplish this tasks.
• In POP, primary focus is on fuctions.
• A typical program structure of the POP is as shown in below fig:
1.5 POP Vs OOP
• 1.5.2 OOP
• OOP allows decomposition of the problems into set of entities called
objects and then builds the data and functions around these objects.
• OOP treats data as a critical element and does not allow it to flow freely
around the system. It ties data more closely to the functions, that operate
on it.
1.5 POP Vs OOP
• 1.5.3 Difference
1.6 Basics of OOP
• 1.6.1 Abstraction
• Abstraction refers to act of representing essential features without
including the background details.
1.6 Basics of OOP
• 1.6.2 Inheritance
• Inheritance is the process by which one class, acquire the properties of
another class. The old class is known as the super class and new class is
known as the sub class.
• In OOP, the concept of inheritance provides the idea of reusability. This
means that we can add additional information to an existing class without
modifying it.
Super Class
Sub Class
1.6 Basics of OOP
• 1.6.3 Encapsulation
• The wrapping of the data and function together into a single unit class, is
known as encapsulation.
class Test
{
int i;
void display()
{
System.out.print(“ i = “ + i);
}
}
1.6 Basics of OOP
• 1.6.4 Classes
• Class is user defined data type and behaves like the built-in types of a
programming language. Objects are variable of type class.
• Once a class is defined, we can create number of objects belonging to that
class.
• For example, if Fruit has been defined as a class, then statement :
Fruit mango = new Fruit();
• Will create an object mango, belonging to the class Fruit.
1.6 Basics of OOP
• 1.6.5 Subclass & Superclass
• Inheritance is the process by which one class, acquire the properties of
another class.
• The old class is known as the super class and new class is known as the
sub class.
Super Class
Sub Class
1.6 Basics of OOP
• 1.6.6 Polymorphism
• Polymorphism means the ability to take more than one form.
• For example:
• Using same function name to perform different types of tasks is known as
function overloading which is example of the polymorphism.
class A
{
void display ()
{
System.out.println("No parameters");
}
void display (int a)
{
System.out.println("a: " + a);
}
}
1.6 Basics of OOP
• 1.6.7 Overloading
• Using single method name to perform different types of tasks is known as
method overloading which is example of the polymorphism.
void add(int a, int b);
void add(float a, float b);
• Here first add function is used to perform addition of two integer
numbers, while second add function is used to perform addition of two
float numbers.
1.6 Basics of OOP
• 1.6.8 Message Communication
• Objects communicate with each other by sending and receiving the
information, in the same way as people pass message to one another.
1.7 Hello World Program
public class Example
{
Public static void main(String args[])
{
System.out.print(“Hello World”);
}
}
1.7 Hello World Program
Write the Program code in notepad and save the file as Example.java
1.7 Hello World Program
Open Command Prompt.
Change the directory, where Example.java file is stored.
1.7 Hello World Program
Use javac command to compile the file
Enter “javac Example.java” command
1.7 Hello World Program
1.7 Hello World Program
Use java command to Run the file
Enter “java Example” command
1.7 Hello World Program
**********