This code is exactly as it is in the book Learning Java (Oracle Press Books), but it doesn't work. I don't understand why it doesn't work, it should work. I've tried it with both OpenJDK and Sun JDK 7, the error is the same.
ThreadCom.java:56: error: unexpected type
if (thrd.getName().compareTo("Tick") = 0) {
^
required: variable
found: value
1 error
The code in question...
class MyThread implements Runnable {
Thread thrd;
TickTock ttOb;
MyThread(String name, TickTock tt) {
thrd = new Thread(this, name);
ttOb = tt;
thrd.start();
}
public void run() {
if (thrd.getName().compareTo("Tick") = 0) { // <- that line
for (int i=0; i<5; i++) ttOb.tick(true);
ttOb.tick(false);
} else {
for (int i=0; i<5; i++) ttOb.tock(true);
ttOb.tock(false);
}
}
}
The code is exactly as it is in the book.
this
thrd.getName().compareTo("Tick") = 0
should be
thrd.getName().compareTo("Tick") == 0
the first is an assignment. the second is a comparison.
I forgot the double equals, just noticed it.
if (thrd.getName().compareTo("Tick") = 0)
should be
if (thrd.getName().compareTo("Tick") == 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.