290 likes | 663 Views
Java Programming. Presented by Daniel Rosenthal Friday, November 30 th , 2007. Java. Java is a platform and a programming language Originally implemented by James Gosling, first publicly released in 1995 Defined by Java: The Java programming language The Java Virtual Machine
E N D
Java Programming Presented by Daniel Rosenthal Friday, November 30th, 2007
Java • Java is a platform and a programming language • Originally implemented by James Gosling, first publicly released in 1995 Defined by Java: • The Java programming language • The Java Virtual Machine • The Java API’s (programming libraries) • The various Java platforms (SE, EE, ME)
Java Terminology • Java Virtual Machine (JVM) – an abstract machine architecture • Java Runtime Environment (JRE) – Sun’s implementation of the JVM • Java Development Kit (JDK) – Software Development Kit for Java The Java Runtime Environment and the Java Development Kit were called the Java 2 Runtime Environment and the Java 2 Development Kit from version 1.2 through version 5.0 (also called 1.5).
To program in Java… • Download and install the latest Java Development Kit (JDK 6) • The most recent version is Java SE 6, which was released December 11, 2006. • The next release is Java SE 7 and will not be released until at least the Summer of 2008
Defining Documents • The Java Language Specification, Third Edition by James Gosling, Bill Joy, Guy Steele, and Gilad Bracha (Published: 2005, ISBN: 0321246780) http://java.sun.com/docs/books/jls/index.html • The Java Virtual Machine Specification, Second Edition (Published: 1999, ISBN: 0201432943) by Tim Lindholm and Frank Yellin http://java.sun.com/docs/books/jvms/ • The Java API Changes with each version http://java.sun.com/javase/6/docs/api/
Types of Files • .java – Java source code file • .class – Java class file (executable binary file)
Programming in Java • All Java code must be within a class and must be stored in a .java file Program.java: publicclass Program { }
Programming in Java • There can be only one public class per source code file (.java), and it must have the same name as the source code file Program.java: publicclass Program { }
Programming in Java • Every class defines a template for creating new variables that program’s can use. Program.java: publicclass Program { }
The 8 Built-In Types • The types boolean, byte, short, int, long, float, double, and char are predefined by the JLS and are called the built-in types (also called primitive types) Program.java: publicclass Program { }
References (The 9th Built-In Type) • There is actually one more built-in type: the reference type. • Reference variables can refer to any user defined type
Writing executable code • Every Java program consists of one or more Java source files. • Every Java program begins execution at a main method • Every main method must have the following signature: publicstaticvoid main(String[] args) {...}
Writing a Program • Adding a main(String[]) method to the class Program: Program.java: publicclass Program {publicstaticvoid main(String[] args) { } }
Writing a Program This program will do nothing and exit. Program.java: publicclass Program {publicstaticvoid main(String[] args) { } }
Compiling from the command line • To compile from the command line, type: > javac <name of source code file> • In this case: > javac Program.java
Compiling from the command line For example:
Compiling from the command line For example:
Compiling from the command line For example:
Running from the command line • To run the program, type: > java <name of class containing main method> • In this case: > java Program • Notice the absence of the .class extension
Running from the command line This program did nothing, and exited.
Variables • Classes define new variable types. Example variable types are int, float, double, etc. Program.java: publicclass Program {publicstaticvoid main(String[] args) { } }
Primitive Variables • To declare a variable of a primitive type, simply write the type followed by the desired variable name. Program.java: publicclass Program {publicstaticvoid main(String[] args) { } }
Primitive Variables Program.java: publicclass Program {publicstaticvoid main(String[] args) { int myVariable; } }
Reference Variables Variables of a class type are called references and are declared similarly to primitive variables: Program.java: publicclass Program {publicstaticvoid main(String[] args) { Program myReference; } }
Variable Assignment Variables can be assigned to by using the assignment operator “=”. Program.java: publicclass Program {publicstaticvoid main(String[ ] args) { int myVariable; myVariable = 5; } }
Variable Assignment An initializer is a value assigned to a variable in the same line that the variable is declared. Program.java: publicclass Program {publicstaticvoid main(String[ ] args) { int myVariable = 5; } }
Instance Variables Reference variables can only refer to one thing: instance variables. Program.java: publicclass Program {publicstaticvoid main(String[ ] args) { Program myReference; myReference = new Program(); } }
Instance Variables Instance variables are variables of user defined types, i.e. variables of class types. Program.java: publicclass Program {publicstaticvoid main(String[ ] args) { Program myReference; myReference = new Program(); } }
Instance Variables Instance variables are created using the new keyword. Program.java: publicclass Program {publicstaticvoid main(String[ ] args) { Program myReference; myReference = new Program(); } }