Overloaded Methods Sending Messages Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Initialization & Cleanup 2 Overloaded Constructors Sending Parameters accessor method 3 4
Sending Parameters Some words about this attribute value!= parameter value 5 6 Some words about this Some methods from Class Object Method Description (from docs API ) Object clone() creates and returns a copy of this object boolean equals(object o) void finalize() int hashcode() indicates whether some other object is "equal to" this one called by the garbage collector on an object when garbage collection determines that there are no more references to the object returns a hash code value for the object String tostring() returns a string representation of the object 7 8
public boolean equals(object o) public boolean equals(object o) 9 10 public String tostring() protected void finalize() V 11 12
Class String Class String? String s = new String( My first string ); String s = My first string ; = String a = First string ; String b = a.concat( Second string ); String a = First string ; String b = a + Second string ; 13 14 Some constructors from Class String Some methods from Class String Constructor Description (from docs API ) Method Description (from docs API ) boolean equals(object o) compares this string to the specified object String() initializes a newly created String object so that it represents an empty character sequence int length() returns the length of this string String(String original) Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string char charat(int index) int indexof(int ch) returns the char value at the specified index returns the index within this string of the first occurrence of the specified character int indexof(string str) returns the index within this string of the first occurrence of the specified substring 15 16
Some (more) methods from Class String Some static methods from Class String Method Description (from docs API ) Method Description (from docs API ) String concat(string str) boolean endswith(string s) boolean startswith(string s) concatenates the specified string to the end of this string tests if this string ends with the specified suffix tests if this string starts with the specified prefix String valueof(boolean b) returns the string representation of the boolean argument String valueof(char c) returns the string representation of the char argument String valueof(int d) returns the string representation of the int argument String valueof(float f) returns the string representation of the float argument String substring(int beginindex) returns a new string that is a substring of this string String valueof(double l) returns the string representation of the double argument boolean isempty() returns true if, and only if, length() is 0 String valueof(object i) returns the string representation of the Object argument 17 18 Arrays Arrays int[] intarray = new int[10]; int intarray[] = new int[10]; String s[] = new String[5]; 19 20
Arrays Arrays 21 22 Arrays Arrays V 23 24
Arrays Arrays 25 26 Wrapper Classes Type Integer Primitive type Wrapper class Constructor byte Byte byte, String short Short short, String int Integer int, String Constructor/Method Description (from docs API ) Integer(int value) Integer(String s) creates an object that represents the specified int value creates an object that represents the int value indicated by the String parameter long Long long, String float Float float, double, String double Double double, String String tostring() int intvalue() returns a String object representing this Integer's value returns the value of this Integer as an int char Character char boolean equals(object o) compares this object to the specified object boolean Boolean boolean, String int compareto(integer i) compares two Integer objects numerically 27 28
Type Integer Static Method Description (from docs API ) Integer valueof(string value) returns an Integer object holding the value of the specified String Integer valueof(int value) returns an Integer instance representing the specified int value int parseint(string s) parses the string argument as a signed decimal integer 29 30 Autoboxing Unboxing 31 32
Java I/O read input characters Java I/O read input bytes from keyboard System.in.read(); from file FileInputStream filestream; filestream = new FileInputStream( myfile.txt ); filestream.read(); import java.io.*; insert try-catch blocks! from keyboard InputStreamReader r; r = new InputStreamReader(System.in); from file InputStreamReader r; r = new InputStreamReader(new FileInputStream( myfile.txt )); r.read(); 33 34 read input strings Java I/O Java I/O from keyboard BufferedReader r; r = new BufferedReader(new InputStreamReader(System.in)); write bytes from file BufferedReader r; r = new BufferedReader(new InputStreamReader(new FileInputStream( myfile.txt ))); FileOutputStream filestream; filestream = new FileOutputStream( myfile.txt ); filestream.write(var); //var is previously defined r.read(); //next character r.readline(); //next line 35 36
Java I/O Java I/O write characters write strings FileOutputStream filestream = new FileOutputStream( myfile.txt ); PrintStream filestream = new PrintStream(new FileOutputStream( myfile.txt )); OutputStreamWriter outcstream = new OutputStreamWriter(fileStream); filestream.print(var); //var is previously defined outcstream.write(var); //var is previously defined filestream.println(var); //var is previously defined 37 38 import java.io.*; 39 40
import java.io.*; Effective Java Companion slides of Joshua Bloch, Effective Java (Second Edition) 41 5 Avoid creating unnecessary objects 5 Avoid creating unnecessary objects V String s = new String( mystring ); String s = mystring ; X Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) V Boolean b = Boolean.valueOf( true ); Boolean b = new Boolean( true ); sum += i; System.out.println(sum); 43 44
5 Avoid creating unnecessary objects 45 46 6 Eliminate obsolete references 47 48
8 Obey the general contract when overriding equals 13 Minimize the accessibility of classes and members An equivalence relation that is reflexive x.equals(x) == true symmetric x.equals(y) == true if and only if y.equals(x) == true transitive x.equals(y) == true, y.equals(z) == true x.equals(z) == true consistent x.equals(y) returns the same value // Potential security hole public static final Thing[] VALUES = {...}; unless x or/and y is/are modified x.equals(null) == false,where x, y, z are non-null reference values 49 50 14 In public classes, use accessor methods, not public fields 15 Minimize mutability Much better: do not expose data but services!? An immutable class = a class whose instances cannot be modified String, boxed primitives,... 51 52
15 Minimize mutability 15 Minimize mutability Don t provide any methods that modify the object s state. Ensure that the class can t be extended. Make all fields final. Make all fields private. Ensure exclusive access to any mutable components. 53 54