This example shows what happens for the instance method, static method, variable in the inheritance.
======================== Code ========================
public class Sub extends Super{
int x = 15;
static int y = 25;
public static void main(String args[]){
//super reference and super object
Super t = new Super();
out.println(t.x + " " + t.y);
t.methodOne();
t.methodTwo();
//super reference and sub object
Super ts = new Sub ();
out.println(ts.x + " " + ts.y);
ts.methodOne();
ts.methodTwo();
//sub reference and sub object
Sub s = new Sub();
out.println(s.x + " " + s.y);
s.methodOne();
s.methodTwo();
}
static void methodOne() {out.println("Sub methodOne()");}
void methodTwo() {
out.println("Sub methodTwo()");
methodOne();
}
}
class Super{
int x = 10;
static int y = 20;
static void methodOne() {out.println("Super methodOne()");}
void methodTwo() {
out.println("Super methodTwo()");
methodOne();
}
}
================================================
======================== Answer ========================
10 20
A methodOne()
A methodTwo()
A methodOne()
10 20
A methodOne()
B methodTwo()
B methodOne()
15 25
B methodOne()
B methodTwo()
B methodOne()
================================================
======================== Conclusion ========================
static methods and variables are based on the reference type
Real object type determines which overridden method is used at runtime
================================================
Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts
Tuesday, November 18, 2008
Tuesday, November 11, 2008
SCJP
Those are my study note of the SCJP. I used those notes to prepare the SCJP exam. Wish myself have luck for the exam.
Uploaded on authorSTREAM by maggie.zhou
Uploaded on authorSTREAM by maggie.zhou
Uploaded on authorSTREAM by maggie.zhou
Uploaded on authorSTREAM by maggie.zhou
Uploaded on authorSTREAM by maggie.zhou
Uploaded on authorSTREAM by maggie.zhou
Uploaded on authorSTREAM by maggie.zhou
Uploaded on authorSTREAM by maggie.zhou
Tuesday, September 16, 2008
A great Thread example from Nikos Pugunias(http://nikojava.wordpress.com/)
Hi Zhou!
You should be 100% certain that wait() is a method of Object.Consider 2 threads: Thread A carries out a loooong task. Thread B must wait for A's result to go on with its job.This trick between threads happens with a single object that both threads can access: a token. it's like the piece of wood that the athletes running 4x100m in the Olympics: Runner 1 starts, Runner 2 waits for the token from Runner 1, as soon as he gets it Runner 2 starts and so on...
//Here's the first runner,
class Runner1 extends Thread { }
//that holds a wooden stick.
//This runner has a loooong task to do,
//and then he will give the token to another runner.
//Here's the second runner
class Runner2 extends Thread { }
//that can also receive a wooden stick.
//This second runner has also something to do,
// but he'll have to wait for the token first.
//+++++++++++++++++++++++++++
//Here's the complete code.
//+++++++++++++++++++++++++++
//*************** Conclusion: ***************//
Using a single object, two threads may schedule in what order they will run in accuracy.In the question you have posted, there two threads: Job and main. They schedule themselves based on the [B]thread[B] instance.It's just a choice to use this instance instead of any other object.Review:When thread A calls token.notify() it says: You may take the token.When thread B calls token.wait() it is waiting to take the token.Always synchronize on the token.
You should be 100% certain that wait() is a method of Object.Consider 2 threads: Thread A carries out a loooong task. Thread B must wait for A's result to go on with its job.This trick between threads happens with a single object that both threads can access: a token. it's like the piece of wood that the athletes running 4x100m in the Olympics: Runner 1 starts, Runner 2 waits for the token from Runner 1, as soon as he gets it Runner 2 starts and so on...
//Here's the first runner,
class Runner1 extends Thread { }
//that holds a wooden stick.
class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
}//This runner has a loooong task to do,
class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
public void run() { // perform a loooooong task here }
}//and then he will give the token to another runner.
class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
public void run() {
synchronized (stick) {
// perform a loooooong task here stick.notify();
}
}
}//Here's the second runner
class Runner2 extends Thread { }
//that can also receive a wooden stick.
class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
}//This second runner has also something to do,
class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
public void run() {
// I want to do something after
// the first runner has finished.
}
}// but he'll have to wait for the token first.
class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
public void run() {
// wait for Runner1
synchronized (stick) {
try { stick.wait();
} catch (InterruptedException e) { }
}
// I want to do something
//after the first runner has finished.
}
}//+++++++++++++++++++++++++++
//Here's the complete code.
//+++++++++++++++++++++++++++
public class Run {
public static void main(String[] args) {
Object token = new Object();
new Runner1(token).start();
new Runner2(token).start();
}
}
class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
public void run() {
synchronized (stick) {
// perform a loooooong task here stick.notify();
}
}
}
class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
public void run() {
// wait for Runner1
synchronized (stick) {
try { stick.wait();
} catch (InterruptedException e) { }
}
// at this point Runner1 is guaranteed
// to have finished his work
}
}//*************** Conclusion: ***************//
Using a single object, two threads may schedule in what order they will run in accuracy.In the question you have posted, there two threads: Job and main. They schedule themselves based on the [B]thread[B] instance.It's just a choice to use this instance instead of any other object.Review:When thread A calls token.notify() it says: You may take the token.When thread B calls token.wait() it is waiting to take the token.Always synchronize on the token.
Saturday, September 13, 2008
First Java step: set classpath
From website: http://mindprod.com/jgloss/classpath.html
The CLASSPATH is an environment variable that tells the Java compiler javac.exe where to look for class files to import or java.exe where to find class files to interpret.
In contrast, the PATH is an environment variable that tells the command processor the where to look for executable files, e.g. *.exe, *.com and *.bat files. The Classpath is one of the most confusing things in Java. Unfortunately, you must master it to even compile HelloWorld.
CLASSPATH And File Naming RecipesHere are my simplified rules for using CLASSPATH and naming the files on the javac.exe and java.exe command line:
The CLASSPATH is an environment variable that tells the Java compiler javac.exe where to look for class files to import or java.exe where to find class files to interpret.
In contrast, the PATH is an environment variable that tells the command processor the where to look for executable files, e.g. *.exe, *.com and *.bat files. The Classpath is one of the most confusing things in Java. Unfortunately, you must master it to even compile HelloWorld.
CLASSPATH And File Naming RecipesHere are my simplified rules for using CLASSPATH and naming the files on the javac.exe and java.exe command line:
Put every class in a package. Don’t use the default package.
Use the latest JDK. It will be the one everyone you ask help from is familiar with. Two dangling prepositions in one sentence. Churchill would be proud.- Configure your SET CLASSPATH= in the environment to clear it out.
- You don’t need to explicitly include rt.jar:
rt.jar system class files :
in C:\Program Files\java\jre1.6.0_07\lib\rt.jar in the JRE 1.6.0 on your local hard disk.
in J:\Program Files\java\jdk1.6.0_07\jre\lib\rt.jar in the in the JDK 1.6.0 on your local hard disk.
in C:\Program Files\java\jre1.5.0_16\lib\rt.jar in the older JRE 1.5.0 on your local hard disk.
in J:\Program Files\java\jdk1.5.0_16\jre\lib\rt.jar in the in the older JDK 1.5.0 on your local hard disk.
unless you are using Jikes, where you need to add it to the JIKESPATH, but not the CLASSPATH.
In all that follows, everything is strictly case sensitive.
- To compile a HelloWorld.java app in the default package in C:\MyDir, use cd \MyDir
javac.exe -classpath . HelloWorld.java - To run a HelloWorld.class app, in the default package in C:\MyDir, use cd \MyDir
java.exe -classpath . HelloWorld - To compile a HelloWorld.java app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
cd \
javac.exe -classpath . com\mindprod\mypackage\HelloWorld.java
rem alternatively
e:
cd E:\com\mindprod\mypackage
javac.exe -classpath .;C:\ HelloWorld.java - To run a HelloWorld.class app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
cd \
rem not you do not do cd \com\mindprod\mypackage!!
java.exe -classpath . com.mindprod.mypackage.HelloWorld - To compile a HelloWorld.class app in C:\com\mindprod\mypackage, in into a jar called helloworld.jar in package com.mindprod.mypackage, use
- To run a HelloWorld.class app in C:\com\mindprod\mypackage, in a jar called helloworld.jar in package com.mindprod.mypackage, use CD \dirwherejaris
java.exe -classpath helloworld.jar com.mindprod.mypackage.HelloWorld
rem or if your Main-Class manifest points to HelloWorld.class.
java.exe -jar helloworld.jar
rem or if your *.jar extension is set to invoke java.exe
helloworld.jar
If for any reason the examples shown do not work with your version of java.exe, try replacing the \ in com\mindprod\mypackage\HelloWorld with / mypackage/HelloWorld.If you stare long enough at those examples, you may understand the logic behind them, and then you can create variants. If you can’t, just slavishly copy the closest matching example.
Friday, September 12, 2008
== vs. equals() in Java
In JAVA, you may be confused with "==" and equals() all the time. What are they all about?
"==":
For primitive: check the velue
For String:
1.if see any new(means String s = new String("xxx"); -->check if they pointing to same object
2.if see only ""(means String s = "xxx"; ) -->check the string content
For Wrapper:
1. same as String, see any new --> check if they pointing to same object
2, if there is not, then tricky things coming:
2.1, if it is byte, short, int and the value is <=127, -->check the value
2.2, otherwise check the if they pointing to same object
equals():
For primitive: no way to use this
For general Object:
1.must override it to compare the key attribute(s) of object;
2,if collection is Hashxxx, you need override hashCode() too, otherwise equals won't effect
For String and wrappers:
they have well overridden equals() and hashCode()
"==":
For primitive: check the velue
For String:
1.if see any new(means String s = new String("xxx"); -->check if they pointing to same object
2.if see only ""(means String s = "xxx"; ) -->check the string content
For Wrapper:
1. same as String, see any new --> check if they pointing to same object
2, if there is not, then tricky things coming:
2.1, if it is byte, short, int and the value is <=127, -->check the value
2.2, otherwise check the if they pointing to same object
equals():
For primitive: no way to use this
For general Object:
1.must override it to compare the key attribute(s) of object;
2,if collection is Hashxxx, you need override hashCode() too, otherwise equals won't effect
For String and wrappers:
they have well overridden equals() and hashCode()
Subscribe to:
Posts (Atom)
