Monday, May 28, 2012

GIST NOTES 10 - Java Development


GIST NOTES 10 - Java Development

[DISCLAIMER: This is solely for non-commercial use. I don't claim ownership of this content. This is a crux of all my readings studies and analysis. Some of them are excerpts from famous books on  the subject. Some of them are my contemplation upon experiments with direct hand coded code samples using IDE or notepad.


I've created this mainly to reduce an entire book into few pages of critical content that we should never forget. Even after years, you don't need to read the entire book again to get back its philosophy. I hope these notes will help you to replay the entire book in your mind once again.]


[Java 7.0]

Compiler
========
javac [options] [source files]


-d option
----------
>destination folder to where .class files should go
>compiler automatically can create package structure folders while creating .class files
>the destination directory should exist in order for this command to succeed


Running Java Application
=========================

java [options] class [args]


java -DmyProp=myVal Myclass a b

-D option is used to set system properties

java.util.Properties can be obtained by System.getProperties() which contains general JVM/OS info, version info and along with it we can add our own user defined properties also to it (e.g. Properties p = System.getProperties(); p.setProperty("prop1","value1");

The same thing is accomplished by -D option. When property value contains spaces, you should double quote it when setting it from command line.

The following are valid:

static public void main(String[] args)
public static void main(String... s) //valid from java 5
static public void main(String hi_input_fellas[])

searching for classes
----------------------
>both java and javac search the same way, same set of directories in the same order
>first they search jdk standard directories
>second in the directories defined by classpaths
>classpath can be defined as OS environment variable or as an option to java/javac commands
>command line classpaths override environment variable classpath
>multiple class paths are separated by colon ( -classpath com/abc/hi:com/lexi )
>in the above example it will not search com or com/abc
>they don't search the current directory for .class files by default

-classpath ./com/hi:./com/lexi:.

>however, javac will search current directory by default for .java files
>in case of duplicate .class files, the file encountered first will be used
>in the classpath search goes from left to right
>option -classpath can be abbreviated to -cp

Packages
--------
>fully qualified name of a class includes the entire package (com.murmur.tools.MyTool)
>when we refer just 'MyTool' after importing the class, it is still an alias referring to com.murmur.tools.MyTool
>classpath should always include the directory which contains the root package(top most package, e.g. com/)
>jar -cf myjar.jar com -->creates the jar containing com directory available in the current directory
>jar -tf myjar.jar -->unjars the jar in the current directory
>java and javac will use a jar like a normal directory tree
>jar files can be searched by including them in class path just like packages; jar file is considered as a directory containing the top-level package that is 'com'
>when we say 'import java.util.*;' we only import classes under util package, but not other sub packages under util e.g. java.util.regex will not be imported
>import statement can import only a single package at a time
>java/javac search JDK/jre/lib/ext directory for any jars and include all of them into classpath automatically(should not be used in shipments of your application)
>

Static Imports
--------------
>ability to import static variables

import static java.lang.Integer.*; //static import
import static java.lang.System.out; //static import
import java.util.*; //regular

...
...


out.println(MAX_VALUE); //instead of Integer.MAX_VALUE and System.out.println()


>'static import' will not compile; it should be 'import static'
>if static imports import same name from two different classes (Integer.MAX_VALUE and Long.MAX_VALUE), usage of such ambiguous variables cause compiler error
>you can static import, constants, objects and methods which are static

Reference: Kathy Sierra's SCJP Book

No comments:

Post a Comment