2012-09-03

Java Refcard

This page about Java is quite old, from days when Java was new and kinda slow. Today there surely are all kinds of goodies and much better libraries.

/* ... */    multiline comment          /** ... */ javadoc comment, common tags
//           until end of line comment  @see @version @return @param           
\u1234       UNICODE character          @author @execption @deprecated         
                                                                               
public       access for all classes                                            
protected    access for classes from same package and subclasses               
<default>    access for classes from same package                              
private      access for no-one but the class itself                            
                                                                               
static       class variable, access by class not instance, inited in class init
final        class can not be subclassed, field can not be changed             
static final CONSTANT, is inlined during compilation                          
{ ... }      block delimiters                                                 
" ... "      String literal,                 
                                                  
void         no return value from method    ++, --  increment/decrement                       
boolean      true/false                     +, -    unary                                     
char         16 bit (UNICODE character)     ~       bitwise NOT                               
byte          8 bit                         !       logical NOT                               
short        16 bit integer                 (type)  typecast                                  
int          32 bit integer                 *, /, % mult, div, remainder                      
long         64 bit integer                 +, -    add, substract                            
float        32 bit IEEE754 floating point  +       String concat                             
double       64 bit IEEE754 floating point  <<      left shift                          
Object       any kind of non-primitive type >>      right shift sign ext.                                           
null         null reference to an Object    >>>     right shift no ext                                           
                                            <, <=, >=, > numeric comparison                                   
field        instance or class variable     instanceof   type comparison                  
method       function, subroutine           ==, !=  equality, inequality of               
member       field or method                        value or reference                    
constructor  class instance initializer     &       bitwise or boolean AND                       
                                            ^       bitwise or boolean XOR                             
\ is escape character                       |       bitwise or boolean OR               
\b backspace       ^H 08                    &&      conditional AND             
\t tab             ^I 09                    ||      conditional OR                                   
\n newline            0A                    ?:      conditional ternary op                                              
\r carriage return ^M 0D                    =       assignment                            
\f form feed                                *=, /=, %=, +=, -=, <<=, >>=,     
\" "                                        >>>=, &=, ^=, |= assign with op  
\' '                                        .equals() obj content comparison              
\\ \                                          
\nnn octal number (max. 377)                  

Reference class header

                                                                              
// for a class in a package, the .class file has to be in a subdir of the     
// classpath or of a jar file: for example, lets look at
// <classpath-dir>/pack/subpack/MyClass.class
package pack.subpack;                                                         
                                                                              
public   // visible outside of package. Only one class/file may be public.    
final    // cannot be subclassed                                              
abstract // contains abstract methods (methods without implementation block)  
class MyClass // instanceof MyClass, MySuperClass, MyInterface        
extends MySuperClass   // is a subclass of MySuperClass:                      
                       // inheritence of non-private members of MySuperClass  
                       // overriding of methods with same name                
                       // shadowing  of fields  with same name                
implements MyInterface // is a MyInterface, implements methods declared there 
{                                                                             
//fields
    // these modifiers can be used only with instance or class    
    // variables, not with vars local to a block (which always    
    // have only block-wide scope and visibility)                                                                        
    public or protected or private or <default>  
    final       // can not be overridden                                           
    static      // "global", shared by all instances of the class             
    MyType myVar; // declaration. If init missing, Java supplies default init 
                  // to 0 or  0.0 or false or null.                           
    static {   // static constructor for init of static vars. executed once   
    }          // when class is loaded.                                       
                                                                              
//constructors                                                                
    public or protected or private or <default>                   
    MyClass() {  // has no return type, since always returns                  
    }            // constructed object. If none specified, Java supplies      
                 // default MyClass() { super(); } constructor.               
    // this(args)  call another constructor of this class or                 
    // super(args) call another constructor of superclass, both have to be   
    // first statement in block. If missing Java supplies super()             
                                                                              
//methods                                                                     
    public or protected or private or <default>                   
    //if class final or method final or private, no overriding is possible,   
    //no dynamic binding needed, less overhead, calls are faster              
    final  // can not be overridden                                           
    static // class method not bound to instance, is also inherited,
           // thus implicitly final          
    native // platform dependent, makes use of native code
    synchronized // aquires monitor lock before executing (peer class if      
                 // peer object otherwise). used in multitherading            
    abstract // ; instead of {...} block                                      
    methodName(params) // overloading: same name with different param list    
    throws AnException // overriding methods must throw same exc. or subtype  
    

Classpath

The classpath is a list of places where Java classes can be found.
In the following path names jre refers to your java home directory, also specified in the java environment variable java.home, which is the top-level directory of your JRE installation. When the VM is searching for a class of a particular name
  1. it will first look among the bootstrap classes. These are core classes like the java.lang package. They are located in jre/lib/rt.jar and jre/lib/i18n.jar.
  2. it will next look for the class among any installed extensions. Installed extensions are jar files in the directory jre/lib/ext. A installed extension may contain executables and shared libraries (such as .dll files). Executables are placed in jre/bin on Windows, or jre/lib/[arch] on Unix. Native libraries may also be placed in jre/lib/ext/[arch], searched afterwards.
  3. it will then look for download extensions. A download extension is a jar file, and it's location is explicitely specified in the Class-Path header field in the manifest of another jar file. Download extensions cannot have any native code.
  4. finally, it searches the classpath, which is stored in java.class.path. You can set it explicitely using the option -classpath or -cp. Otherwise, the CLASSPATH environment variable sets it. If this also is not available, then it defaults to the current directory (which most of the time will not work). When using the -jar option, the classpath is set to the jar file, which overrides any other definition. The JAR file's manifest is consulted to find the name of the application's main class specified via the Main-Class manifest attribute.
When you install both JRE and JDK, you should use the directories of the JRE, which are also set in your registry on Windows by the installer. If you have both a JDK with it's own JRE and a standalone JRE, the system should use the latter . If it still looks under the JDK-JRE instead, rename the "jre" folder in the JDK, which will force it to use the actual JRE.