| Java Primitive Data
Types |
||||
| Type | Contains | Default | Size | Range |
boolean |
true or false | false | 1 bit | n.a. |
char |
Unicode character unsigned | \u0000 | 2 bytes (16 bits) | \u0000 to \uFFFF or 0 to 216-1 |
byte |
Signed integer | 0 | 1 byte (8 bits) | -128 to 127 or (-27 to 27-1) |
short |
Signed integer | 0 | 2 bytes (16 bits) | -32768 to 32767 or (-215 to 215-1) |
int |
Signed integer | 0 | 4 bytes (32 bits) | -2147483648 to 2147483647 or (-231 to 231-1) |
long |
Signed integer | 0 | 8 bytes (64 bits) | -9223372036854775808 to 9223372036854775807 or (-263 to 263-1) |
float |
IEEE 754 floating point single-precision | 0.0f | 4 bytes (32 bits) | 1.4E-45 to 3.4028235E+38 |
double |
IEEE 754 floating point double-precision | 0.0 | 8 bytes (64 bits) | 439E-324 to 1.7976931348623157E+308 |
| Java Language
References |
|||||||||||||||||||||||||
| Escape code for
Java
Regular Expression
http://www.wellho.net/regex/javare.html
|
Java Regular
Expressions
The following are some char keyword in used by the regular expressions class. In order to use the char, use \ followed by the char or insert the printable string between \Q and \E metacharacters There are more about regex than I thought after searching the web. http://en.wikipedia.org/wiki/Regular_expression String tx = "Delta values are labeled \"\u0394\" on the chart."; |
| Java Fundamental
References |
|
//Basic
java class structurepublic class ClassName public method1() public
static void main(String args[]) |
|
//Java do not have unsigned data type //Representation of
the unsigned for a signed data type.byte temp; //extract the unsigned value |
|
//f indicates that 1.5 is of data type float rather
than a double.//(any literal is
intepreted as a double and not a float)float x = 1.5f ; double x = 1.5; |
|
//Casting
a numeric value to a charchar c3 = (char)87;char c1 = '\u0057'; |
|
//declare
& allocate memory for byte arraybyte remoteIP[] = new byte[4]; |
|
//defining
byte constant arrayprivate static final byte[] MYCONSTANT = {0x04, 0x04, (byte)0xFF, 0x64}; |
|
//Bitwise
Operator>>> //right shift without preserving the sign
bit>> //right shift preserving the sign bit<<
//left shift preserving the sign bit |
|
//to
check/compare the data/object type of a unknown java objectboolean x = aAnimal instanceof Fish |
| Time, Date,
Calendar,
Timer |
|
//Delay/sleep functiontry{Thread.sleep(1000);}catch(InterruptedException e){} |
|
//System time counting in millisecond. Measuring the
cpu time to execute certain tasklong sysMiliSecCount = System.currentTimeMillis( ); |
|
//Calendar, Date, Time (data & formatting function)Calendar cal; |
| Array, Data Container | |
//Arraylist container exampleArrayList<String> a; |
|
HashMap<Integer, Computer> map;//Iterator using the HashMap's
key//Iterator<Integer> i=map.keySet().iterator();//Iterator using the HashMap's
valueIterator<Computer> i=map.values().iterator(); |
|
//Example:
Ensure that the Array data container object is synchronized//Get the list that is synchronized
from ArrayList mySyncList = Collections.synchronizedList(arrayList);//Another synchronized example for Map containerTreeMap<object1,object2> myMap = new
TreeMap<object1,object2>();//Get the list that is synchronized
from ArrayListmySyncMap = Collections.synchronizedMap(myMap); |
//Java
ArrayList is NOT synchronized, meaning it is possible that any two
process can access the un-sychronized data at the same time, causing
runtime error. Two process may try to write data to the same memory
location. |
| Integer String
Manipulation |
|
int num = Integer.parseInt(String); |
//Example:
Convert from string to numerical value//parse hex string//convert a number to a binary string |
String.split(); |
| Java debugging,
exception and error handling resources |
|
|
log4j Example: FactoryLog for logging info, error, debug, etc messages. Similar to System.out.println();
Save to project folder /lib Save to project folder /resource /resource (factory logger).zip (includes examples to config Log4j for logging multiple classes into individual log file.)
Log4j references: |
|
//Using LogFactory for debugging & logging example.import org.apache.commons.logging.Log;
//print out the class name |
|
//Two examples to print out error messages:e.printStackTrace(); |
|
public KnxSession() throws KnxCommException |
|
//KnxCommExeception Classpackage knxEib.knxException; |
| File I/O,
Properties/Configuration file |
|
|
Example: File Write
String filename = getClass().getName()+".txt"; |
|
BufferedReader input = null; |
//Example
of reading configuration from a property fileprivate void loadNetworkConfig()getClass.getClassLoader().getResourceAsStream(filename); |
|
//Various method of loading file/resources//The java code should obtain the
resource using getClassloader() as the standard//loading file that is located in the
directory as specified in the classpathInputStream is = getClass.getClassLoader().getResourceAsStream("file.txt");//same as above (another way of
writing the code)InputStream is = getClass.getResourceAsStream("/file.txt");//loading file that is located in the
same directory as the *class file//looking for file in the current
class packageInputStream is = getClass.getResourceAsStream("file.txt"); |
|
//inside the file config.propertiesIP=192.168.1.1 |
|
| Detect file modification.pdf |
| Threading processes |
|
//Threading example public class myThreadClass extends Thread//flag to stop the thread process private
boolean fIsStopRequested=false;
//flag to control process flow to be in syn with other critical function private static boolean fIsSynProcess=false;//object holding the
reference to this thread private Thread thisThread = null;public myThreadClass() public void run()//getting the thread process ID Thread thisThread = Thread.currentThread(); String
threadName = getClass().getName() + " routine, thread started: " + thisThread.getName();
while(true)//stop to stop thread
if(isStopRequested())
//other process in progress, hold thread for a moment
while(isSynProcess())//Place the looping thread
process code here. }
//interrupt for used to exit from blocking function in the run() method's while loop if (tempThread != null)} |
|
//Spinning off a new Thread process without writing
inside a proper class fileThread rescan = new Thread(new Runnable()
//ToDo: Place your code that } |
|
//Another method of writing threadObject obj = new Object(); |
| Java Destructor Method - ShutdownHook | |
//This example continue from the previous
myThreadClass example.//ShutdownHook is
registered for the myThreadClass thread process.//If the java program terminate
abnormally, shutdownhook will be activated.//to stop
myThreadClass's thread tc.requestStop().private ThreadClassShutdownHook tcShutdownHook = null; //add this to the thread class variabletcShutdownHook = new ThreadClassShutdownHook(tc);
//add this to
the thread class constructor |
|
public class ThreadClassShutdownHook extends ThreadmyThreadClass
tc;ThreadClassShutdownHook(myThreadClass tc) |
| GUI & Event
Listener example - Create window - Window listener - Mouse listener - Keyboard listener |
|
| - example
interface, inherirt,
abstract (pc, network projector project) |
|
| -
http://www.javamex.com/tutorials/threads/invokelater.shtml any swing component codes that requires periodically refresh need to apply this invoke function. http://www3.ntu.edu.sg/home/ehchua/programming/java/J5e_multithreading.html |