Ravi KumarJd wrote:Great answer - Thank you. I agree with you we cannot import instance methods or attributes directly with import statement. I wondered why the compiler not stopping me, when I append .* after a concrete class. If I understand correctly, for non static concrete Types/classes it's NOT better to append .* after the class name though the compiler okay.
As it stands, the code will not compile as the code does not acquire a lock on t before calling t.wait();
bob fissle wrote:I'm trying to write a loop that will print to the buffer based on the following conditions:
The example above is a dumbed down version of what I'm trying to do, I'm mainly interested in the logic. Please assume that variables a and b will have an infinite amount of numbers -- for the sake of this example. The main point that i'm trying to illustrate is the pattern in the variables a and b; a starts with 0 and 1, and then every other two numbers are found in the variable a, and the opposite for b. I'm trying to figure out how to write logic that will print out values a or b based on the pattern described above inside a loop.
Helen Ma wrote:Hi, Sebati,
Do you think the answer is only C. A few people on the forum feel that the only answer is C, not D. I don't think we need to worry about x.equal (null) for the exam. If the string code is null, the code.length will throw a null pointer exception, that is not the objective of this question.
Dennis Deems wrote:Sebanti, can you please tell us what edition of K&B you are using?
Which of the following will fulfill the equals() and hashCode() contracts for this class? (Choose all that apply.)
Answers are:
A. return ((SortOf)o).bal==this.bal;
B. return ((SortOf)o).code.length()==this.code.length();
C. return ((SortOf)o).code.length()*((SortOf)o).bal==this.code.length()*this.bal;
D. return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate==this.code.length()*this.bal*this.rate;
Correct Answers: C & D
EXPLANATION:
C and D are correct. The equals() algorithm must be at least as precise in defining what "meaningfully equivalent" means as the hashCode() method is.
A and B are incorrect because these equals() implementations would allow instances to be equal that hashCode() would not see as equal.
Final Fields Initialization
Final fields don't get default values, they have to be explicitly initialized.
A final variable can only be initialized once, either via an initializer or an assignment statement. If a final instance variable is not assigned a value - there will be a compiler error !
If not initialized at the point of declaration: this is called a 'blank final' variable.
A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared or an instance initializer block can be used.
Similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared.
Instance Init Blocks or constructors cannot assign values to final static variables - this will be a compiler error. Why ? An object of the class might never be created and the static final variable will be unintialized.
A final field has to be initialized in every constructor OR the compiler will complain.
Alternatively the final field can be assigned in an intializer block, however if the field is also being assigned in the constructor then the compiler complains.
Why ? Because the initializer runs before rest of the constructor body, so it will amount to reassigning the final variable which is not allowed.