Java - Concepts

Updated: 2019-01-04

Call one constructor from another(multiple constructors exist)

  • use this

char

16-bit unsigned integers, representing Unicode code points in the Basic Multilingual Plane, encoded with UTF-16, and whose default value is the null code point ('\u0000')

0x0000 - 0xFFFF

Why String is Immutable(Final)

  • Security: Java class loading mechanism works on class names passed as parameters, Strings are made immutable to prevent malicious manipulations
  • Performance/Cache-able
  • Thread-safety: can be share between multiple threads

CLASSPATH

  • CLASSPATH cannot be a folder, need to include each jar /path/to/*.jar
  • use the first match

Java Memory Model

Java Memory Model

The Java memory model describes how threads in the Java programming language interact through memory. The Java memory model defines when changes to memory made by one thread become visible to another thread

Inter Process Communication (IPC)

pipes and sockets

ThreadLocal, BlockingQueue, Counting Semaphore and ConcurrentHashMap

MessageDigest

This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.

  • MD5
  • SHA-1
  • SHA-256

http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest

http://docs.oracle.com/javase/7/docs/api/index.html?java/security/MessageDigest.html

String text = "This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.";
MessageDigest md;
try {
    md = MessageDigest.getInstance("SHA");
    md.reset();
    md.update(text.getBytes());
    System.out.println(md.digest().toString());
} catch (Exception e) {
    e.printStackTrace();
}

Encoding

Autoboxing and Unboxing:

Autoboxing: automatically convert from a primitive to a wrapper

The "wrapper" classes for the primitive data types allow you to make a non-primitive object on the heap to represent that primitive type. For example:

primitive(stack)

char c = 'x';

wrapper(heap)

Character ch = new Character('x')
Character ch = 'x';

Strings in switch Statements

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
    String typeOfDay;
    switch (dayOfWeekArg) {
        case "Monday":
            typeOfDay = "Start of work week";
            break;
        case "Tuesday":
        case "Wednesday":
        case "Thursday":
            typeOfDay = "Midweek";
            break;
        case "Friday":
            typeOfDay = "End of work week";
            break;
        case "Saturday":
        case "Sunday":
            typeOfDay = "Weekend";
            break;
        default:
            throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
    }
    return typeOfDay;
}

java -jar vs java -cp

Cannot use both!

  • java -jar: specify an executable jar, the classpath should be specified in Manifest
  • java -cp: specify classpath

http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Why no Operator overloading

Operator overloading always leads to abuse

The right way to print an array

System.out.println(Arrays.toString(arr));

Null

do not use null values in a set or as a key in a map.

Set.isEmpty() does not check for null

if null is needed, use Collections.unmodifiableList() otherwise use Guava's ImmutableList

https://github.com/google/guava/wiki/UsingAndAvoidingNullExplained