- String concatenation:
+
- Eg:
System.out.println("Hell" + 0 + " world");
printsHell0 world
- Eg:
- There is no
const
keyword,final
can be used to declare constant 'variables'. - Boolean type is
boolean
in Java, not bool. default
in function signature: To mention that this definition is the default implementation of this function- Can be used inside an
interface
. - https://stackoverflow.com/questions/31578427/what-is-the-purpose-of-the-default-keyword-in-java
- Can be used inside an
volatile
: used to indicate that a variable may be used by mutliple threads ??- Type casting: Syntax is like in C
javap name.class
: tries to convert a class file back to java file ??
API:
- Streams: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html
- Regex patterns: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
- Optional values: https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
Pattern matching (switch-case)
switch(exp) {
case One -> 1;
case Two -> 2;
default -> 3;
}
See:
- https://www.infoq.com/articles/pattern-matching-for-switch/
- https://docs.oracle.com/en/java/javase/18/language/pattern-matching-switch-expressions-and-statements.html
Method references
https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
Example in above link:
Arrays.sort(rosterAsArray,
compareByAge(a, b)
(a, b) -> Person. );
can be achieved with
Arrays.sort(rosterAsArray, Person::compareByAge);
ie, kind of like the usual way of using functions in Haskell.
Interface
- Like module type in ocaml (or signature in sml)
default
methods: 'A default method is a public non-abstract instance method, that is, a non-static method with a body, declared in an interface type.'ʳ
Functional interface
@FunctionalInterface
- An interface with exactly one abstract method
- Useful for functional programming style. How??
The value would take the role of the function that was left abstract.
Can think of functions more like values ??
- Eg:
String concat(String s)
vsFunction<String, String> concat
See:
- https://herovired.com/learning-hub/blogs/functional-interface-in-java/
- https://www.freecodecamp.org/news/functional-programming-in-java/
- https://www.scaler.com/topics/functional-interface-in-java/
$
in identifiers
$
is allowed in identifiers, but it is meant to be used selectively by convention.
TL;DR: Better not use it.
From oracle website:
The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
See:
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8
- https://stackoverflow.com/questions/14056918/when-should-i-use-the-dollar-symbol-in-a-variable-name
Generics
?
: Used as an unknown/any type in generic functions<? super R>
: A class which is a super class of R- This class would have lesser or equal amount of features as R
R <: ?
- https://docs.oracle.com/javase/tutorial/java/generics/lowerBounded.html
<? extends T>
: A class which is derived from class T- This class would have more or equal amount of features as T
? <: T
- https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java
- https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html
Function interfaces
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html
Interface | Type |
---|---|
Function<A ,B> |
A -> B |
BiFunction<A, B, C> |
(A, B) -> C |
BinaryOperator<T> |
(T, T) -> T |
UnaryOperator<T> |
T -> T |
apply
: apply arg function to given argsidentity
: make identity function
Composition
andThen
: goes from left to rightcompose
: goes from right to left
These two are apparently the same, but with argument order reversed.
- https://stackoverflow.com/questions/43849066/java-8-functions-compose-and-andthen
a.andThen(b)
is same asb.compose(a)
I guess g.compose(f)
is there to be more in line of what we mean when we say g ∘ f
in theory. ie, first do f
and then do g
.
While writing in Java, often the andThen
version is more convenient.
Record class
See:
Sealed classes and interfaces
Enforces a form of subtyping ??
See:
Wrapper classes for primitive types
There are 8 primitive data types in Java. Some of them:
Primitive | Wrapper |
---|---|
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
string |
String |
https://en.wikipedia.org/wiki/Primitive_wrapper_class_in_Java
'double cannot be converted to float'
- Float numbers are by default
double
, notfloat
- Eg: 3.14 is
Double
Double dD = 3.14;
floatValue(); // is type Float
dD.
double dd = 3.14;:w
float) dd; // is type Float ?? (
https://stackoverflow.com/questions/32837783/convert-double-to-float-in-java
A sub-set of built-in exceptions
- NoSuchElementException
https://programming.guide/java/list-of-java-exceptions.html
Find size of an object
- Use jol: A tool associated with openJDK
- Could also use
java.lang.instrument
package.
Packaging
Remarks
- Every (non-primitive) type in java is inhabited by
null
.- This means that
void
is actually likeunit
.
- This means that
- Primitive types are unboxed. Can't be
null
. - Java has no union types like in C. Java doesn't offer that level of fine grained control over memory allocation, apparently.
- There is no way to have type aliases. But can have derived class as a work-around.
Doubts:
- Can we make a general way to have type constructors?
- Reflection in java (often not recommended??)
Misc
- CFR: A decompiler for JVM
- Class files can be converted to Java
- Even for other languages that target JVM. Like Scala, Frege.
- Frege: A haskell-like language for JVM (development has stalled)
- There was a bug in the binary search implementation of java.util.Arrays
- Because of overflow in
int mid = (low + high)/2
. - Change it to
int mid = low + (high - low)/2
to avoid the overflow. - https://research.google/blog/extra-extra-read-all-about-it-nearly-all-binary-searches-and-mergesorts-are-broken/
- https://thebittheories.com/the-curious-case-of-binary-search-the-famous-bug-that-remained-undetected-for-20-years-973e89fc212
- Because of overflow in