Java Overall Flashcards
Java 4 Different Package Levels
Public, Protected, Default, Private
Private: to self
Public: to all
Default (Package Internal): same package //package/JDK/3rd party internal
Protected (Package + subclass Internal): package internal, but open to whoever extends my class
Client | | (closed library, no subclass allowed) Library (public) Library (default) Library (private) | | (Open library, subclass allowed) JDK (public, protected) JDK (default) JDK (private)
2 different Class Relationship: Package vs Inheritance
how they map to 4 access modifiers
*There is no parent accessing child class.
Then for classes 1) same package, yes inheritance //internal framework inheritance class 2) same package, no inheritance //internal framework inheritance class 3) diff package, yes inheritance //when you extends a JDK class, or a 3rd party library class 4) diff package, no inheritance //your class vs JDK
What does “transient” keyword do?
In a serializable object, “transient” keyword tells JVM not to serialize the value of the property (JVM will actually serialize with the default value). This access modifier can be used to “do not serialize secretes values to file”
note using with “static” (never serialized because it is not part of object) or “final” (always serialized) void the “transient”
What does “volatile” keyword do?
Volatile in java is different from “volatile” qualifier in C/C++. For Java, “volatile” tells the compiler that the value of a variable must never be cached as its value may change outside of the scope of the program itself. In C/C++, “volatile” is needed when developing embedded systems or device drivers, where you need to read or write a memory-mapped hardware device. The contents of a particular device register could change at any time, so you need the “volatile” keyword to ensure that such accesses aren’t optimized away by the compiler.
“abstract” method
A method declared with “abstract” means it will not have an implementation, forcing the class to be declared “abstract” so it cannot be instantiated.
interface method
A method declared inside “interface” has no implementation and thus is an “abstract” method.
All methods in regular interfaces are public and abstract.
functional interface
Started in 1.8
A functional interface
1) has one and only 1 method declaration.
2) [may] Also have “default” implementation or “static” implementation.
java.util.function.Function interface vs functional interface
“functional interface” is a specification
java.util.function.Function is an instance that satisfies “functional interface” specification, thus
java.util.function.Function is a Functional interface
It has one and only one declared method
public interface Function { public apply(T parameter); }
java.util.function.Function interface vs functional interface
“functional interface” is a specification
java.util.function.Function is an instance that satisfies “functional interface” specification, thus
java.util.function.Function is a Functional interface
It has one and only one declared method
public interface Function { public apply(T parameter); }
Consumer is a functional interface
It has one and only one declared method
public interface Function { public accept(T t); //consumes value t }
What is “default” keyword?
It means default “implementation” of a functional interface.
It does not mean “default access”, as default access is implied by lack of access modifier, not by the “default” keyword
java.util.function.Function interface, “apply()” method
java.util.function.Function is a functional interface with one and only one declared method
~~~
public interface Function {
public apply(T parameter); }
```
The “apply” is similar to javascript
objFUNC implements Java Function::apply() vs
C++ std::bind(objFUNC) vs
JS objFUNC.apply() or call()
They are all ways to invoke a function
JS
js_func.apply(obj, arg1, arg2…);
==obj.func(arg1, arg2)
‘obj’ is the ‘this’ pointer in the function, thus similar calling
C++
funcObj = std::bind(func, arg1, arg2…);
==funcObj(); //create a closure that includes func, and arg
==func(arg1, arg2)
java
A funcObj implements the Function::apply(arg1, arg2) functional interface.
Then funcObj() will invoke the function.
In this sense, java apply() is similar to C++ std::bind()
What is Consumer functional interface
Consumer interface declares one and only one method void accept(T t); like other functional interfaces.
This is a functional interface that represents a sink function that consumes a value without returning any value. A Java Consumer implementation could be printing out a value, or writing it to a file, or over the network etc.
It has “default” implementation “addThen()”
default Consumer andThen(Consumer super T> after)
which will chain consumers.
c0.addThen(c1).addThen(c2).addThen(c3).accept(); will invoke in sequence: /*first*/ c0.accept(v); /*then*/ c1.accept(v); /*then*/ c2.accept(v); /*then*/ c3.accept(v)
functional interface and lambda
Functional interface declares one and only one method. (with “default” or “static” helper methods).
Lamba is a function object that implements one and only one method.
Functional Interfaces Can Be Implemented by a Lambda Expression
Lamda to Functional Interface
is
Anonymous class to Java Interface
//This is a both functional and a regular interface interface HelloWorld { void foobar(); } HelloWorld helloworld = new HelloWorld() { @override void foobar() { System.out.println("Impl..."); } }
Helloworld helloworld = () -> {
System.out.println(“Impl…”);
}
Lamda to Functional Interface
is
Anonymous class to Java Interface
//This is a both functional and a regular interface interface HelloWorld { void foobar(); } HelloWorld helloworld = new HelloWorld() { @override void foobar(T parameter1, T parameter2) { System.out.println("Impl..."); } }
Helloworld helloworld = () -> {
System.out.println(“Impl…”);
}
lambda is syntax sugar of an object instance (of an anonymous class that implements a functioanl interface).
Since a functional interface has 1 and only 1 method, lamba can invoke the method without saying the method name.
Lamba is an “function object” instance that implement the corresponding “functional interface” with the “this” pointer bound in.
Helloworld helloworld=
(parameter1, parameter2) -> expression
Helloworld helloworld=
(parameter1, parameter2) -> {code block}
So invoked as helloworld(arg1, arg2),
not
helloworld.foobar(arg1, arg2);
lambda is an object instance (of an anonymous class that implements a functioanl interface) lambda can be passed/returned as any object lambda is an object instance of the "functional interface"
Constant Interface Antipattern
Do not put static constant in a interface
interface helloword {
static final int K = 10;
}
class A implements helloworld { //use K }
Constant Interface Antipattern
Do not put static final in a interface
interface IF {
static final int K = 10; //anti-pattern
}
class A { static final int K=10; //good pattern }
import static A.K;
Java outer class has full access to inner class members including private ones, and vice versa
WARNING!
https://stackoverflow.com/questions/1801718/why-can-outer-java-classes-access-inner-class-private-members
The “private” modifier on inner class and its members are “useless”, and it does not prevent application to access those members from an outer class object.
[*] Java Static Nested class is mostly a namespace purpose.
For nested static class, the enclosing class always has access to it (regardless if it is private)
“static Nested class” is can be considered as “implementation” class, that all its members including private members are accessible by the outer class.
[*] Non-static inner class if part of the outer class in storage. (It is like nested struct in C)
when you malloc() or new() a outer class, the storage space of inner class is also allocated
Objects of Non-static inner class must contain the “this” pointer to the outer class object (the two are “life-time bundled” instances)
(Objects of static nested class does not contain the “this” pointer of any outer class object— the two are life-time independent instances)
Outer object contains explicit reference to inner objects,
Inner objects contains the implicit “this” reference to the outer objects (initialized by JVM) (The explicit reference to outer object would be Outter.this)
For class Outter { int a; class Inner ( int a; //local a a = 10; //outer a Outter.this.a = 20; } }
Since the inner object must contain the “this” pointer of the outer object, the outer object must be created first.
Inner class object is created FROM outer object.
(
either
new Inner() inside outer class, or
new Outer().new Inner() from external class)
- can access all members (including private) of the outer object via its copy of the outer object’s “this” pointer
- Inner at class level and inner at local/function level
non-static inner class != nested struct in c
In C, a nested struct is created when outer struct is malloc() or new()’d. They co-exist and are co-located
In Java, inner object may or may not be created by the outer class. The outer object out-live the inner object if any. The inner object has an implicit copy of the outer object’s “this” pointer. They do not co-locate
[*] Outer object is fully visibie inside inner object via the inner object’s copy of the outer “this” pointer.
So watch out for variable hiding/shadowing
-
Lambda is syntax sugar for “new() Anonymous Class” (instance) for Functional interface which is special case of interface (one and only one method)
When not to use lambda?
lambdas lack names and documentation; if a computation isn’t self-explanatory, or exceeds a few
lines, don’t put it in a lambda.
Lambda is syntax sugar for “new() Anonymous Class” (instance) for Functional interface which is special case of interface (one and only one method)
When not to use lambda?
When to use Anonymous class (still useful in lambda era)
lambdas lack names and documentation; if a computation isn’t self-explanatory, or exceeds a few
lines, don’t put it in a lambda.
Lambdas are limited to functional interfaces (1 abstract method)
anonymous classes covers multiple abstract methods.
a lambda cannot obtain a reference to itself. In a lambda,
the “this” keyword refers to the enclosing instance,
In an anonymous class, the this keyword refers to the anonymous class instance.