Java - Introduction

Created:2018-08-30  Last modified:2019-10-24


  1. Java Introduction

    Java: Object-Oriented Programming, Write-Once-Run-Anywhere

    .java code -> (javac) -> .class bytecode -> (java) -> JVM run

    Born in 1995, James Gosling, Sun Microsystems

    JDK (Java Development Kit): A tool sets include JVM, and programming tools, standard packages.

    one feature released every 6 months. (6月一小更)

    JIT (Just in Time compilation)

    Java code is compiled into bytecode, and the bytecode is interperted by JVM. It is slower than running machine code directly in a CPU. So JIT comes in. JIT will compile the most frequently used bytecode (-XX:CompileThreshold=5 set the frequency to 5) and compile it to machine code.

    The name of Java JIT is HotSpot, JIT is part of the JVM

    Granularity of JIT is in function level.

                    java -client -Xbatch -XX:+PrintCompilation ClassName
                    #print the info if JIT is involved.
                    #-client: make sure to run in client mode.
                    #-XX:+PrintComiplation: print compile info
                    #-Xbatch: make output serially, not concurrently. In other words, JIT compilation will block the program execution.
                
    1. JDK1.1: Inner class, limited reflection, JavaBean, rmi, JDBC
    2. J2SE 1.2: strictfp, Swing, JIT compiler, collections package, weak/soft/phantom references
    3. J2SE 1.3: modified RMI
    4. J2SE 1.4: exception chain, IPv6, assertion(断言)
    5. J2SE 5 (2004, 09, 30): generics, annotation, autoboxing, enumeration, for each loop, static import
      5 is product version, 1.5 is developer version
    6. J2SE 6:
    7. J2SE 7: lambda expression
    8. J2SE 8 (2014, 03, 18), JVM replaced permgen space by metaspace
    9. J2SE 9: AOT (ahead-of-time compilation); modularity, deprecate finalize
    10. Java SE 10: local var keyword;
    11. Java SE 11: the current long term support Java.

    Java EE

    1. Java SE is for small scale development, e.g. a desktop application. Java EE is for server-side large scale programming, which defines more standard (Interface) e.g. servlet
    2. JSR-369 (Java Specification Requests, A Java Community Process) defines the standard of Servlet container and Servlet. Including lifecycle, common APIs.

    Java Language Specification

    Java language specification is a standard. It can have multiple implementations, e.g. Oracle JRE (HotSpot), JDK and OpenJDK.

  2. Java Project Structure

    Java uses "package" to organize code, it's a logic organization, like C# and C++'s namespace. The difference is that the "package" is not only expressed with package com.company.feature but also the physical directory hierarchy.

    A java's code is compiled into Bytecode .class, and then compiled into a library .jar file.Different from C# that a namespace can cross multiple .dll assembly, Java package cannot cross multiple .jars./p>

    1. Package example

              /directory1
                  /directory2
                      /directory3
                          Xxxx.java
              
                  package directory1.directory2.directory3;
                  import ....;
                  // java.lang.* is imported by default.
                  /* Common class defined by java.lang,
                      String, Boolean, ....
                      Math, Object, Thread, Void,
                      Exception, ArithmeticException
                  */ 
                  public class Xxxx{
                      public static void main(String [] args){
      
                      }
                  }
              
  3. Command lines

    javac: compile .java to .class
    jar: package .clas to .jar and unpackage .jar
    java: run

    1. Compiling with external .jars, multiple sources, and a desginated output.

      -cp or -classpath: 3rd party jars
      -d: output location

                                  javac -cp ".:../../jars/*" 
                                  ../src/umn/dcsg/wieralocalserver/*.java 
                                  ../src/umn/dcsg/wieralocalserver/datadistribution/*.java 
                                  ../src/umn/dcsg/wieralocalserver/storageinterfaces/*.java 
                                  -d ../../out/production/wieraLocal
                              
    2. jar packaging

      When packaging a project to a .jar, a manifest will be included into the jar. If the package is a library, then we can let jar generate the manifest file. But if the project has the main entry, we have to indicate the entry in the manifest file.

      The manifest file (MAINFEST.MF) should at least include the following line to indicate the main entry.

                              Main-Class: classname
                          
                              # jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
                              # -c create new archieve
                              # -m include a manifest file
                              # -v verbose mode 
                              # -f output name
                              jar cmvf MANIFEST.MF output.jar -C output/ ./Nan/Review/Program.class
      
                              # -C the directory that contains compiled packages.
                          
    3. jar unpacking

      Unpacking is useful because some 3rd party libraries also include their documentation inside the .jar file. Download some source code from Maven repo. Unpack it with jar to look through source code and read documents

      Two versions of jars are available in the maven repo, .jar and javadoc.jar.
      The javadoc.jar contains the generated java doc.

                              #unpack
                              jar -xf javadoc.jar 
                              # -x extract files from the archive. 
                              # -f give the archive .jar name.
                          
    4. java start program

                              # start a .class file
                              java -cp "../../jars/*:../../out/production/wieraLocal/"  umn.dcsg.wieralocalserver.LocalServer
                              # start a jar file that has a manifest
                              java -jar output.jar
                          
  4. Update Java Version

    Update java verson on MacOS

    1. You may install multiple java/javac (jre/jdk) on the same OS. Now you want to switch java version.
                              # 1. check current used java version
                              java -version; javac -version
                              # 2. check all installed java 
                              /usr/libexec/java_home -V
                              # 3. switch version, update (add) JAVA_HOME environment variable in ~/.bash_profile
                              export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
                          
  5. Rules

    1. Package

      Java organizes code as packages. Packages are not only the logic partition of code functionalities, but also physical partition on code distribution. Multiple packages can be compiled into a single .jar. But a single package cannot exist in multiple jars. In contrast, C# uses namespace as a logic partition, and .dll assembly for code distribution. A namespace can cross multiple .dll, a .dll can have multiple namespaces.

    2. .java file

      Each .java file must have and only have one public class, or enum, or interface definition. And the type name must same as the filename. A .java can have multiple non-public class definition.