Javacrash
Javacrash
• hello world
• basic data types
• classes & objects
• program structure
• constructors
• garbage collection
• I/O
• exceptions
• Strings
Hello world
import java.io.*;
1
Basic data types
public class fahr {
public static void main(String[] args){
for (int fahr = 0; fahr < 300; fahr += 20)
System.out.println(fahr + " " +
5.0 * (fahr - 32) / 9.0);
}
}
• basic types:
– boolean true / false
– byte 8 bit signed
– char 16 bit unsigned (Unicode character)
– int 32 bit signed
– short, long, float, double
• String is sort of built in
– "..." is a String
– holds chars, NOT bytes
– does NOT have a null terminator
– + is string concatenation operator
2 versions of echo
public class echo {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++)
if (i < args.length-1)
System.out.print(args[i] + " ");
else
System.out.println(args[i]);
}
}
2
Classes, objects and all that
class thing {
public part:
methods: functions that define what operations
can be done on this kind of object
private part:
functions and variables that implement the
operation
}
public class RE {
String re; // regular expression
int start, end; // of last match
3
Constructors: making a new object
public RE(String re) {
this.re = re;
RE r;
r = new RE(s);
public class RE {
static int num_REs = 0;
• class methods
– most methods associated with an object instance
– if declared static, associated with class itself
– e.g., main()
4
Program structure
• typical structure is
class RE {
private variables
public RE methods, including constructor(s)
private functions
5
I/O and file system access
• import java.io.*
• byte I/O
– InputStream and OutputStream
• file access
• buffering
• exceptions
Character I/O
• use Buffered(Reader|Writer)
– for speed
– because it has a readLine method
6
Line at a time I/O
Exceptions
7
try {…} catch {…}
Why exceptions?
• reduced complexity
– if a method returns normally, it worked
– each statement in a try block knows that the previous
statements worked, without explicit tests
– if the try exits normally, all the code in it worked
– error code grouped in a single place
8
String methods
• String parsing
import java.util.regex.*;
public class RE {
Pattern p;
Matcher m;
9
Java vs. C and C++
• no preprocessor
– import instead of #include
– constants use static final declaration
• C-like basic types, operators, expressions
– sizes, order of evaluation are specified
byte, short, int, long: signed integers (no unsigned)
char: unsigned 16-bit Unicode character
boolean: true or false
• really object-oriented
– everything is part of some class
– objects all derived from Object class
– static member function applies to whole class
• references instead of pointers for objects
– null references, garbage collection, no destructors
– == is object identity, not content identity
• all arrays are dynamically allocated
– int[] a; a = new int[100];
• strings are more or less built in
• C-like control flow, but
– labeled break and continue instead of goto
– exceptions: try {…} catch(Exception) {…}
• threads for parallelism within a single process
– in language, not a library add-on
10