Chapter 1 | Introduction to Java
Java History
• 1991 - Green Team started by Sun Microsystems (now
owned by Oracle Corporation).
• *7 Handheld controller for multiple entertainment
systems.
• There was a need for a programming language that
would run on various devices.
• Java (first named Oak) was developed for this purpose.
Java Applications and Applets
• Java programs can be of two types:
• Standalone Applications
• Stand-alone programs that run without the aid of a web browser.
• Relaxed security model since the user runs the program locally.
• Can be command-line or GUI-based (using frameworks like Swing or JavaFX)
• Web Based Applications (Formerly Applets)
• Applets were small applications that require the use of a Java enabled web browser to
run.
• Applets are not supported by many web browsers these days for security reason and
are deprecated since Java 9 (2017).
• Java now supports the Spring Framework, a comprehensive solution for building
enterprise applications, particularly for server-side logic in web development.
Program Development Process
Portability
• With most programming languages,
portability is achieved by compiling
a program for each CPU it will run
on.
• Java provides an J V M for each
program
platform so that programmers do not
have to recompile for different
platforms.
compiler compiler
• In general, different operating
systems (Windows, Macs, Unix …) compiler
require different machine code,
hence you must compile most Unix
programming languages separately Win
MAC
for each platform.
Portability
• Java is a little different.
• Java compiler produces bytecode not
machine code.
• Bytecode can be run on any computer with
the Java Virtual Machine installed. Win
Java Program Java Bytecode
MAC
compiler Interpreter
Unix
JDK vs JRE vs JVM
JDK= Java Development Kit
JDK is the core component of Java Environment
and provides all the tools, executables and
binaries required to compile, debug and execute a
Java Program. JDK is a platform-specific software
and that’s why we have separate installers for
Windows, Mac, and Unix systems
JVM= Java Virtual Machin
JVM is responsible for converting Byte code to the
machine specific code. JVM is also platform
dependent and provides core java functions like
memory management, garbage collection, security
etc. It and provides platform independence. JDK and
JRE both contains JVM so that we can run our java
program.
JRE= Java Runtime Envirment
JRE is the implementation of JVM, it provides a platform to execute java programs. JRE
consists of JVM and java binaries and other classes to execute any program successfully.
Java Versions
• The software you use to write Java programs is called the
Java Development Kit, or JDK.
• There are different editions of the JDK :
• Java SE - Java2 Standard Edition.
• Java EE - Java2 Enterprise Edition.
• Java ME - Java2 Micro Edition.
• Java is originally developed by Sun Microsystems and now
owned by Oracle Corporation.
• JDK is available for download at
[Link]
Advantages and Disadvantages of Java
Advantages:
• Java is platform independent. Once it's compiled, you can run the bytecode
on any machine with a Java interpreter. You do not have to recompile for
each platform.
• Java is safe. Certain common programming bugs and dangerous operations
are prevented by the language and compiler.
• Java standardizes many useful operations like managing network
connections and providing graphical user interfaces.
Disadvantages:
• Running bytecode through the interpreter is not as fast as running machine
code, which is specific to that platform.
• Because it is platform independent, it is difficult to use platform specific
features (e.g., Windows taskbar, quick launch) in Java.
• Java interpreter must be installed on the computer in order to run Java
programs.
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) {
[Link]("Hello World!");
}
}
• Save this file as [Link] (watch capitalization)
in a directory:
c:\java
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 [Link]
• You have now created your first compiled Java program named
[Link]
• 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.
Object-Oriented Programming
• Java is an object-oriented programming
language
• For the rest of this lecture, we’ll introduce you
to the basic principles of object-oriented
programming.
• We won’t be using these principles
immediately, but they will become important
over the next few weeks.
OOP Concepts
• In object-oriented programming (OOP), programs
are organized into objects
• The properties of objects are determined by their
class
• Objects act on each other by passing messages
Object
• Definition: An object is a software bundle
that has State and Behavior.
• Software Objects are often used to model
real-world objects.
• Example: dogs have states (name, color,
hungry, breed) and behaviors (bark, fetch,
and wag tail).
Class
• Definition: A class is a blueprint that defines the states
and the behaviors common to all objects of a certain
kind.
• In the real world, you often have many objects of the
same kind. For example, a guard dog, herding dog,
snoop dog . . .
• Even though all dogs have four legs, and bark, each
dog’s behavior is independent of other dogs.
• For example: Dog #1 is a black Poodle, Dog #2 is a red
Irish Setter
Examples
• Example 1: Dogs
• States: name, color, breed,
and “is hungry?”
• Behaviors: bark, run, and wag
tail
• Example 2: Cars
• States: color, model, speed,
direction
• Behaviors: accelerate, turn,
change gears
Message
• Definition: Software objects interact and
communicate with each other by sending messages
to each other.
• Example: when you want your dog to gather a herd of
goats, you whistle and send him out.