Posts Tagged java
Jave Regular Expression study guide
Metacharacters:
\d : A digit
\w : A word char
\s : A white space char
[] : match one of the char contained in the square bracket
Quantifiers:
* Zero or more occurrences
? Zero or one occurrence
+ 1 or more occurrence
The Predefined Dot:
. any char
NOTE:
conversion type is used for formatting NOT for regex.
eg. printf(), format() …
bcdfs
b-boolean, c-char, d-integer, f-float,s-string
escape sequence:
\n = line feed
\b = backspace
\t = tab
Java Assertion Study note
In SCJP exam, You are expected to know appropriate and inappropriate assertions
appropriate:
1. Do check private method
2. check code never should be reached
inappropriate:
1. check public method
2. check command-line arguments
3. assertion check will bring side affects
eg.assert a=a+1 // a’s value will be varied depending on whether assertion is enabled or not.
Java Interface Study note
1. All methods are implicitly public abstract.
2.All variables are implicitly final static constants.
3.Interface CAN NOT have static method.
WHY : Java allows mult-interface implementation, what the child should inherit if two interfaces have the same static method?
4. casting a class to interface is always legal in Java during compile.
eg.
for A class, you can cast it to an interface, this rule apply to instanceof as well.
eg1.Runnable r = (Runnable)new A();
note:
but runtime if A has no relationship with Runnable, will throw Runtime Exception(ClassCastExcetption)
if A is a final class and has no relationship with Runnable, it will fail compile.
eg2.A a = (A)r; // this is legal as well.
eg3.$Left instaceof $Right, Left can be any objects,but no interface(fail the build), right can be interfaces
note:
again, compile will fail when $left is an ojbect of a final class, $right is an interface having no relationship with $left.
Java Thread Study note
1.A new Thread gets started means a new stack gets kicked off apart from the main stack
2. InterruptedException(checked exception) will be thrown when a thread is waiting, sleeping and pausing for long time. (sleep(), wait() and join())
note:
yield() is not throwing this exception.
3.A thread can’t be started again once it’s started. Trying to start a thread more than once will throw IllegalThreadStateException(Runtime Exception)
4.wait(),notify(),notifyAll() are inherited from Object class. They have to be surrounded by the synchorized context. Otherwise it throws IllegalThreadMonitorException(Runtime Excpetion).
eg. sychronized(a) {
….
a.wait()
….
}
5. only thread can be started, not runnable.
6.Thread constructor can take Runnable or String(name of the thread).
Test Tips:
before you go any further, check the following!!!
Compile
1. start() – check if it is against Runnable – compile error
2. wait(),join(),sleep() – InterruptedException is handled or not – compile error
3.Thread.sleep() is ok, sleep() it self can be only called in the subclass of Thread. so class implements Runnable has to call Thread.sleep() not sleep() directly.
Runtime
1. wait(),notify(),notifyAll() needs to be in synchronized context – runtime exception IllegalMonitorStateException
2. the same thread gets called start() twice – runtime exception – IllegalThreadStateException
Java Inner Class Study note
Just quick sum-up what we should be careful during the SCJP exam when you see inner class questions.
CAN
1. inner class can be defined in method, in class, as arugments as well.
2.Method local inner class can only have modifier abstract and final.
CAN NOT
1. inner class CAN NOT have static methods. Neither static variable unless they are public static final constants
WHY
Because an inner class is implicitly associated with an instance of it’s outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it’s safe to declare static methods in a static nested class.
2. method local inner class CAN NOT access method local variables unless they are Marked as final.
WHY
Simply because, the method local inner class is like an local variable. When the method completes, the object might still be passed by reference on the heap refered and used by some other code. The method local variable just can’t live as long as the method-local inner class object .
3.static inner class (this is not actually considered as regular inner class) CAN NOT have access to outer class’s instance members (non-static methods + instance variables)
4.static inner class can not extend a non-static inner class(Just burn this in mind)
WHY
Just like static method can’t directly access instace method, it has to be via object of the class. Static class can’t have the access to instance members.
4.inner class can be private+abstract, but CAN NOT be final+abstract.
WHY
it’s easy to tell , rite? hehe, just think about it.
SCJP – scenarios you won’t get java compiled
-
Arrays.length VS String.length()
Most of the test tricks you by eg. args.length(), it will fail the compile. So don’t bother to figure out what results will be after execute. It won’t compile.
-
Ambiguous
This is a trick one, we will hit more ambiguous compile error in this article. One class implements/extends more than one parents. The parents have a same variable, the child has no idea which one gets inherited from what parent. Compile is confused and has no idea what to do. It gives you compile error(ambiguous).
eg. class A{int a;}
Inteface B{int a;}
FAIL: class C extends A implements B{ compiler has no idea which a to inherit}
eg.public void test(int… i) VS public void test(Integer… i)
both in the same class, when we make a call like test(4), compile fails as ambiguous call is made
-
instanceof
instanceof takes object on the left and interface/class on the right.So primitive will fail instanceof
eg. FAIL: int i=1;
i instanceof Integer is not legal, compile error.
-
binarySearch takes(List,key)
eg. Collection c = new ArrayList();
c.add(“shengjie”);
…..
Collections.binarySearch(c, “shengjie”); // compile error, c needs to have a explicit downcasting (List)c
-
Assignment
List <– ArrayList OK – Polymorphism
Integer <– int OK – Auto-boxing
Integer[] <– int[] FAIL - type is fixed when array initialised
-
passing param to method without explicit downcasting if downcasting is needed
-
Calendar.getInstance(Locale), SCJP test always tries to pass a date or smth else to Calendar.getInstance(Date) Compile Fail
-
When Overriding – It’s Public String toString(){} NOT Public void toString(){}
-
Catch Exception Sequence
sub-exception has to be caught before the super-exception, so the below will have compile error
eg.try{
}catch(RuntimeException){
}catch(IllegalStateException){ // this can be thrown by calling matcher.group() when matcher.find() is not true
}
-
Private Inner Class can’t be called/accessed by other classes
unlike the private fields can be accessed by getter/setter by other classes, inner class can’t be accessed by other classes. If the code snippet is trying to access the private inner class object, it fails the compile
-
Casting a class to anther class type which is not in the same hierarchy tree
-
static method tries to access Instance Member/Static class tries to access Instance Member
Answer to WHY is pretty simply, because static goes with class other than instance. So…bear it in mind. Static class can be thought as static member of the enclosing class.
-
Final field is declared and no initialization
public final int i; Compile Error
public final int i=0; OK
-
Arrays.reverse() doesn’t exist
Arrays : sort() binarySearch()
Collections: sort() binarySearch() reverse() reverseOrder()
Don’t let the test play you like a fool!!!
-
wait(),notify(),notifyAll() throws InterruptedException which is not handled
either catch it or throw it, doing nothing will fail the compile
-
Having unreachable code
eg.
…
public void test() throws Exception{
throw new RuntimeException();
throw new anotherException(); // this is ok, i am not sure why, just burn into your mind
int i = 0; // this never gets reached, so it fails the compile, tricky, yeah
LOL
}
jar command
Posted by shengjie in Tips Everyday on June 29, 2009
//create Jar file
jar -cmf
c:create
f:file
m:manifast
eg. jar -cmf -C ./workspace/jar-test/classes myJarTest.jar *.class
//Extract Jar file
jar -xf
x:extract
f:file
eg. jar -xf myJarTest.jar
// update jar file
jar -uf
u: update
eg. jar uf foo.jar foo.class
//list jar file content as a table
jar -tf
t:table
eg. jar -tf myJarTest.jar