java programming presented by daniel rosenthal friday, november 30 th, 2007

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

Upload: jayson-hood

Post on 01-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

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

Java Programming

Presented by Daniel Rosenthal

Friday, November 30th, 2007

Page 2: 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• The Java API’s (programming libraries)• The various Java platforms (SE, EE, ME)

Page 3: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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 Kitfrom version 1.2 through version 5.0 (also called 1.5).

Page 4: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 5: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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 Yellinhttp://java.sun.com/docs/books/jvms/

• The Java APIChanges with each versionhttp://java.sun.com/javase/6/docs/api/

Page 6: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Types of Files

• .java – Java source code file

• .class – Java class file (executable binary file)

Page 7: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Programming in Java

• All Java code must be within a class and must be stored in a .java file

Program.java:

public class Program{}

Page 8: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 9: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Programming in Java

• Every class defines a template for creating new variables that program’s can use.

Program.java:

public class Program{}

Page 10: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 11: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 12: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 13: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Writing a Program

• Adding a main(String[]) method to the class Program:Program.java:

public class Program{ public static void main(String[] args) { }}

Page 14: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Writing a Program

This program will do nothing and exit.

Program.java:

public class Program{ public static void main(String[] args) { }}

Page 15: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Compiling from the command line

• To compile from the command line, type:

> javac <name of source code file>

• In this case:

> javac Program.java

Page 16: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Compiling from the command line

For example:

Page 17: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Compiling from the command line

For example:

Page 18: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Compiling from the command line

For example:

Page 19: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 20: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Running from the command line

This program did nothing, and exited.

Page 21: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 22: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

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

Page 23: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Primitive VariablesProgram.java:

public class Program{ public static void main(String[] args) { int myVariable; }}

Page 24: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

public class Program{ public static void main(String[] args) { Program myReference; }}

Reference VariablesVariables of a class type are called references

and are declared similarly to primitive variables:

Program.java:

Page 25: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

public class Program{ public static void main(String[ ] args) { int myVariable; myVariable = 5; }}

Variable AssignmentVariables can be assigned to by using the

assignment operator “=”.

Program.java:

Page 26: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

public class Program{ public static void main(String[ ] args) { int myVariable = 5; }}

Variable AssignmentAn initializer is a value assigned to a variable in

the same line that the variable is declared.

Program.java:

Page 27: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

Instance VariablesReference 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(); }}

Page 28: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

public class Program{ public static void main(String[ ] args) { Program myReference; myReference = new Program(); }}

Instance VariablesInstance variables are variables of user defined

types, i.e. variables of class types.

Program.java:

Page 29: Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007

public class Program{ public static void main(String[ ] args) { Program myReference; myReference = new Program(); }}

Instance VariablesInstance variables are created using the new

keyword.

Program.java: