Introduction to Java
What is Java?
Java is a simple, object oriented, distributed,
interpreted, robust, secure, architecture neutral,
portable, high performance, multithreaded,
dynamic language.
From: Java: An Overview
James Gosling, Sun Microsystems,
What is Java?
Java is
▫ A complete programming language developed by
Sun
▫ Can be used to develop either web based or stand-
alone software
▫ Many pre-created code libraries available
▫ For more complex and powerful programming
language.
Java History
Year Development
1990 Sun decided to developed special software that could be
used for electronic devices. A project called Green
Project created and head by James Gosling.
1991 Explored possibility of using C++, with some updates
announced a new language named “Oak”
1992 The team demonstrated the application of their new
language to control a list of home appliances using a hand
held device.
1993 The World Wide Web appeared on the Internet and
transformed the text-based interface to a graphical rich
environment. The team developed Web applets (time
programs) that could run on all types of computers
connected to the Internet.
Java History
Year Development
1994 The team developed a new Web browser called
“HotJava” to locate and run Applets. HotJava gained
instance success.
1995 Oak was renamed to Java, as it did not survive “legal”
registration. Many companies such as Netscape and
Microsoft announced their support for Java
1996 Java established itself as both
1. “the language for Internet programming”
2. a general purpose OO language.
1997- A class libraries, Community effort and standardization,
Enterprise Java, Clustering, etc..
Features of Java
• Simple
• Secure
• Portable
• Object-oriented
• Robust
• Multithreaded
• Interpreted
• High performance
• Distributed
• Dynamic
Features of Java : Simple
Java is a simple language because
• It contains many features of other languages
like C and C++.
• Pointers, storage classes and goto statements
are not allowed in java.
• Java Doesn’t support Multiple Inheritance
• No need to remove unreferenced objects
because there is Automatic Garbage Collection
in java.
• Thus removes complexity.
Features of Java : Secure
Java is a secure language because
• No explicit pointer
• Programs run inside virtual machine sandbox.
• Java provides secure way to access web
applications.
• Java says not only verify all memory access but
also ensure that no virus code can execute on
the system.
• The absence of pointer ensure that program can
not access to memory location without
authorization.
Features of Java : Platform Independent & Portable
JAVA COMPILER
(translator)
JAVA BYTE CODE
(same for all platforms)
JAVA INTERPRETER
(one for each different system)
Windows Macintosh Solaris Windows NT
JDK vs JVM vs JRE
• JDK: Java Development Kit - Tools for
developing Java apps.
• JRE: Java Runtime Environment - Runs Java
applications.
• JVM: Java Virtual Machine - Executes Java
bytecode.
Features of Java : Platform Independent &
Portable
• Java code can be run on multiple platforms
e.g.:Windows, Linux, Sun Solaris, Mac/OS etc.
• Java code is compiled by the compiler and converted
into bytecode.
• This bytecode is a platform independent code because it
can be run on multiple platforms i.e. “Write Once and
Run Anywhere(WORA)”.
• Programs run inside virtual machine sandbox.
• Java is called portable language because we may carry
the java bytecode to any platform and thus java
programs can be transferred over world wide web.
• Thus java is also called as Architecture-neutral
langauge.
Features of Java : Object Oriented
• Object-oriented means program is a combination of
different types of objects and no coding is outside
of class definitions.
• An extensive class library available in the core
language packages.
Features of Java : Robust
• Robust simply means strong. Java uses strong
memory management.
• In JAVA there are lack of pointers that avoids
security problem.
• There is automatic garbage collection in java.
There is exception handling and type checking
mechanism in java.
• All these points makes java robust
Features of Java : Multithreaded
• Java supports programming, which allows
the user to write programs that perform
many functions simultaneously.
• The two or more part of the program can run
concurrently then each part of such a program
is called a Thread and this type of programming
is called multithreaded programming.
• Each thread defines a separate path of
execution. Thus, multithreading is a specialized
form of multitasking.
Features of Java : Compiled & Interpreted
Programmer
Executable
Source Code Object Code
Code
Text Editor Compiler linker
.c file .o file [Link] file
Notepad, vi
Scenario for compiled Language
Features of Java : Compiled & Interpreted
Programmer
Machine Language Instruction
Source Code Byte Code
Text Editor Compiler Interpreter
.java file .class file
Notepad, vi javac java
Features of Java : Compiled & Interpreted
• The source code is first compile and generates the code
into an intermediate representation called Java bytecode
which is a highly optimized set of instruction code.
• This code can be interpreted on any system that has a
Java Virtual Machine and generates the machine code.
• Java bytecode was carefully designed by using a just-in-
time compiler so that it can be easily translated into
native machine code for very high performance.
Features of Java : High Performance
Java is faster than traditional language
using interpretation since byte code is
"close" to native code.
Features of Java : Distributed
• Java allows the object to access the
information across the network with the
help of RMI (Remote Method Invocation)
• Java allowed objects on two different
computers to execute procedures
remotely.
• So this feature supports the client/server
programming.
Features of Java : Dynamic
• The linking of data and methods to where they are
located, is done at run-time.
• New classes can be loaded while a program is running.
• Linking is done on the fly.
• Java programs carry with them substantial amounts of
run-time type information that is used to verify and
resolve accesses to objects at run time.
• This makes it possible to dynamically link code in a
safe and perfect manner
C Vs Java
C Language Java
Procedure oriented Object-Oriented
-Emphasis is on procedure -Emphasis is on data
Compiled language C0mpiled & Interpreted
language
C is a low-level language JAVA is a high-level
(difficult interpretation for
language (abstracted from
the user, closer significance
the machine-level details,
to the machine-level code) closer significance to the
program itself)
Uses the top-down approach Uses the bottom-up
approach
Support pointer Pointer go backstage in
JAVA
C Vs Java
C Language Java
Memory is managed by the Java support automatic
user. memory management.
C does not support JAVA supports Method
overloading at all Overloading
C supports Preprocessors JAVA does not support
Preprocessors.
No exception handling JAVA supports exception
support. handling and errors are
handled using various
exception handling
techniques
Class
• A class is a blueprint or template for creating objects.
It defines properties (variables) and behaviors (methods)
common to all objects of that type.
class ClassName {
// Fields (Variables or Data Members)
datatype variableName;
// Methods
returnType methodName(parameters) {
// method body
}
}
Class
class Car {
// Fields
String color;
int speed;
// Method
void drive() {
[Link]("Car is driving");
}
}
Object
An object is an instance of a class. It has state (values of
variables) and behavior (methods it can perform).
ClassName objName = new ClassName();
Dot (.) Operator in Java
The dot operator (.) in Java is used to access
members of a class or object, such as:
• Fields (variables)
• Methods
• Nested classes
It helps to connect an object or class reference with its
members.
Object
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object of class Car
[Link] = "Red"; // Setting value
[Link] = 100;
[Link](); // Calling method
[Link]("Color: " + [Link]);
[Link]("Speed: " + [Link]);
}
}
Container vs. Definition classes
• Generally, classes can be used for two purposes:
▫ Container classes:
a collection of static methods that are not bound to any
particular object (e.g., the main() method).
These static methods usually have something in common
▫ Definition classes:
These classes define new objects.
Container vs. Definition Classes -
Example
• The Math class is an example of the first kind. It
is a container for math utility methods:
[Link](), [Link](), [Link](), ...
• The class Turtle is an example of the second
kind. It defines a new type of objects, Turtle
objects.
Objects and Classes
• The relationship between Classes and Objects can be
seen as the relationship between a blueprint/model and
the actual object built from it.
▫ Example: Blueprint of a house (class) and the house
(object)
• The class defines:
▫ The type of data that will be held in an object
▫ The code for the methods of the object
• In order to use a class you must instantiate an object
from it.
▫ This corresponds to building the house from the
blueprint.
▫ There may be many objects from a single class
Sample Program
class Student {
String name;
int age;
void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
public class Sample {
public static void main(String[] args) {
Student s1 = new Student();
[Link] = “Abhay"; // . operator to access field
[Link] = 20;
[Link](); // . operator to call method
}
}
Data types
Java is a strongly typed language, which means every
variable must have a data type.
• Primitive Data Types
• Non-Primitive (Reference) Data Types
Primitive data types
Type Size Default Description Example
Small integer (–128 to
byte 1 byte 0 byte a = 100;
127)
short 2 bytes 0 Short integer short b = 2000;
int 4 bytes 0 Default integer type int c = 100000;
long d =
long 8 bytes 0L Larger integer values
100000L;
float 4 bytes 0.0f Single-precision decimal float e = 10.5f;
Double-precision double f =
double 8 bytes 0.0d
decimal 20.99;
A single character
char 2 bytes '\u0000' char g = 'A';
(Unicode)
boolean h =
boolean 1 bit false true or false
true;
Non-Primitive data types
Type Description Example
String Sequence of characters String name = "Java";
Fixed-size collection of same
Array int[] arr = {1, 2, 3};
type
Car myCar = new
Class Blueprint of objects
Car();
interface Drawable
Interface Blueprint of methods (no body)
{...}
Object obj = new
Object Parent class of all classes in Java
Object();
Data types Example
• int age = 25;
• double salary = 55000.50;
• char grade = 'A';
• boolean isActive = true;
• String name = "Rina";
• int[] scores = {90, 80, 70};
Command Line Arguments
Pass data to main method at runtime.
Example:
• public static void main(String[] args)
• Run: java Main John
• Output: Hello, John
Input using Scanner Class
• Used for reading console input easily.
Example:
Scanner sc = new Scanner([Link]);
String name = [Link]();
import [Link];
Input using BufferedReader Class
• Efficient for reading large inputs.
Example:
BufferedReader br = new BufferedReader(...)
String input = [Link]();
int age = [Link](input);
import [Link];
import [Link];
import [Link];