Download Getting Started with Java Programming and more Slides Algorithms and Programming in PDF only on Docsity!
Java Crash Course
A Tutorial Introduction for C++
Programmers
So what the heck is Java (besides coffee, that
is)?
- A programming language.
- Syntax and constructs are very similar to C++
- A virtual platform
- Java Virtual Machine is a software “machine” or “hypothetical
chip”
- Since it’s “virtual”, it can be implemented on any hardware
- Cross-platform distribution achieved via .class binary file of
bytecodes (instead of machine-dependent machine code)
Write Once, Run Anywhere
- A class library
- Standard APIs for GUI, data storage, processing, I/O, and
networking.
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!”); } }
- Put in: Hello.java
- Compile with: javac Hello.java
Creates Hello.class
- Run with: java Hello
Java notes for C++ programmers
- Everything’s an object
- Every object inherits from java.lang.Object
- No code outside of the class definition!
- No global variables (use static variables instead)
- Single inheritance only
- Instead, implement interfaces
- All classes are defined in .java files
- One top level public class per file
- The file has to have the same name as the public class!
- Syntax is similar (control structures are very similar).
- Primitive data types are similar
- But a bool is not an int
- To print to stdout, use System.out.println()
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!
- Bytecode is an intermediate representation of
the program (the class file)
- Think of it as the machine-code for the Java Virtual Machine
- The Java interpreter (java) starts up a new
“Virtual Machine”
- The VM starts executing the user’s class by
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
- Data types same as in C++ (except bool)
- bool,char,byte,short,int,long,float,
double,string, etc.
- Operators (same as C++)
- Assignment: =, +=, -=, *=, …
- Numeric: +, -, *, /, %, ++, --, …
- Relational: ==. !=, <, >, <=, >=, …
- Boolean: &&, ||,!
- Bitwise: &, |, ^, ~, <<, >>, …
- Control Structures more of what you expect:
1. conditional: if, if else, switch
2. loop: while, for, do
3.break and continue
Classes, References, & Packages
- Classes and Objects
- “All Java statements appear within methods, and all methods are defined within classes”.
- Java classes are very similar to C++ classes (same concepts).
- Instead of a “standard library”, Java provides a lot of Class implementations or packages
- What are packages?
- You can organize a bunch of classes and interfaces into a package (or library of classes) - defines a namespace that contains all the classes.
- Use the import keyword to include the packages you need
- You need to use some java packages in your programs
- java.awt (Abstract Windowing Toolkit), java.io (for Files, etc.), java.util (for Vectors, etc.)
- References
- No pointers everything’s a reference!
- classes
- arrays
Exceptions
- When a program carries out an illegal action, an
exception is generated.
- Terminology:
- throw an exception : signal (in the method header)
that some condition or error has occurred but we
want to pass the buck and not deal with it.
- catch an exception : deal with the error (or whatever)
ourselves inside the function/method.
- Catch it using a try/catch block (next slide).
- In Java, exception handling is necessary (forced
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
- One top level public class per .java file.
- Typically end up with many .java files for a single
program with at least one containing a static
public main() method (if they’re applications).
- Class name must match the file name!
- The compiler/interpreter use class names to figure out
what the file name is.
- Classes have these three features:
- A constructor that’s used to allocate memory for the
object, initiailize its elements, and return a reference to
the object
- Methods (function members) Docsity.com
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;
}