Java I Lecture 1

54
 Java I-- Copyright © 2000-2007 Tom Hunter  

Transcript of Java I Lecture 1

Page 1: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 1/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Page 2: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 2/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Page 3: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 3/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Chapter 1

 Introduction to Computers,

the Internet and the Web 

Page 4: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 4/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• It all started with an angry programmer. 1990 

Sun Microsystems Software

Engineer Patrick Naughton*,age 25, was disgusted with

his job at Sun. He had the impossible

 job of making different software APIs--from

dozens of languages, platform OS’s and

vendors--all work as one. It was impossible.

The Genesis of Java 

* After his stint at Sun, Mr. Naughton went on to work for Disney and, temporarily, for the FBI…. 

Page 5: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 5/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

The Genesis of Java 

 Naughton announced to CEO

Scott McNealy that he was quittingSun.

Pat was going to join NeXT,

 because Sun was a mess.

McNealy asked Pat to write a

memo outlining his gripes.The CEO asked Pat to suggest a

solution, “As if you were God.” 

Page 6: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 6/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

The Genesis of Java 

• Formation of the “Green Project” 

Jan 1991

The array of standards spurred the

formation of the “Green Project.” Its goalwas making Consumer Electronics devices 

talk to each other .

Since VCRs, Laser Disc Players and

Stereos were all made with different CPUs,

they all needed special programming.

Page 7: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 7/54 Java I-- Copyright © 2000-2007 Tom Hunter 

• James Gosling, then age 36, was asked to find a

 programming language for the project.

Gosling, who had left

IBM in 1984 to join Sun,

first chose C++. But hesoon gave up on C++,

which was incapable of doing

what he wanted. So, he started tomodify C++, (which is a direct

descendant of the C programming language).

The Genesis of Java 

Page 8: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 8/54 Java I-- Copyright © 2000-2007 Tom Hunter 

• Soon, Gosling was writing a new language,

which he named “Oak ” after the tree outsidehis window.

Oak  to had to be:

• Small to work on Consumer electronics,

• Platform independent, to avoid hassles like the

ones Naughton encountered,

• an Interpreted language,• Object Oriented,

The Genesis of Java 

Page 9: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 9/54 Java I-- Copyright © 2000-2007 Tom Hunter 

The Genesis of Java 

• Reliable--which made him remove aspects of C++:

  i.) No Multiple Inheritance--he used interfaces  instead

ii.) No Operator Overloading

iii.) No Manual Memory allocation and dealloc

  iv.) No Pointers--no pointer arithmetic* 

v.) No assignment in conditionals (== vs = )

and add  things C++ lacked:

i.) Implicit Garbage Collection--no memory leaks  ii.) Data Structures only in Objects 

iii.) Built in Security.

*

 Technically, Java still has pointers but they are calledreferences

 and cannot be used to do pointer arithmetic or any of the other shenanigans that make C programming such a joy.

Page 10: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 10/54 Java I-- Copyright © 2000-2007 Tom Hunter 

• Demo of *7, Programmed in Oak  

3 Sept 1992

This was the prototype of the

first device to use the Oak programming language.

The “Star7” also featured the

debut of “Duke,” the Java mascot. An early applet

showed Duke doing

cartwheels across the screen.

The Genesis of Java 

Page 11: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 11/54 Java I-- Copyright © 2000-2007 Tom Hunter 

The Genesis of Java 

• Oak becomes Java.

Jan 1995

By this time, the Internet had taken off.

Bill Joy, one of Sun’s founders, realized thatthe needs of the Web [ reliability, platform

independence, security ] exactly matched the

characteristics of Oak, which had just beenrenamed Java.

Page 12: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 12/54 Java I-- Copyright © 2000-2007 Tom Hunter 

The Genesis of Java 

• Java Catches Fire 

23 Mar 1995

Although Java had not yet been

officially released , it was spreading likewildfire among developers.

Then, something very lucky happened...

Page 13: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 13/54 Java I-- Copyright © 2000-2007 Tom Hunter 

Page 14: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 14/54 Java I-- Copyright © 2000-2007 Tom Hunter 

The Genesis of Java 

•Netscape Navigator 2.0 

23 May 1995

Two months later, at the SunWorld

conference, Marc Andreessen stepped onstage and announced that “Java is real, and it

will be incorporated into Netscape Navigator

2.0.” 

 At this moment, Sun’s entire Java team only

numbered 30 people.

Page 15: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 15/54 Java I-- Copyright © 2000-2007 Tom Hunter 

Java’s

Major Advantage

over C & C++ 

Page 16: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 16/54 Java I-- Copyright © 2000-2007 Tom Hunter 

• Because pointers were a major source of bugs in C and C++, Gosling omitted pointers

entirely from Java.

• Actually, pointers are still an important part

of the language--all objects are referenced by

 pointers--but the language handles them, not

the programmer. 

Java’s Major Advantage over C & C++ 

Page 17: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 17/54 Java I-- Copyright © 2000-2007 Tom Hunter 

“Java is C without

the Guns and

 Knives.” 

Thus, it has been said that...

Java’s Origins in C & C++ 

Page 18: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 18/54 Java I-- Copyright © 2000-2007 Tom Hunter 

• By now, Java itself has matured into its 7thversion, named Java 7.

• Java is Object-Oriented*--that means

everything in the language behaves like an

object.

• What exactly that means will be explainedin the coming lectures.

Java Architecture 

* Purists will say the presence of primitive variables makes this only 99.9% true.

Page 19: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 19/54 Java I-- Copyright © 2000-2007 Tom Hunter 

Java Architecture 

Java’s Architecture comes from four

separate but intertwined technologies:

• the Java Programming Language

  • the Java class file format • the Java API, or  Application Programming Interface

  • the Java Virtual Machine

Page 20: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 20/54 Java I-- Copyright © 2000-2007 Tom Hunter 

Java Architecture 

Source programs are written in the Java

Programming Language.

All procedural code falls within methods.

Programs are compiled into Java class files. 

Classes run in the Java Virtual Machine.

Page 21: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 21/54 Java I-- Copyright © 2000-2007 Tom Hunter 

Java Architecture 

• When a Java program runs,

it is assisted by other classes

in the Java Application

Programming Interface, or

API.

Page 22: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 22/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Java Architecture 

Hello.java 

Java

Compiler 

Hello.class 

Compile-Time

Environment 

Run-Time

Environment 

Java

Virtual

Machine 

Object.class  String.class 

Example Java API class files Combined, the Java

Virtual Machine and the

Java API form a“Platform.” 

The Java Platform

Page 23: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 23/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• The Java Platform is unique, because it can

work without modification  on

any platform, on any operating system, if   that

 platform has a “Java Virtual

Machine.” 

Java Architecture 

Page 24: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 24/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Comparison of a typical Procedural

 Program with a Java Program: 

• In a typical C program, the source code iscompiled  into a native machine language

module that consists of 1’s and 0’s. 

 What is the ?

Java Architecture 

 Java

 VirtualMachine 

Page 25: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 25/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

C object module

compiled intomachine language 

C Source Code

• The machine language is specifically tailored  

to one OS, be it Wintel, Mac, UNIX or MVS.

• Therefore, it is impossible for one object

module to be portable between platforms.

Page 26: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 26/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

In contrast to conventional programming

languages, a Java program is not  compiled

into native machine language.

• Instead, Java makes bytecode.

• Bytecode is the result of a “compile”, butthe compile results in an intermediate form

that stops short of native machine-specific

code.

Java “bytecode” 

Java Architecture 

Page 27: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 27/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• Instead of making a machine language

native code for each particular OS, Java

makes a single, universal bytecode module

that feeds into any  Java Virtual Machine (JVM).

• Each OS has its own differentimplementation of the

Java Virtual Machine.

Java Architecture 

Page 28: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 28/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• The JVM sets up its own world withinyour RAM.

• The JVM creates an internal software-only sub-computer within the OS.

• The bytecode talks to the JVM, and theJVM talks to the Operating System.

Java Architecture 

Page 29: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 29/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• Thus, you get the Holy Grail of software reuse: 

“Write Once, 

 Run Anywhere”. 

Java Architecture 

Page 30: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 30/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Java Source

Java Bytecode

Wintel Mac UNIX MVS

JVM-Win  JVM-Mac  JVM-Unix  JVM-IBM 

You can easily see why BillGates isn’t in love with Java! 

Thebytecode

is met

half-wayby the

 JVM.

Page 31: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 31/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• The Virtual Machine interprets the

 bytecode one instruction at a time, andtranslates it into native machine code.

• You compile your program once into bytecode, but it is interpreted  anew every

time it runs.

• JIT [Just In Time] compilers can cache

the compiled machine code.

Java Architecture 

Page 32: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 32/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Security and the

“Sandbox” 

Page 33: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 33/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Security and the “Sandbox” 

C and C++ are famous for speed.

• One reason they are fast is because C

and C++ don’t do things like checking the bounds of arrays.

• In C or C++, a program can walk off

the edge of an array and invade the memoryspace beyond.

• Hackers love that about C and C++.

Page 34: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 34/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Security and the “Sandbox” 

• Another weakness of C/C++, that is a

favorite among Hackers, is the BufferOverflow.

• In this attack, the Hacker floods too muchdata into a buffer and whatever overflows it is

turned loose on the system.

• Java solves these problems 

Page 35: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 35/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• How Java Combats malicious code: 

Java checks array boundaries

Java halts Buffer Overflows

Java has Garbage collection to get rid

of objects that are no longer used.

Java’s verifier checks to make sure 

the code is safe before it runs.

• Gosling built security into Java, using a

concept known as the “Sandbox.” 

Security and the “Sandbox” 

Page 36: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 36/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

JDK 1.2  Security Model

Vulnerable System Resources

(files, etc) Even Local Code is Not Trusted 

SANDBOX

Security and the “Sandbox”  Remote Code 

Has Fine-Grain Access Control

All Code, both Local and Remote, Must Pass Security Policy

Local Code 

Page 37: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 37/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Security and the “Sandbox” 

• 5 Steps To Writing A Java Program: 

1.) Write it in a Text Editor2.) Compiler  creates bytecode3.) The “Class loader ” places the .class

  file in memory.4.) The “Bytecode Verifier ” makes sure

the code adheres to Java’s security 

rules.5.) The JVM Interpreter reads bytecode

and makes platform native code.

Page 38: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 38/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Security and the “Sandbox” 

• You see, preventing  problems is a major

design consideration in Java. Other languagesdo this too but Java was built around it.

• This idea led to the most import aspect of

Java: Object Orientation.

• Object Orientation protects data and lets a

 program do only what is explicitly permitted.

• You could say Java is pessimistic.

Page 39: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 39/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Objects in Java 

• In Java, Object Orientation is so

 pervasive that it’s nearly impossible to

write a strictly procedural program inthe language.

Page 40: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 40/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Objects in Java 

• Objects are reusable components.

• In Java, everything must be run from a

“class” file. This “class” contains bytecode. 

• Java source code has the extension

Xxx. java

Page 41: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 41/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Objects in Java 

• If I write a Java program called: 

Hello.java 

then, when compiled, this program will

 be called:

Hello.class 

Page 42: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 42/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Objects in Java 

• A class object is compiled Java code that

contains its own data variables, calledmembers, and sections of procedural code

called methods.

If you have programmed in COBOL, a

method is like a paragraph you perform.

If you have programmed in C or C++, a

method is like a function your program calls.

Page 43: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 43/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Objects in Java 

• The combination of the data variables 

and the methods 

that are used to read,

writeor modify

those variables

is called a class.

Page 44: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 44/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Objects in Java 

• Java has a rich collection of Class Libraries.

• These are also known as the Java API or

 Application Programming Interface.

• To program in Java, you must 

i.) Learn the Language, and

ii.) Learn the Class Libraries.

Page 45: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 45/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Objects in Java 

• These class libraries greatly simplify your

 job as a Java programmer.

• They help you to write complex programsquickly.

• To master Java, you must master theseclass libraries.

Page 46: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 46/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

Compiling A Java Program 

• You have created a Java program called  Hello.java 

• To compile it, you run the JDK supplied

utility called:

javac

C:\javac Hello.java

If this was successful, a file called:Hello.class will be produced.

Page 47: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 47/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

• The two largest varieties of Java programs:

Applications

Applets

First Java Program 

i

Page 48: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 48/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

First Java Program 

• A Java Application is a free-standing program that is capable of running

directly in the Java Virtual Machine.

• A Java Applet is a mini-program that is

much more limited in its abilities. An

Applet can only run within the context ofan HTML browser.

Page 49: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 49/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

The double slashes denote a “C++”-style comment.

Everything on the line after  the double slashes is

ignored by the compiler.

A Java Application // HelloWorld.java Our first Java Application// HelloWorld.java Our first Java Application

public class HelloWorld  

{

}

// HelloWorld.java Our first Java Application

public class HelloWorld

{

public static void main( String args[])

{

System.out.println( “Hello World!” ); 

}}

Page 50: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 50/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

This is the class name.

Every single bit of code in Java must sit in curly brackets.

Class names are capitalized.Words within the name are also capitalized.

This Java program must be saved in a file with the exact

same name--matching the upper case--as you see in blueabove: HelloWorld.java 

A Java Application // HelloWorld.java Our first Java Application// HelloWorld.java Our first Java Application

public class HelloWorld  

{

}

// HelloWorld.java Our first Java Application

public class HelloWorld  

{

public static void main( String args[])

{

System.out.println( “Hello World!” ); 

}}

Page 51: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 51/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

 Now our Application is complete. We have added the

method “main”. All methods are lower case. main is a

special method--it actually runs the program.

In any application, you are always guaranteedthat method main will run.

A Java Application // HelloWorld.java Our first Java Application// HelloWorld.java Our first Java Application

public class HelloWorld  

{

}

// HelloWorld.java Our first Java Application

public class HelloWorld

{

public static void main( String args[])

{

System.out.println( “Hello World!” ); 

}}

A J A li ti

Page 52: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 52/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

A Java Application 

C:\>javac HelloWorld.java

C:\>

• A successful compile of your java

 program will return to a bare cursor, asyou see here.

A J A li ti

Page 53: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 53/54

 Java I-- Copyright © 2000-2007 Tom Hunter 

C:\>javac HelloWorld.java

C:\>java HelloWorld

Hello World!

A Java Application 

• To run your compiled Application,you enter lowercase java HelloWorld 

on the command line.• Notice, the “.class” extension is

omitted.

Page 54: Java I Lecture 1

8/10/2019 Java I Lecture 1

http://slidepdf.com/reader/full/java-i-lecture-1 54/54

 Now load the JDK. I recommend using th

most current one.Get the documentation,

change the class path and

write your first Java program.

In Class Activity