theme: Plain Jane autoscale: true
public final void sayHello (String message) throws RuntimeException
access | final | return | method name | parameter list | throws Exception list
modifier | (optional) | type name |- return type: check type
- method name: valid identifier
Void m1(String s) {
return null;
}
void m1(String s) {
// no return
}
String m1() {
return "hello";
}
- vargars must be lastargument
- there can be only one
- Exercise: create a method
calcMedianthat returns the median value of nintnumbers. At least you must pass one value - Exercise: create a method
hodorthat given some Strings returns a single string with s1 + " Hodor! " + s2 + " Hodor! "
- 3 keywords, 4 levels: private, protected, (package), public
public void public m1() {}
public void m1 {
}
package void m1(String s) {
}
protected int m1(String s) {
}
protected void final m1(String s) {}
protected void m1(String s);
- static: class method
- abstract: method has no body, child class should override it
- final: can't override this method
- synchronized: thread safe
- strictfp (not in OCA/OCP): floating point calculations IEEE 754 1
- native (not in OCA/OCP): to interact with native libraries (C, C++)
- variables
- methods
- static imports
- passing by value vs. passing by ref
- in Java there's only pass by value
- we pass a copy of the reference
- simple types
- object types
- use IntelliJ to learn how to write them :-D
- A Lambda is just a piece of code we can pass around. Think of it as a function pointer.
- Lambda that takes a string as param and doesn't return anything
(String s) -> { ...statements; };Runnable task1 = new Runnable() {
@Override
public void run() {
System.out.println("Something!");
}
};
task1.run();Runnable task2 = () -> { System.out.println("Yeah!"); };
task2.run();Runnable task2 = () -> System.out.println("Yeah!");
task2.run();- a functional interface has exactly one abstract method
- default methods in an interface have an implementation, they are not abstract
public class Main {
public static void main(String[] args) {
Downloadable d;
d = uri -> { return uri + " Downloaded"; };
}
}
interface Downloadable {
String download(String uri);
}
interface Eater {
public String eat(int i);
}
Eater e = (int i) -> { return "Eaten i==" + i; };
System.out.println(e.eat(11));
- focus on what, not in who
- "I want to convert all elements in this array to String", instead of "loop through..."
- Lambda expression
- block of code
- you can store it in a variable
- you can pass it around
- methods without a name
Stream<Person> f = people.stream().filter((Person p) -> {return p.isHappy() == true;});
f.forEach(p -> System.out.println(p.getName()));- Not in OCA, but useful
List<String> names = Arrays.asList("Grouch", "Chicc", "Harp");
// forEach to print
names.stream().forEach(e -> System.out.println(e ));
names.stream().map((e) -> e + "o").forEach(e -> System.out.printf(e + "\n"));
names.stream().map((e) -> e.toUpperCase()).forEach(e -> System.out.printf(e + "\n"));- functional interface
Predicate - has a
test method
boolean test(T t);Predicate<String> p = s -> s.startsWith("G");
System.out.println(p.test("Manolo")); // --> false
System.out.println(p.test("Groucho")); // --> truePredicate<String> startsWithG = s -> s.startsWith("G");
Predicate<String> correctSize = s -> s.length() == 7;
Predicate<String> correctName = startsWithG.and(correctSize);
System.out.println(correctName.test("Manolo"));
System.out.println(correctName.test("Groucho")); // --> true
List<String> names = Arrays.asList("Grouch", "Chicc", "Harp");
Predicate<String> p = s -> s.startsWith("G");
names.stream().filter(p).forEach(System.out::println);Optional<String> s = names.stream().reduce((s1, s2) -> { return s1+s2; });
if (s.isPresent()) {
System.out.println(s.get());
}Interface Function<T,R>
- T: the type of the input to the function
- R: the type of the result of the function// m1() takes a String and returns a String, hence the <String, String>
static Function<String, String> m1() {
return new Function<String, String>() {
@Override
public String apply(String s) {
return s.concat(" and two boiled eggs!");
}
};
}static Function<String, String> m2() {
return new Function<String, String>() {
@Override
public String apply(String s) {
return s.concat(" yeah!");
}
};
}
// ....
Function<String, String> f;
f = m1();
System.out.println(f.apply("Hola"));
f = m2();
System.out.println(f.apply("Hola"));
Footnotes
-
Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.
http://docs.oracle.com/javase/specs/ ↩