java Java Java is a programming language developed

  • Slides: 11
Download presentation
java

java

Java • Java is a programming language developed by Sun Microsystems in 1995. •

Java • Java is a programming language developed by Sun Microsystems in 1995. • Java is one of the first languages to be platform independent. – Java is compiled to byte-code that runs on a Java interpreter

Java VM • A Java program never executes directly (i. e. , natively) on

Java VM • A Java program never executes directly (i. e. , natively) on a machine; instead the Java interpreter reads the byte code and executes the corresponding native machine instructions.

Java API • To run Java programs on a computer, all that is needed

Java API • To run Java programs on a computer, all that is needed is the interpreter (java virtual machine) and some library routines (the API)

The Java API • The Java API is the code that comes with the

The Java API • The Java API is the code that comes with the java interpreter (a. k. a. the java virtual machine) when you download java. • The java API is a collection of Java classes---take a look at the API for Java SE 6 • See if you can find the String class.

A Simple Java Program public class Hello. World { public static void main(String[] args)

A Simple Java Program public class Hello. World { public static void main(String[] args) { System. out. println("Hello World!"); } } • This program prints Hello World! to the monitor • Things to notice: – All programs are classes – All programs have a method called main() that has one array of Strings parameter.

Running Java outside of Processing Follow these steps to run Hello. World 1. Download

Running Java outside of Processing Follow these steps to run Hello. World 1. Download Hello. World. java and save it to your Desktop. 2. Open a terminal window. 3. In the terminal window, use the command cd to change to the Desktop directory where Hello. World. java is stored. 4. In the terminal window, type javac Hello. World. java to compile your program to byte code 5. In the terminal window, type java Hello. World to run your compiled program on the java virtual machine.

In-class exercise • Run the following program and explain what happens; how is the

In-class exercise • Run the following program and explain what happens; how is the output generated? • The program requires 3 class: Pets. java, Cat. java, and Dog. java • These classes are shown on the following slides.

Cat Class public class Cat { public void hiss () { System. out. println

Cat Class public class Cat { public void hiss () { System. out. println ("Hiss!"); } public void scratch (Dog victum) { System. out. println ("I'm scratching the dog"); victum. growl (); } public void bite (Dog silly. Dog) { System. out. println ("I'm bitting the dog"); silly. Dog. yelp (); scratch (silly. Dog); } }

Dog Class public class Dog { public void bark () { System. out. println

Dog Class public class Dog { public void bark () { System. out. println ("Arf!"); } public void growl () { System. out. println ("Grrrr!"); } public void yelp () { System. out. println ("Awooo!"); } public void been. Bitten. By (Cat silly. Cat) { System. out. println ("I've been bitten by a cat with a mean hiss: "); silly. Cat. hiss (); } }

Pets Class public class Pets { /* Creates a Cat and a Dog */

Pets Class public class Pets { /* Creates a Cat and a Dog */ public static void main (String [] args) { Cat tom = new Cat (); // create Cat object Dog spike = new Dog (); // create Dog object // demonstrate Cat behavior tom. bite (spike); System. out. println (); // Skip a line of output // demonstrate Dog behavior spike. been. Bitten. By (tom); } }