java
Document Sample


Java
Steve Haroz
How does Java work?
• Code (similar to c++) compiled to byte
code
• Byte code runs on a virtual machine
• A virtual machine is created for each OS
• The same byte code (i.e. executable or
library) can run on any Java VM
Java vs. C++
• Efficiency C++
• Portability Java
• Object Oriented Both
• Strongly Typed Java
• Pointers C++
• Garbage Collection Java
• Consistent Primitives Java
Java does not have…
• Header files
• Macros
• Pointers and references
• Unions
• Operator overloading
• Templates (maybe?)
Hello World
public class HelloWorld {
public static void main(String[] args) {
String s = "Hello World";
char c = '!';
s = s + c;
for (int i = 0; i < 5; i++)
System.out.println(i);
System.out.println(s);
}
}
Compile and Run
• Compile
javac HelloWorld.java
Output File: HelloWorld.class
• Run
java HelloWorld
Output: Hello, World
Arrays and Objects
// notice location of brackets
String[] days = new String[] {"Su", "Mo",
"Tu", "We", "Th", "Fr", "Sa"};
Object x;
x = new String[] {"Su", "Mo", "Tu", "We",
"Th", "Fr", "Sa"};
Exceptions
try {
f();
} catch (MyException e) {
e.printStackTrace();
} catch (YourException e) {
e.printStackTrace();
} finally {
... // always executed
}
Next few slides blatantly stolen from
Stoney Jackson
How are instance methods defined?
Instance methods take an implicit parameter:
instance on which method is invoked
public class Movie {
public int script, acting, directing;
public int rating() {
return script + acting + directing;
}
}
public class Demo {
public static void main (String argv[]) {
Movie m = new Movie();
m.script = 6; m.acting = 9; m.directing = 8;
System.out.print(“The rating of this movie is”);
System.out.println(m.rating());
}
}
How to extend classes?
Inheritance: mechanism for extending behavior
of classes; leads to construction of hierarchy of
classes [Note: no multiple inheritance]
What happens when class C extends class D:
Inherits instance variables
Inherits static variables
Inherits instance methods
Inherits static methods
C can:
Add new instance variables
Add new methods (static and dynamic)
Modify methods (only implementation)
Cannot delete anything
How to extend classes?
public class Attraction {
public int minutes;
public Attraction() {minutes = 75;}
public int getMinutes() {return minutes;}
public void setMinutes(int d) {minutes = d;}
}
public class Movie extends Attraction {
public int script, acting, directing;
public Movie() {script = 5; acting = 5; directing = 5;}
public Movie(int s, int a, int d) {
script = s; acting = a; directing = d;
}
public int rating() {return script + acting + directing;}
}
public class Symphony extends Attraction {
public int playing, music, conducting;
public Symphony() {playing = music = conducting = 5;}
public Symphony(int p, int m, int c) {
playing = p; music = m; conducting = c;
}
public int rating() {return playing + music + conducting;}
}
What are abstract classes?
Abstract class: Merely a place holder for class
definitions; cannot be used to create instances.;
public abstract class Attraction {
public int minutes;
public Attraction() {minutes = 75;}
public int getMinutes() {return minutes;}
public void setMinutes(int d) {minutes = d;}
public abstract void m();
}
Following is an error:
Attraction x;
x = new Attraction();
Following is not an error:
public class Movie extends Attraction { … }
public class Symphony extends Attraction { … }
Attraction x;
x = new Movie ();
x = new Symphony();
Java Does Not Have Const
public class MyClass
{
public static final int x = 5; // const
// Members and methods…
}
Your Own Exception
public class MyException extends Exception
{
public MyException() {}
public MyException(String message)
{
super(String message);
}
}
Java Resources
• Eclipse - www.eclipse.org – good IDE
• Java API -
java.sun.com/j2se/1.5.0/docs/api/
• Google -
directory.google.com/Top/Computers/Prog
ramming/Languages/Java/
Get documents about "