Chapter One
Introduction
Key Objectives
• In this chapter students are able to:
– Understand, the history of java, types of
programming paradigm.
– Get insight of java features
– Understand the java program life cycle
– Develop Simple java program
Types of programming paradigm
• Unstructured Programming Languages: The most primitive
of all programming languages having sequentially flow of
control.
– Code is repeated throughout the program
• Structured Programming Languages: Has non-sequentially
flow of control.
– Use of functions allows for re-use of code.
• Object Oriented Programming: Combines Data & Action
Together
Unstructured Programming Languages
• The earliest of all programming language
• Simple banking application example
int account_number = 20;
int account_balance= 100;
account_balance= balance+100;
Printf(“Account number=%d”, account_number);
Printf(“Account_number=%d”, account_balance);
account_balance=account_balance-50;
Printf(“Account number=%d”, account_number);
Printf(“Account_number=%d”, account_balance);
Unstructured Programming Languages
• This paradigm was used in earlier versions of BASIC,
COBOL, and FORTRAN
Structured Programming
• With the arrival of Structured programming repeated lines
on the code were put into structures such as functions or
methods.
int account_number = 20;
Void showData(){
int account_balance= 100;
Printf(“Account number=%d”,
account_balance=account_ba account_number);
lance+100;
Printf(“Account_number=%d”,
account_balance=account_ba account_balance);
lance-50;
}
Calling
showData(); made to
the function
C language and Pascal are some examples of Structural
Programming languages
Object-Oriented Programming
Class Account{
int account_number;
int account_balance;
public void showdata(){
system.out.println(“Account Number”+account_number);
system.outprintln(“Account Balance”+ account_balance);
}
}
int account_number = 20;
data
int account_balance= 100;
account_balance=account_balance+100;
showData();
account_balance=account_balance-50; Actions
showData();
Object-Oriented Programming
•By combining data and action, we will get many
advantages over structural programming with,
Abstraction
Encapsulation
Inheritance
Polymorphism
History of java
• Java is developed by Sun Microsystems in 1991.
• Java is originally developed to support platform
independent language that could be embedded in
consumer electronic devices.
• java is now used to develop large-scale enterprise
applications, provide applications for consumer
devices (e.g., cell phones, pagers and personal digital
assistants) and for many other purposes.
Top computer languages on July 2020
Source: http://statisticstimes.com/tech/top-computer-languages.php
Features of java
• In this section we will see some of the excellent features of
java programming language
1. Simple:
– Java syntax is based on C++ (so easier for programmers to learn
it after C++).
– Java has removed many complicated and rarely-used features, for
example, explicit pointers, operator overloading, etc.
– There is no need to remove unreferenced objects because there is
an Automatic Garbage Collection in Java.
Features of java
2. Object Oriented: Java is completely an object-
oriented programming language.
– Everything in Java is an object.
3. Platform Independent: Java is a write once, run
anywhere language.
– So, Java code can be run on multiple platforms,
for example, Windows, Linux, Sun Solaris,
Mac/OS, etc.
Features of java
4. secured: With Java, we can develop virus-free systems.
– Class loader, byte verifier, security manager
5. Robust:
• It uses strong memory management.
– There is automatic garbage collection in java
– There are exception handling and the type checking
mechanism in Java
Features of java
6. Architecture-neutral: in java, there are no implementation
dependent features, for example, the size of primitive types is
fixed.
7. Portable: you can carry Java bytecode to any platform
8. Distributed:
– This feature of Java makes us able to access files by
calling the methods from any machine on the internet.
9. multi-threaded:
– We can write Java programs that deal with many tasks at
once by defining multiple threads
Components of java
• How does it work?
– Bytecode
– Java Virtual Machine (JVM)
– Java Compiler generates architecture neutral byte-codes
– Java Virtual Machine interprets the byte-codes
– Byte-codes can be ported to any machine
– JVM It converts Java bytecode into machines language.
– JVM is a part of Java Run Environment (JRE).
Java Development Environment
• There are many programs that support the development of
Java software, including:
– Sun Java Development Kit (JDK)
– Sun NetBeans
– IBM Eclipse
– Borland JBuilder
– MetroWerks CodeWarrior
– BlueJ
– jGRASP
– IntelliJ IDEA
Skeleton of Java program
• The fundamental structure of any Java – //The file containing this class
program should look like: must be named Demo.java
[package declarations] – “class” is Key word
[import statements] – “Demo.java” is file name
[class declaration] your java application
• An example is given below:
– “abc “ is a package name. It
package abc;
is used to put related classes
import java.lang;
together( we’ll see in the later
public class Demo { sections)
public static void main(String[] args) {
Sytem.out.println("Hello! World");
}
}
Skeleton of Java program
• Sytem.out.println("Hello! World");
– System is a built-in class present in java.lang package.
– This class has a final modifier, which means that, it
cannot be inherited by other classes.
– It contains pre-defined methods and fields, which
provides facilities like standard input, output, etc.
– out is a static final field (ie, variable) in System class
which is of the type PrintStream (a built-in class,
contains methods to print the different data values).
– static fields and methods must be accessed by using the
class name, so ( System.out ).
Skeleton of Java program
– out here denotes the reference variable of the type
PrintStream class.
– println() is a public method in PrintStream class to
print the data values.
– Hence to access a method in PrintStream class, we
use out.println() (as non static methods and fields can
only be accessed by using the reference variable)
Your First Java Program
• Open your text-editor and type the following piece of Java
code exactly:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
• Save this file as HelloWorld.java (watch capitalization) in the
following directory:
c:\java
20
Compiling and Running Your First Program
• Open the command prompt in Windows
• To run the program that you just wrote, type at the command prompt:
cd c:\java
• Your command prompt should now look like this:
c:\java>
• To compile the program that you wrote, you need to run the Java
Development Tool Kit Compiler as follows:
At the command prompt type:
c:\java> javac HelloWorld.java
• You have now created your first compiled Java program named
HelloWorld.class
• To run your first program, type the following at the command prompt:
c:\java>java HelloWorld
Although the file name includes the .class extension , this part of the
name must be left off when running the program with the Java interpreter.
21
Creating, Compiling, and Running Programs
22
Types of Java Program
• All Java programs can be classified as Applications
and Applets.
• Java applications are large programs that run stand-
alone, with the support of a virtual machine.
• Applet is a relatively small Java program executed
under the by a browser (inside an applet viewer).
• Applets are great for creating dynamic and
interactive web applications.
23
Types of Java Program…
• Applets on client-side and servlets on server-side
makes Java a truly "Internet-based language".
• To execute applets, the browsers come with JRE (Java
Runtime Environment). The browsers with Java
Runtime Environment (or to say, JVM) loaded are
known as Java enabled browsers.
• Note: Browser do not have a Java compiler as a
compiled applet file (.class file) is given to browser to
execute.
24
Types of Java Program…
Advantages of Applets :
• Execution of applets is easy in a Web browser and does
not require any installation or deployment procedure in
real time programming (where as servlets require).
• Writing and displaying (just opening in a browser)
graphics and animations is easier than applications.
• In GUI development, constructor, size of frame,
window closing code etc. are not required (but are
required in applications).
25
Types of Java Program…
26