Quick Introduction To Java
Quick Introduction To Java
(ISI) Java I 1 / 33
Outline
1 Program structure
3 Language basics
4 Useful classes
5 Exceptions
(ISI) Java I 2 / 33
Program structure
public char c, d;
}Variables
public Hello(char c, char d) { Constructor
this.c = c; Order
this.d = d; does
}
not
public void someFunction() { Methods matter.
/* some code here */ (functions)
return;
}
(ISI) Java I 4 / 33
Outline
1 Program structure
3 Language basics
4 Useful classes
5 Exceptions
(ISI) Java I 5 / 33
Constructors
(ISI) Java I 6 / 33
Inheritance
Subclass Superclass
import java.io.* ;
(ISI) Java I 8 / 33
Access modifiers II
static
associated with class, not with any specific instance / object
only one instance of the variable / function across all instances of the
class (≈ “global”)
exists even if class is never instantiated
static methods cannot access any variables
example uses:
(ISI) Java I 9 / 33
Access modifiers III
abstract:
declarations without any associated implementation
method must be implemented in subclasses
class with abstract method(s) → abstract class
abstract class cannot be instantiated with new
(ISI) Java I 10 / 33
Outline
1 Program structure
3 Language basics
4 Useful classes
5 Exceptions
(ISI) Java I 11 / 33
Primitive / basic data types
Casting
Syntax as for C, except underscores permitted and ignored
Automatic casting only when conversion is guaranteed to be lossless
(ISI) Java I 12 / 33
Conventions for variable, function, class names
(ISI) Java I 13 / 33
Wrapper classes for primitive types
Examples
Character.isDigit(’3’); // true
Character.isUpperCase(’a’) // false
Character.toUpperCase(’a’) // ’A’
Integer i = 3;
i = new Integer(3);
int j = i.intValue();
j = Integer.MAX_VALUE;
j = Integer.parseInt("123"); // like atoi()
(ISI) Java I 14 / 33
Expressions, statements, method calls
(ISI) Java I 15 / 33
Input/output
System.out.print(); System.out.println();
(ISI) Java I 16 / 33
Outline
1 Program structure
3 Language basics
4 Useful classes
5 Exceptions
(ISI) Java I 17 / 33
Strings I
String concatenation
Concatenation operator: +
Possible to concatenate anything with a string
Objects should provide toString() method(s)
(library classes all have reasonable toString() methods)
(ISI) Java I 18 / 33
Strings II
// substring(startIndex, endIndex+1)
int i;
for (i = 0; i < str.length(); i++)
if (str.charAt(i) == str.substring(i, i+1).charAt(0))
System.out.print(".");
System.out.println(" sanity check.");
// split(regularExpression)
String[] words = str.split("\\s+");
// Tokenisation
StringTokenizer tok = new StringTokenizer(str);
while (tok.hasMoreTokens()) {
String s = tok.nextToken();
System.out.println(s);
}
(ISI) Java I 19 / 33
Strings II
// substring(startIndex, endIndex+1)
int i;
for (i = 0; i < str.length(); i++)
if (str.charAt(i) == str.substring(i, i+1).charAt(0))
System.out.print(".");
System.out.println(" sanity check.");
// split(regularExpression)
String[] words = str.split("\\s+");
// Tokenisation
StringTokenizer tok = new StringTokenizer(str);
while (tok.hasMoreTokens()) { StringTokenizer() takes as
String s = tok.nextToken();
System.out.println(s); optional second argument a string
} containing separator characters
(defaults to whitespace).
(ISI) Java I 20 / 33
Strings III
s.startsWith("a"); // true
t.endsWith("f"); // false
s.indexOf(’d’); // 3
s.indexOf("def"); // 3
t.lastIndexOf(’x’); // 3
t.toUpperCase(); // "DEFXYZ"
u = t.replace("xyz", "def"); // "defdef"
v = "ABCDEF".toLowerCase(); // "abcdef"
s == v; // false
s.compareTo("abc"); // > 0 (like strcmp())
s.compareTo(v); // == 0 (like strcmp())
s.equals(v); // true
(ISI) Java I 21 / 33
Arrays
{
Arrays.sort(a); a.toString(a) does not work
System.out.println(Arrays.toString(a)); a.toString() does not work as
expected
(ISI) Java I 22 / 33
Vectors
import java.util.Vector;
(ISI) Java I 23 / 33
Iterators
(ISI) Java I 24 / 33
HashMaps
table.containsKey("seven"); // true
table.containsKey("twelve"); // false
(ISI) Java I 25 / 33
Sets
(ISI) Java I 26 / 33
Outline
1 Program structure
3 Language basics
4 Useful classes
5 Exceptions
(ISI) Java I 27 / 33
Exceptions
(ISI) Java I 28 / 33
Outline
1 Program structure
3 Language basics
4 Useful classes
5 Exceptions
(ISI) Java I 29 / 33
Misc. issues
(ISI) Java I 30 / 33
Cloning
Coming soon
(ISI) Java I 31 / 33
Sources / references I
(ISI) Java I 32 / 33
Sources / references II
Cheatsheets
http://introcs.cs.princeton.edu/java/11cheatsheet/ ∗ ∗ ∗
http://github.com/amitguptagwl/Java-Rule-Book
http://github.com/cirosantilli/java-cheat
http://github.com/in28minutes/java-cheat-sheet
http://hackr.io/blog/java-cheat-sheet
http:
//www.edureka.co/blog/cheatsheets/java-cheat-sheet/
http://www.mindprod.com/jgloss/jcheat.html
http://www.paradise.caltech.edu/cook/Workshop/Java/
Overview.html
http://www.rankred.com/java-cheat-sheets/
(ISI) Java I 33 / 33
Sources / references III
Books
1. Core Java for the Impatient, Cay S. Horstmann.
Addison-Wesley / Pearson Education Inc.
2. Effective Java (3rd ed.), Joshua Bloch. ∗ ∗ ∗
Addison-Wesley / Pearson Education Inc.
Misc. resources
https://www.mindprod.com/jgloss/pointer.html
https://www.mindprod.com/jgloss/gotchas.html
∗ ∗ ∗ – recommended
(ISI) Java I 34 / 33