1 / 11

java

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.

alexia
Télécharger la présentation

java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. java

  2. 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

  3. 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.

  4. 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)

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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); } }

  10. 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 (); } }

  11. 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); } }

More Related