You are on page 1of 33

Java

Write Once, Run Anywhere

Java - General

Java is:
Object Oriented programming language with a built-in application programming interface (API). Used to create applications. Platform independent programming language.

Object-Oriented

Java supports OOD


Polymorphism Inheritance Encapsulation

Java programs contain nothing but definitions and instantiations of classes


Everything is encapsulated in a class!
3

Difference b/w Java and C,C++

Much of the syntax of Java is the same as C and C++. One major difference is that Java does not have pointers. However, the biggest difference is that you must write object oriented code in Java. Procedural pieces of code can only be embedded in objects.
4

Developed in the early seventies, C became the de-facto language for microcomputer programming in the seventies and eighties.

C was not interpreted, it was compiled.

__________
Later, in the early eighties, C++ introduced object orientation to the C world. Originally called C with Classes, Bjarne Stroustrup developed the additions to C that, in 1985, became C++. OO is when programmers try to mimic the real world more closely and make programs act like objects and not just be grocery lists.
5

C++

What is C++?
Procedural C Global Functions File-specific functions Structs Pointers (addresses) Low-level memory access C Preprocessor Variables Arrays Loops Conditionals Classes - Grouping of related data together - With associated methods (functions) new for object creation delete for object destruction Constructors, Destructors Operator Overloading Assignment operators Conversion operators Inheritance (sub-classing) Virtual functions & polymorphism Access control (private/public/protected)

Function Libraries Standard functions Custom libraries O/S functions

Templates (Generic classes)


Non-C features e.g. References

Class Libraries (+templated classes) Standard library Custom libraries Platform specific libraries

What about Java?


Procedural C Global Functions File-specific functions Structs Pointers (addresses) Low-level memory access C Preprocessor Variables Arrays Loops Conditionals Classes - Grouping of related data together - With associated methods (functions) new for object creation delete for object destruction Constructors, Destructors Operator Overloading Assignment operators Conversion operators (toString()?) Inheritance (sub-classing) (ONLY) Virtual functions & polymorphism Access control (private/public/protected)

Function Libraries Standard functions Custom libraries O/S functions Java Native Interface

Templates Generics (weaker)


Non-C features (ONLY) references

Class Libraries (Standardised) Collections Networking Graphics

What Sun changed for Java


Remember: C++ came first


The Java changes were deliberate!

Java is cross-platform
Interpreted intermediate byte-code (.class files) Standard cross-platform class libraries Libraries include graphics (AWT, SWING, ), networking, Platform independent type sizes Cannot take advantage of platform-specific features Pointer arithmetic (but it can be fast) Writing outside arrays (checks take time) Low-level access to memory (dangerous per powerful) Uninitialised data (initialisation takes time)

Java prevents things which are potentially dangerous


Java forces you to use objects


Even when it would be quicker not to

Java does garbage collection for you


Safer(?), but may execute slower than freeing memory yourself
8

Say goodbye to the seventies and welcome to a short (but hopefully informative) history of the Java programming language
Authentic Seventies Nerds
9

James Gosling began developing Java beginning in 1991 It was first called Project Green and Oak First developed for remote cable TV boxes
James Gosling Circa 1971

James Naughton creates HotJava in 1995. its a web browser that lets you run Applets. The entire browser is written in Java.
In 1998 Java 1.2 is released Java just released version 1.6 or J2SE 6

James Gosling Today

10

Java Buzzwords
Simple Object Oriented Architecture Neutral Portable Multithreaded High Performance JIT

11

FREE
12

Is Java Interpreted or Compiled?

13

Is Java Interpreted or Compiled?

BOTH!!
Java programs are compiled to Bytecode Bytecode is then interpreted by a JVM, or Java Virtual machine. The virtual machine is what runs your program Its the JVM that cares about your Operating system, NOT THE PROGRAM! WORA - Write Once, Run Anywhere!
14

Is Java Object Oriented?

15

Is Java Object Oriented?

YES!
Every bit of code in a Java program is in a Class

Code Reuse, Encapsulation, Polymorphism, Inheritance

16

Is Java Machine Independent?

17

Is Java Machine Independent?

YES!

Java is independent only for one reason:


Only depends on the Java Virtual Machine (JVM), code is compiled to bytecode, which is interpreted by the resident JVM, JIT (just in time) compilers attempt to increase speed.
18

How it works!

Java is independent only for one reason:


Only depends on the Java Virtual Machine (JVM), code is compiled to bytecode, which is interpreted by the resident JVM, JIT (just in time) compilers attempt to increase speed.

19

Notice:
Java is CASE SENSITIVE!! Whitespace is ignored by compiler Whitespace makes things easier to readhence it affects your grade File name has to be the same as class name in file. Need to import necessary class definitions

20

How it works!
Compile-time Environment Compile-time Environment
Class Loader Java Source (.java) Java Bytecodes move locally or through network Bytecode Verifier Java Class Libraries

Java Interpreter

Just in Time Compiler

Java Compiler

Java Virtual machine

Runtime System

Java Bytecode (.class )

Operating System
21

Hardware

Java - Security
Pointer denial - reduces chances of virulent programs corrupting host, Applets even more restricted

May not
run local executables, Read or write to local file system, Communicate with any server other than the originating server.
22

Java programs have five phases


Edit
Use an editor to type Java program vi or emacs, notepad, Jbuilder, Visual J++ .java extension

Phases

Compile
Translates program into bytecodes, understood by Java interpreter javac command: javac myProgram.java Creates .class file, containing bytecodes (myProgram.class)
23

Java programs have five phases (continued)


Loading
Class loader transfers .class file into memory
Applications - run on user's machine Applets - loaded into Web browser, temporary

Classes loaded and executed by interpreter with java command java Welcome
HTML documents can refer to Java Applets, which are loaded into web browsers. To load, appletviewer Welcome.html
appletviewer is a minimal browser, can only interpret applets
24

Java programs have five phases (continued)


Verify
Bytecode verifier makes sure bytecodes are valid and do not violate security Java must be secure - Java programs transferred over networks, possible to damage files (viruses)

Execute
Computer (controlled by CPU) interprets program one bytecode at a time Performs actions specified in program

Program may not work on first try


Make changes in edit phase and repeat
25

Phase 1

Editor

Disk

Program is created in the editor and stored on disk. Compiler creates bytecodes and stores them on disk.

Phase 2

Compiler

Disk Primary Memory

Phase 3

Class Loader Class loader puts bytecodes in memory. . . . . . . Primary Memory Bytecode verifier confirms that all bytecodes are valid and do not violate Javas security restrictions.

Disk

Phase 4

Bytecode Verifier

Phase 5

Interpreter

. . . . . . Primary Memory

. . . . . .

Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as 26 the program executes.

Installing JDK 1.6 on Windows


1. Double click jdk-6-windows-i586.exe to run the installation program.

27

2. Click Accept to display the JDK Custom Setup dialog

28

3. You may install JDK in a custom directory. For simplicity, dont change the directory. Click Next to install JDK.

29

4. You may install JRE in a custom directory. For simplicity, dont change the directory. Click Next to install JRE. 5. After installation completed, Click Finish to close the dialog.

30

A Simple Program
public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Save file with .java extension
31

Creating and Compiling Programs

On command line
javac file.java

32

Executing Applications

On command line
java classname

33

You might also like