1 / 29

Java Programming

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

magda
Télécharger la présentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming Presented by Daniel Rosenthal Friday, November 30th, 2007

  2. 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)

  3. 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).

  4. 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

  5. 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/

  6. Types of Files • .java – Java source code file • .class – Java class file (executable binary file)

  7. Programming in Java • All Java code must be within a class and must be stored in a .java file Program.java: publicclass Program { }

  8. 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 { }

  9. Programming in Java • Every class defines a template for creating new variables that program’s can use. Program.java: publicclass Program { }

  10. 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 { }

  11. 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

  12. 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) {...}

  13. Writing a Program • Adding a main(String[]) method to the class Program: Program.java: publicclass Program {publicstaticvoid main(String[] args) { } }

  14. Writing a Program This program will do nothing and exit. Program.java: publicclass Program {publicstaticvoid main(String[] args) { } }

  15. Compiling from the command line • To compile from the command line, type: > javac <name of source code file> • In this case: > javac Program.java

  16. Compiling from the command line For example:

  17. Compiling from the command line For example:

  18. Compiling from the command line For example:

  19. 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

  20. Running from the command line This program did nothing, and exited.

  21. Variables • Classes define new variable types. Example variable types are int, float, double, etc. Program.java: publicclass Program {publicstaticvoid main(String[] args) { } }

  22. 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) { } }

  23. Primitive Variables Program.java: publicclass Program {publicstaticvoid main(String[] args) { int myVariable; } }

  24. 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; } }

  25. Variable Assignment Variables can be assigned to by using the assignment operator “=”. Program.java: publicclass Program {publicstaticvoid main(String[ ] args) { int myVariable; myVariable = 5; } }

  26. 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; } }

  27. 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(); } }

  28. 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(); } }

  29. Instance Variables Instance variables are created using the new keyword. Program.java: publicclass Program {publicstaticvoid main(String[ ] args) { Program myReference; myReference = new Program(); } }

More Related