Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
-
Upload
jayson-hood -
Category
Documents
-
view
214 -
download
0
Transcript of Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
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 Kitfrom 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 Yellinhttp://java.sun.com/docs/books/jvms/
• The Java APIChanges with each versionhttp://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:
public class 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:
public class Program{}
Programming in Java
• Every class defines a template for creating new variables that program’s can use.
Program.java:
public class 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:
public class 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:
public static void main(String[] args) {...}
Writing a Program
• Adding a main(String[]) method to the class Program:Program.java:
public class Program{ public static void main(String[] args) { }}
Writing a Program
This program will do nothing and exit.
Program.java:
public class Program{ public static void 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:
public class Program{ public static void 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:
public class Program{ public static void main(String[] args) { }}
Primitive VariablesProgram.java:
public class Program{ public static void main(String[] args) { int myVariable; }}
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:
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:
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:
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(); }}
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:
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: