
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive guide on how to get started with java programming, including the required tools, mechanics of writing java programs, and running applications and applets. It also covers java basics, classes, objects, strings, arrays, and concurrent multi-threaded programming.
Typology: Slides
1 / 40
This page cannot be seen from the preview
Don't miss anything!

































Mechanics of Writing Java Programs
ļ§ Create a Java source file. Must have the .java extension and contain
only one public class.
ļ§ Compile the source file into a bytecode file. The Java compiler ,
javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a .class bytecode file.
ļ§ Run the program contained in the bytecode file. The Java VM is
implemented by a Java interpreter , java. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.
Putting it all together
public class Hello { public static void main(String args[]) { System.out.println(āHello, world!ā); } }
ļ§ Creates Hello.class
Java notes for C++ programmers
Requisite First Program (Application
Version)
Put in HelloWorld.java:
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
So whatās going on?
The Java bytecode and interpreter at work!
the program (the class file)
āVirtual Machineā
running its main() method
Requisite First Program (Applet Version)
Put in HelloWorld.java: import java.awt.Graphics; public class HelloWorld extends java.applet.Applet { public void paint(Graphics g) { g.drawString("Hello Worldā, 35, 15); } }
**Put in test.html:
Test the applet
Test the applet
**
Java Language Basics
Classes, References, & Packages
Exceptions
exception is generated.
that some condition or error has occurred but we
want to pass the buck and not deal with it.
ourselves inside the function/method.
by the compiler ļ compilation errors!)
Try/Catch/Finally
try {
// code that can throw an exception
} catch (ExceptionType1 e1) {
// code to handle the exception
} catch (ExceptionType2 e2) {
// code to handle the exception
} catch (Exception e) {
// code to handle other exceptions
} finally {
// code to run after try or any catch
}
This block is always run
Defining a Class
program with at least one containing a static
public main() method (if theyāre applications).
what the file name is.
object, initiailize its elements, and return a reference to
the object
A Sample Class
public class Point {
public Point(double x, double y) {
this.x = x; this.y=y;
}
public double distanceFromOrigin(){
return Math.sqrt(xx+yy);**
}
private double x,y;
}