110 likes | 549 Views
java. 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.
E N D
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 aJava interpreter
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 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 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 HelloWorld { 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 HelloWorld • Download HelloWorld.java and save it to your Desktop. • Open a terminal window. • In the terminal window, use the command cd to change to the Desktop directory where HelloWorld.java is stored. • In the terminal window, type javac HelloWorld.java to compile your program to byte code • In the terminal window, type java HelloWorld to run your compiled program on the java virtual machine.
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 ("Hiss!"); } public void scratch (Dog victum) { System.out.println ("I'm scratching the dog"); victum.growl (); } public void bite (Dog sillyDog) { System.out.println ("I'm bitting the dog"); sillyDog.yelp (); scratch (sillyDog); } }
Dog Class public class Dog { public void bark () { System.out.println ("Arf!"); System.out.println ("Arf!"); } public void growl () { System.out.println ("Grrrr!"); } public void yelp () { System.out.println ("Awooo!"); } public void beenBittenBy (Cat sillyCat) { System.out.println ("I've been bitten by a cat with a mean hiss:"); sillyCat.hiss (); } }
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.beenBittenBy (tom); } }