java. java java is a programming language developed by sun microsystems in 1995. java is one of the...

11
java

Upload: lorena-preston

Post on 04-Jan-2016

229 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: 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

java

Page 2: 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

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

Page 3: 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

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.

Page 4: 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

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)

Page 5: 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

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.

Page 6: 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

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.

Page 7: 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

Running Java outside of Processing

Follow these steps to run HelloWorld

1. Download HelloWorld.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 HelloWorld.java is stored.

4. In the terminal window, type javac HelloWorld.java to compile your program to byte code

5. In the terminal window, type java HelloWorld to run your compiled program on the java virtual machine.

Page 8: 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

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.

Page 9: 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

Cat Classpublic 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); }}

Page 10: 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

Dog Classpublic 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 ();

}}

Page 11: 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

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);

}}