Introduction to Java
Introduction
Why Java?
Java Virtual Machine
HelloWorld (standalone)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Comments are almost like C++
Primitive data types are like C
Expressions are like C
Control statements are like C
Control statements II
switch (n + 1) {� case 0: m = n - 1; break;� case 1: m = n + 1;� case 3: m = m * n; break;� default: m = -n; break;�}
Java isn't C!
Java program layout
import java.awt.*;�import java.util.*;
public class SomethingOrOther {� // object definitions go here� . . .�}
This must be in a file named SomethingOrOther.java !
What is a class?
So, what is a class?
Name conventions
The class hierarchy
An example of a class
class Person {� String name;� int age;
void birthday ( ) {� age++;� System.out.println (name + ' is now ' + age);� }�}
Another example of a class
class Driver extends Person {� long driversLicenseNumber;� Date expirationDate;�}
Creating and using an object
An array is an object