Jia Tan

Ranch Hand
+ Follow
since Jan 28, 2009
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jia Tan

I have this test for reference:

My apologies, Ruben is right on this, it's not an Exception so won't be caught.
If you don't specify any options when running java command, the default behavior is use java -da and you will get 1 as a result. If you use java -ea option, your code will get into the catch block and get 3 as a result.
I think because String and Object are in the same inheritance tree, so comiple with no error. If you change


and uncomment the cast statement, it will compile but raise ClassCastException at runtime.
I would suggest the 4th rule being changed to:

4. Combination of the following overloaded methods will result in compiler error:
a. Var-args (V) of primitive type same as argument primitive type
b. Widening + Var-args (WV)
c. Boxing/Unboxing + Var-args (BV)


I guess the rules is: you cannot have more than one primitive widening + var-args (WV) overloaded methods, and you cannot have var-args(V) co-exists with other primitive widening + var-args (WV) overloaded methods

the following are all resulted in ambiguous by calling doX(5):

doX(int... i) & doX(long... l)
doX(int... i) & doX(float...f)
doX(long... l) & doX(double... d)
doX(int... i) & doX(long... l), call doX(5) get ambiguous too
I just checked the other machine, yes the classpath environment variable did exists with qt.jar, I believe it was set when I install quicktime. Anyways, after I remove the variable, javac and java start to pick up current directory by default. Thank agian to Punit for you insight!
For String/StringBuilder/StringBuffer functions, can we say index parameter are all 0-based, but the first index will always be inclusive, and the second index(if present) are always exclusive?
That machine is not at my hand now, I will check later. Thank you Punit for all the help!
That's why! Thank you Punit! I was testing K&B original example which has xcom under test, and in that directory there are A.class and B.class! Later I created com/xcom file structure under same test directory, and thing started getting confused! After I remove the xcom under test, everything back to normal.

I am still wondering why my other computer don't accept current dir why I didn't specify -classpath in javac or java, I will do more research on that.

I didn't set CLASSPATH on my machine. echo %classpath% simply returns "%classpath%"
I re-run the commands, they did worked on my machine. The environment on this machine is:
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_03
PATH=%JAVA_HOME%\bin (yes, I remove all other path entries)
All the commands I gave worked.

I tried on another computer, not only the commands you pointed out does not work, but also the commands without explicit -classpath won't work. I tried to include current directory (.) in the CLASSPATH environment variable, and the commands without explicit -classpath back on work again. What could be wrong with my settings?

Please if someone can run those commands on your machine and shed some lights on it. TIA
I did more test on the classpath issue, here is how:

directory structure:



After I did some test on compile and running the program, I found some really weird command combinations. The following commands are all tested on my machine:
(I'm using jdk 1.6.0_3)

javac command:

1) current directory is test:
javac com/xcom/B.java
javac -cp . com/xcom/B.java

2) current directory is com:
javac -cp .. xcom/B.java

3) current directory is xcom:
javac -cp ../.. B.java

java command:

1) current directory is test:
java com.xcom.B
java com/xcom/B
java com/xcom.B
java com.xcom/B

java -cp . com.xcom.B
java -cp . com/xcom/B
java -cp . com/xcom.B
java -cp . com.xcom/B

2) current directory is com:
java -cp .. com.xcom.B
java -cp .. com/xcom/B
java -cp .. com/xcom.B
java -cp .. com.xcom/B

java -cp .. xcom.B
java -cp .. xcom/B

3) current directory is xcom:
java -cp ../.. com.xcom.B
java -cp ../.. com/xcom/B
java -cp ../.. com/xcom.B
java -cp ../.. com.xcom/B

java -cp ../.. xcom.B
java -cp ../.. xcom/B

Basically, you need to include one level higher than the top package directory unless it's already current directory


for .class files:

If you do NOT specify -classpath or -cp, the java and javac searches current directory by default;
if you do specify -classpath or -cp, java and javac will not search current directory, if any dependent class file is in current directory, you need to include (.) explicitly in the -classpath option.

According to javac document:

-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This classpath overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.

In Sun's java document:

If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the current directory (.).