Professional Documents
Culture Documents
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
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.
__________
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)
Class Libraries (+templated classes) Standard library Custom libraries Platform specific libraries
Function Libraries Standard functions Custom libraries O/S functions Java Native Interface
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)
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
10
Java Buzzwords
Simple Object Oriented Architecture Neutral Portable Multithreaded High Performance JIT
11
FREE
12
13
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
15
YES!
Every bit of code in a Java program is in a Class
16
17
YES!
How it works!
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
Java Compiler
Runtime System
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
Phases
Compile
Translates program into bytecodes, understood by Java interpreter javac command: javac myProgram.java Creates .class file, containing bytecodes (myProgram.class)
23
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
Execute
Computer (controlled by CPU) interprets program one bytecode at a time Performs actions specified in program
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
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.
27
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
On command line
javac file.java
32
Executing Applications
On command line
java classname
33