Lecture notes |
Pdf slides |
|
(#1 of 6) |
Lecture notes |
Pdf slides |
|
(#2 of 6) |
Lecture notes |
Pdf slides |
|
(#3 of 6) |
Lecture notes |
Pdf slides |
|
(#4 of 6) |
Lecture notes |
Pdf slides |
|
(#5 of 6) |
Lecture notes |
Pdf slides |
|
(#6 of 6) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Text2File
Lecture notes |
Pdf slides |
|
Text2File
errors:
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
interface
syntax
Lecture notes |
Pdf slides |
|
AutoCloseable
promise
Lecture notes |
Pdf slides |
|
abstract
class replacement
Lecture notes |
Pdf slides |
|
interface
vs. abstract
class
Lecture notes |
Pdf slides |
|
interface
MyAutoCloseable
Lecture notes |
Pdf slides |
|
MyAutoCloseable
to flush
Lecture notes |
Pdf slides |
|
MyFlushable
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 3) |
Lecture notes |
Pdf slides |
|
(#2 of 3) |
Lecture notes |
Pdf slides |
|
(#3 of 3) |
Lecture notes |
Pdf slides |
|
String
Lecture notes |
Pdf slides |
|
(#1 of 4) |
String
Lecture notes |
Pdf slides |
|
(#2 of 4) |
String
Lecture notes |
Pdf slides |
|
(#3 of 4) |
String
Lecture notes |
Pdf slides |
|
(#4 of 4) |
Comparable
interface
Lecture notes |
Pdf slides |
|
String
and Comparable
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 12) |
Lecture notes |
Pdf slides |
|
(#2 of 12) |
Lecture notes |
Pdf slides |
|
(#3 of 12) |
Lecture notes |
Pdf slides |
|
(#4 of 12) |
Lecture notes |
Pdf slides |
|
(#5 of 12) |
Lecture notes |
Pdf slides |
|
(#6 of 12) |
Lecture notes |
Pdf slides |
|
(#7 of 12) |
Lecture notes |
Pdf slides |
|
(#8 of 12) |
Lecture notes |
Pdf slides |
|
(#9 of 12) |
Lecture notes |
Pdf slides |
|
(#10 of 12) |
Lecture notes |
Pdf slides |
|
(#11 of 12) |
Lecture notes |
Pdf slides |
|
(#12 of 12) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Comparator
in action
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
interface
definitions and abstract
Classes
Multiple standards involved:
Note: Compatible hardware must obey both standards.
public class Text2File {
private final PrintStream out;
public Text2File(final String fileName)
throws FileNotFoundException {
out = new PrintStream(new File(fileName));}
public void println(final String s) {
out.println(s);}
public void closeFile() {
out.close();}
}
|
File
Some dumb text More dumb text |
Missing output.closeFile()
call.
Some text portion may not be flushed to disk.
Calling output.println(...)
after
output.closeFile()
:
output.closeFile();
output.println("Too late!");
Last call will be silently ignored.
|
Compile time error: Required: java.lang.AutoCloseable Found: de.hdm_stuttgart.mi.sd1.Text2File |
accessModifier interface interfaceName [throwsClause]?{
[field]*
[method]*
}
package java.lang; ❶ public interface AutoCloseable { /** * Closes this resource, * relinquishing any * underlying resources. */ void close() ❷; } |
public class Text2File implements AutoCloseable ❶{ private ❷ PrintStream out; ... public void println(final String s){ out.println(s); } public void close() ❸{ out.close(); ❹ out = null; ❺ } } |
package hdm.project; ❶ abstract public class AutoCloseable ❷{ /** * Closes this resource, * relinquishing any * underlying resources. */ abstract void close();❸ } |
public class Text2File extends AutoCloseable ❶{ private PrintStream out; ... public void println(final String s){ out.println(s); } @Override public void close() ❷{ out.close(); out = null; } } |
Java™ disallows multiple inheritance.
A class may implement an arbitrary number of interfaces:
public class X implements I1, I2, I3 {...}
/**
* Support auto-closing of resources
*/
public interface MyAutoCloseable {
/**
* close resource in question. Example: Terminate
* a database connection or a file stream.
*/
public void close();
}
/**
* Flush pending values.
*/
public interface MyFlushable extends MyAutoCloseable {
/**
* Save pending i.e. buffered values.
*/
public void flush();
}
public class Text2FileFlushable implements MyFlushable {
private PrintStream out;
...
/**
* Flushing pending output to underlying file.
*/
public void flush(){
out.flush();
}
/**
* Closing file thereby flushing buffer. Caution: Further calls
* to {@link #println(String)} will fail!.
*/
public void close() {
out.close();
out = null;
}
}
Default methods.
Base classes.
interface
definitions and abstract
Classes
interface Comparable<T> { ┌────┘ ▼ int compareTo(T o); }
public class String implements Comparable <String ❶>, ... { ... ┌────────────┘ @Override ▼ public int compareTo(final String ❷ other) { ... return ...; } }
Antisymmetric: sgn(x.compareTo(y)) == -sgn(y.compareTo(x))
Transitive: x.compareTo(y)
> 0
and y.compareTo(z) > 0
⇒ x.compareTo(z) > 0
.
x.compareTo(y)==0
⇒ that sgn(x.compareTo(z)) == sgn(y.compareTo(z))
,
for all z.
Recommendation: (x.compareTo(y)==0) ==
(x.equals(y))
Unsorted | Case sensitive | Case insensitive | Descending |
---|---|---|---|
UK quick hello sign ATM |
ATM UK hello quick sign |
ATM hello quick sign UK |
sign quick hello UK ATM |
Solution: Provide your own Comparator
!
import java.util.Comparator; public class SortCaseInsensitive implements Comparator<String> { ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━┛ @Override ▼ ▼ public int compare(final String a, final String b) { return a.toLowerCase().compareTo(b.toLowerCase()); } }