Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007."— Presentation transcript:

1 Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 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: public class 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: public class Program { }

9 Programming in Java Every class defines a template for creating new variables that program’s can use. Program.java: public class 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: public class Program { }

11 References (The 9 th 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: public static void main(String[] args) {...}

13 Writing a Program Adding a main(String[]) method to the class Program: Program.java: public class Program { public static void main(String[] args) { }

14 Writing a Program This program will do nothing and exit. Program.java: public class Program { public static void main(String[] args) { }

15 Compiling from the command line To compile from the command line, type: > javac 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 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: public class Program { public static void 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: public class Program { public static void main(String[] args) { }

23 Primitive Variables Program.java: public class Program { public static void main(String[] args) { int myVariable; }

24 public class Program { public static void main(String[] args) { Program myReference; } Reference Variables Variables of a class type are called references and are declared similarly to primitive variables: Program.java:

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

26 public class Program { public static void main(String[ ] args) { int myVariable = 5; } Variable Assignment An initializer is a value assigned to a variable in the same line that the variable is declared. Program.java:

27 Instance Variables Reference variables can only refer to one thing: instance variables. Program.java: public class Program { public static void main(String[ ] args) { Program myReference; myReference = new Program(); }

28 public class Program { public static void 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:

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


Download ppt "Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007."

Similar presentations


Ads by Google