Java Grammar
This post talks about the grammar of Java.
Data Types
Category of Type
- Basic type: byte, short, int, long, boolean, float, double, char
- cannot be set to null
- use
==
to check equals
- Reference type: class, (interface is also class)
- can be set to null
- use
equals
to check equals
Common Classes
- Wrapper class
- Boolean, Byte, Short, Integer, Long, Float, Double, Character
- Wrap the basic type to reference type, could be set to null
1
2
3int i = 3;
Integer n = new Integer(i);
int x = n.intValue();
- String
- is a class
- the content cannot be changed, every modification returns new string object
- “”.match could use regex
- JavaBean
- classes that obey the rule to use properties (getter, setter) to get access to private fields
- field: private Type xyz
- getter: public Type getXyz()
- setter: public void setXyz()
- classes that obey the rule to use properties (getter, setter) to get access to private fields
- Optional
- Optional.isPresent() -> boolean
- Optional.get() -> T
- Regex
1
2
3
4
5
6
7
8import java.util.regex.*;
Pattern pattern = Pattern.compile("xxxx");
Matcher matcher = pattern.matcher("input string");
matcher.matches(); // true of false
matcher.group(0); // whole expression
matcher.group(1); // first group substring
matcher.find(); // find the next match
Anonymous Class
1 | Runnable r = new Runnable() { |
Lambda Expression
Lambda Expression is a coding style that supports Functional Programming, which treats functions as the units to operate.
It could replace the anonymous class which is for the FunctionalInterface
(interface owning 1 single funciton).
1 | // Anonymous Class |
Reflection (Class
Instance)
Reflection could get the information of an object in runtime even know nothing about the specific types of the object.
- When JVM loads a type of class, JVM creates one
Class
instance, which contains all the information of the type of class. e.g. class name, package name, base class, implemented interfaces, methods, fields - In the runtime, if we can get the
Class
instance, we could get the information of the binded class. This process is called reflection
Note. the Class
is a class with name of Class
Get Class
Instance
To get a Class
instance, we could uses the following methods
1 | // By static field of a class |
Usages of Class
Instance (Reflection)
1 | Class cls = T.class; |
Annotation
Annotation is a special comment or mark for codes. It will be ignored by compiler but could be put into the .class file. It could do the following things.
- (SOURCE) Let compiler check the code (not put into .class file)
- e.g. @Override checks whether the father class has the same method
- Help to generate document
- (CLASS) Let tools to modify classes dynamically during class loading
- (RUNTIME) Used in JVM runtime
- for reflection (Require @Target(RUNTIME))
Common Used Annotation
Meta annotation: use to annotate other annotation
@Documented
content appears in javadoc@Inherited
indicates that annotation could pass to the subclass of baseclass who is annotated by the annotation@Target
set the ElementType of an annotation (where to use)- values: TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE
- Indicate what type(s) of content this annotion describes
- Default: all types
@Rentention
set the RententionPolicy of an annotation- values: SOURCE, CLASS, RUNTIME
- Indicate which stage the compiler should keep this annotation
- Default:
RentionPolicy.CLASS
@Report
indicates that annotation can be used several time in a place
Use to annotate codes
@Deprecated
compiler throws warning if this function is used@Override
compiler throws error if base class doesn’t have the same function@SuppressWarnings
compiler keeps silent for specific warnings@SafeVarargs
Suppress warnings for all callers of a method or constructor@FunctionalInterface
Specifies that type declaration is intended to be a functional interface@Repeatable
Specifies that the annotation can be applied more than once to the same declaration@Test
JUnit run this function as a test case
Custom Annotation Definition
- Type: declared by
@interface
(java.lang.annotation.Annotation) - Annotation Body: set parameters (method without arguments) and default values
- Use other meta annotation to configure
@Target
@Retention
1 | // Means could annotate class, interface |
Usages
Annotation is only a comment of codes, it should be used by other tools. e.g. SOURCE annotation is used by compiler, RUNTIME annotation is used by code.
In order to use the RUNTIME annotation, reflection is used to get the annotation.
1 | // A: annotation type |
Generics
Java uses Type Erasure
to implement the generics, which means the compiler takes <T>
as object, and do the cast type convertion for <T>
- The template
T
cannot be the basic type - Cannot get the
Class
instance of a generic e.g.Pair<String>.class
- Cannot initialize a
T
e.g.new T()
Subclass<T>
can converts toBaseclass<T>
Type<Subclass>
cannot converts toType<Baseclass>
1 | public class Pair<T> { |
?
, extends
and super
For the arguments of a function, there could be 4 types of generics indicators
<T>
: receive the T type<?>
: receive any types, but the arguments cannot be read or write, can only used for null check- can use as a variable
- baseclass of any
<T>
<? extends T>
: arguments could be T or subclass of T, read-only- could use
T get()
to get reference of type T - couldn’t use
set(T)
to pass reference of type T, exceptnull
- could use
<? super T>
: arguments could be T or baseclass of T, write-only- couldn’t use
T get()
to get reference of type T, exceptObject
- could use
set(T)
to pass reference of type T
- couldn’t use
PECS principle (Producer Extends Consumer Super)
- Producer: return
T
, use<? extends T>
- Consumer: receive
T
, use<? super T>
1 | static <T> boolean isNull(Pair<T> p) { |
Collections
Abstract programming means we need to own the interface, but assign a specific class to it. e.g.
1 | Queue<Integer> queue = new LinkedList<>(); // LinkedList<Integer> |
- Collection (interface)
- List, AbstractList (interface)
- ArrayList: implement with array
- LinkedList: implement with linkedlist
Vector
- Set, SortedSet (interface)
- HashSet: no ordered
- LinkedHashSet
- TreeSet: sorted key
- Queue (interface)
- LinkedList
- PriorityQueue: sorted the element
- Dequeue: double ended queue
- List, AbstractList (interface)
- Map (interface)
- AsbtractMap (interface)
- HashMap
.keySet()
.entrySet()
,.getKey()
,.getValue()
- LinkedHashMap
- EnumMap: stores enum type key
HashTable
- HashMap
- SortedMap (interface)
- TreeMap: sorted the key with
Comparable
interface of key
- TreeMap: sorted the key with
- AsbtractMap (interface)
Create and initilization for array and collections
1 | // Array |
Collections support generics, to use custom class in collections, following requirements should be met.
- List
- implements
equals()
- implements
- Map
- implements
equals()
for key - implements
hasCode()
- implements
IO
- Byte Stream: byte by byte
- InputStream (Abstract)
- OutputStream (Abstract)
- Character Stream: character by character
- Reader (Abstract)
- Writer (Abstract)
Stream
Stream is a data processing pipeline for collection elements. It uses a SQL-like grammar and lambda expression to make the processing code look tidy.
To use stream, first the elements needs to be transferred as stream elements, then be operated under intermediate operations, then be output by terminal operation.
Stream won’t store any middle data and won’t change the input data. It is lazy operation which means the stream will be triggered when the output data is used.
Create Stream
- (Collections)object[].stream
- Collections.stream(object[])
- Stream.of(Object[])
Intermediate Operations
- distinct
- filter
- map
- flatmap: map + flatten, put the mapped elements into new stream
- limit: get specific numbers of elements
- peek: exectue another function for the elements of stream, but elements keep still
- sorted
- skip: drop the first n elements
Terminal Operations
- Match
- allMatch
- anyMatch
- noneMatch
- count
- max, min
- collect
- find
- findAny
- findFirst
- forEach: execute another function for the elements of stream (like peek), but not guranteed the order of elements
- forEachOrdered
- reduce: reduce (sum, multiply, etc) all elements ant put in a new stream
- toArray
Exception
Exception Classes
- Throwable
- Error
- OutOfMemoryError
- NoClassDefFoundError
- StackOverflowError
- Exception
- “Checked Exception”, need to be catch in code
- IOException
- UnsupportedCharsetException
- FileNotFoundException
- SocketException
- ParseException
- GeneralSecurityException
- SQLException
- TimeoutException
- “CustomException”
- IOException
- “Unchecked Exception”, RuntimeException, shouldn’t catch, fix code instead
- NullPointerException
- IndexOutOfBoundsException
- SecurityException
- IllegalArgumentException
- NumberFormatException
- “Checked Exception”, need to be catch in code
- Error
Try Catch Snippet
1 | try { |
Useful Function of Exception
1 | // Different constructor |
Assert
assert
could use to determine whether a condition could meet. If it is not meet, terminate the program. Therefore, the assert
could only be used in test phrase rather than real production.
assert
needs to be enabled by passing a -enableassertions
or -ea
to JVM
Logging
Logging could log useful info when running code to a file or command line. It could replace using System.out.printline
to debug.
- Logging Implementation
- JDK Logging: java.util.logging.Logger
- Log4j: org.apache.logging.log4j.Logger
- Logback
- Logging Interface: could combine with one of the logging implementation
- Commons Logging: org.apache.commons.logging.LogFactory
- SLF4J
Log4j 2
- JAR files: log4j-api-2.x, log4j-core-2.x
- Packages: org.apache.logging.log4j.LogManager, org.apache.logging.log4j.Logger
- Logging level: OFF < FATAL < ERROR < WARN < INFO < DEBUG < TRACE < ALL
- Log4j2 could set the logging level, only information belonging to that level or the levels below that level could be logged
JVM
Arguments
-cp
,-classpath
: set the class path, don’t set the current class path-ea
,-enableassertions
: enableassert
function-Dproperty=value
: pass arguments
Import Packages
Java uses JVM to load classes from other packages. When running a class which imports other packages, JVM checks the existence of those packages according to classpath
. The classpath
could be passed to by using the argument -cp
when executing JVM.
jar
is a zip file containing a folder of packages, it could also be passed as a classpath. jar
file could have a /META-INF/MANIFEST.MF
file, where other dependencies and the Main-Class
could be set. The jar
file could be executed directly by JVM.
Maven is an good tool to manage the jar
packages.