You are on page 1of 24

Introduction to

Java
Session Objectives

 Determine Programming Models


 Features Of Object Oriented Programming Model
 Encapsulation
 Data Abstraction
 Inheritance
 Polymorphism
 History of Java
 Features of Java
 Java vs C++
 Types of Java Programs
Programming Models

Procedural programming model - Each problem is divided into smaller


problems and solved using specified modules that act on data

Object-oriented programming model – It perceives the entire software


system as a collection of objects which contain attributes and behaviors.
Object Oriented Programming
Model

Encapsulation - Encapsulation hides the implementation details of an


object and thereby, hides its complexity.
Data Abstraction – It hides the implementation of the data belonging to
an object.
Inheritance - Inheritance creates a hierarchy of classes and helps in
the reuse of the attributes and methods of class.
Polymorphism – Polymorphism triggers different reactions in objects in
response to the same message.
Encapsulation

Data
Hiding
Data Abstraction

Hiding
Implementation
details of the
Data
Inheritance

“Derived class”
inherits
features from
“Base Class”
History of Java

 Java began as Programming Language Oak.


 Oak - Developed by members of Green Project, which included James Gosling
and 2 more people in 1991 to create products for electronics market.
 Existing programming languages were not suited for the same.
 Chief Programmer of Sun Microsystems, James Gosling, was given the task of
creating software for controlling consumer electronic devices.
 The requirement of Software was to run on different kinds of computers,should
have power of networks and be based on web.
 Members of the Oak team realized that Java would provide the required cross
platform independence that is independent from hardware,network and OS.
Features of Java

 Java is an Internet programming language:


Java programs can be used to access data existing
across the network irrespective of the source platform
Java is secure:
Due to the strong type-checking done by Java any
changes made to the program are tagged as errors and
the program will not execute. Java is, therefore, secure
Java is a platform –independent language:
The Java compiler compiles Java code to an intermediate
bytecode that is understood by the Java environment. The
bytecode can be executed by the Java interpreter or any
Java-enabled Internet browser
Due to the bytecode compilation process and its
interpretation by a browser, Java programs can be executed
on various hardware and operating systems
Java is a high-performance language:
Java programs are comparable in speed to the programs
written in other compiler-based languages like C and C++.
Java programs are faster than the programs written in other
interpreter-based languages, such as BASIC
Java is simple:
The syntax of Java is similar to C++. Java does not support
pointers, multiple inheritance, goto statement, and operator
overloading
Features that slow down application development cycle
have been omitted in Java
Java Memory Management

Java prevents errors that arise due to improper memory


usage because in Java, programmers do not need to
manipulate memory
Java vs. C++:
Java is a pure object-oriented language because every
statement in Java is written inside a class. In C++, the
main() method is always written outside any class
In Java, all the data types except the primitive types are
objects. Even the primitive data types can be
encapsulated inside classes
Data types:
Java has two additional primitive data types besides the
ones available in C++
The byte data type of Java occupies one byte of memory space
and can store integral numbers
 The other data type boolean can store any one of the two
boolean values, true and false
 Java omits pointers and structs that are available in C++
 In Java, the char data type stores characters in Unicode
format unlike the 8-bit ASCII format in C++. Unicode is a
16-bit character format that can store Asian language
alphabets
 Data types in Java have a fixed size, regardless of the
operating system used
Constructs:
In Java, the constructs such as if...else, while, and
do...while take expressions that result in a boolean value
The switch construct and the for loop in Java are similar to
their counterparts in C++
Inheritance:
Java does not support multiple inheritance
All classes in Java are derived from Object class
To inherit from a class, you use the extends keyword
 Method Overloading:
Java allows method overloading but does not support
operator overloading
 Arrays:
In C++, an array is a collection of elements where as
in Java, arrays are real objects because you can
allocate memory to an array using the new operator
In Java, array accesses are checked to ensure that the
indices are within the range of the array
Java Programs

Java programs fall into two categories, Applications and Applets


Applications

 Execute from any operating system prompt


 They can be either window-based applications or
console applications
 They have more security privileges than applets
 They reside on the hard disk of a local machine
Java Programs (Contd.)
Applets

Execute in a Java-enabled browser


They are window-based
They can access the resources of the host machine
only, they cannot access the files on the computer to
which they are downloaded
They reside on a remote machine
Sample Application:- Welcome.java

public class Welcome

public static void main(String args[])

System.out.println(“Hello”);

}
In Java, the main() method and the class in which it is
defined should be declared public because the Java
runtime environment has to access the main() method to
execute a program
The main() method should be declared static because it
has to exist before any object of the class is created
The command line parameter is a String type variable –
main(String args[]). The number of arguments is
determined by the String class object
The String Object:
In Java, a String is a real object and not an array of null-
terminated characters as in C++
In Java, the String objects are consistent. The way in
which strings are obtained and the string elements are
accessed is consistent across all systems. They have well-
defined programming interfaces
In Java, the String objects are reliable. They do not give
rise to memory leaks in a program
Java uses all the system resources such as the display device
and the keyboard with the help of the System class. This class
contains all the methods that are required to work with the
system resources
The out object encapsulated inside the System class
represents the standard output device. This object contains the
println() method
The println() method displays data on the screen
Executing a Java Program:
Java programs are executed by a program called the Java
Virtual Machine (JVM). The JVM contains the runtime
environment and the class loader
You must save a Java source file with the .java extension.
The name of the file should be the same as the public class
name
When you compile a .java file, a .class file is created
To compile a file, you use the javac utility.
To execute a .class file, you use the java utility
For example, to compile and execute a class Welcome:
Save the file as Welcome.java
At the command prompt, type
javac Welcome.java <Enter>
At the command prompt, type
java Welcome <Enter>

You might also like