N Sam

Ranch Hand
+ Follow
since Nov 23, 2013
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 N Sam

This question is from the Esteban Herrera - Java Practice Questions book.

The pertaining code is given below. The answer given for question 6.15 states .... "m1 receives a primitive value, so the object is unboxed.....".

The above explanation in the answer seems to imply "only because m1 parameter is primitive type, an unboxing is done, and the print out of above code is false". But, i tried running the same code with one modification: the parameter to m1 is the object Boolean b. Now, the argument passed from main becomes an object reference, because of public void m1(Boolean b). Why is this not changing the value of variable b (in main) to true ? (I ran the code and the result is the print out 'false'). Why is the object reference passing not giving the changed result, in this case ?

@Estaban - Anyways, it seems from the above that the answer to this question should not imply ".....m1 receives a primitive value and thus.....", because the result is same in both cases.

@moderator - please don't mark this thread resolved. I will mark it once i finish the book.
I was going through the a thread here that mentions "private classes are non-existant in java" and why it is so. (This is the thread). But, when i was reading the book "Effective Java by Joshua Bloch" there is a sentence on page 34 that starts with "the class is private or package-private....". How come this author is referring to a private class, while the above thread is discuss how private class is meaningless ? The only explanation that i can think of is that the author is referring to private class in a nested inner class (which seems legal). Just want to double check my understanding with experts.

7 years ago
Thanks Campbell, for the pointers. The code piece was from stackoverflow.com. From what i have read, the correct override code would be ....

Also, the rule "always override hashcode, when equals is overridden" is on my mind.
Did i get it right ?
7 years ago
Campbell - i simplified the expression a bit for the purpose of focusing on my question. I searched for errata on the book, but there is none to be found on internet. The expression occurs in a question, and the right answer is to pick the answer "Compilation fails". The expression on page 45 is

The point made by you and Winston and Heena was a good one that i will not forget during my own OCA exam :-) Thanks.
7 years ago
Thanks Heena, your example brought out the point clearly.

Campbell - I am not sure which predicate you were referring to as 'original predicate' because soon after the OP, the expression got changed for rest of thread. If you are referring to

then it was clear to me what this does, so i didn't present it. This will perform a bitwise AND on count and (count+1). If you were referring to the predicate

then, this case is also was clear to me. Due to operator precedence the above would be equivalent to

In this case, if count is odd integer, then the right hand side operand evaluates to 'true'.
If count is an even integer, then the right hand side operand would evaluate to 'false'. In both cases, we have a integer as left side operand and a boolean as right side operand.
So, the compiler gives an error.

In all cases a bitwise AND is performed. I wasn't aware of the fact that & evaluates both operands (no matter what); this became clear after Heena's example.
Thanks all, for bringing out this point. This would make a perfect question on the OCA exam.

7 years ago
Thanks Campbell for your reply. Henry - if you change name to "not fred" then you won't see the print outs; So i don't see your point, henry.
I don't understand winston's point in giving this code piece because the evaluation result is same using & as well as &&. Now it is clear to me that & is bitwise operator and is overloaded for boolean operands. Winston's point of 'how they evaluate' can be understood only if i step into java source code, which is too much for my level.
7 years ago
OK - the parameter should match for this to override the "Object class equals()". So the correct code would be....
7 years ago
In overriding, the signature has to be the same between the 2 methods (including parameter types and position). The signature in Object class is
public boolean equals(Object obj)
The code i supplied has equals() different only in the parameter Foo f. But i thought "Foo" is an "Object" too, so it is equivalent to above signature described in the Object class.
7 years ago
I tested both cases mentioned above with the following code piece and the result was as expected in the print. Both expressions should result in a true or false situation ?

Campbell - I checked operator precedence, and confirmed that is not the problem. (as mention in message above).
7 years ago
I read the following comment at stackoverflow.com. It is not clear to me why equals in the code below does not override - i looked up Object class equals() and the signature is same.

public class Foo {
private String id;
public boolean equals(Foo f) { return id.equals(f.id);}
This class compiles as written, but adding the @Override tag to the equals method will cause a compilation error as it does not override the equals method on Object.
7 years ago
Under operator precedence, i see == has higher precedence than bitwise & operator. So, it would bind like
if ( count & ((count%2) == 0 ) ) ...
I also think the wording is wrong in the book in saying "only booleans have to be operands". I would like to understand how the bitwise & operator works given boolean operands. For an if condition, the logical AND would be &&, so how come the bitwise & operator works in place of &&. Compiler accepts a condition like if (true & false).....
7 years ago
Just out of curiosity then.... Isn't there any way in Java to print out the memory location of a reference variable ? Perhaps some method somewhere, that can be used, maybe ?
7 years ago
Two intentions in my mind. First, i want to understand how to print memory locations of reference types, in JAVA. This has been an invaluable debugging tool for me in C. So knowing how to do this would be useful for me in future.
Second reason is that i want to see what a statement like StringBuilder sb3 = sb2 really does (is it shallow copy of the reference value or a deep copy of the string itself). That is the reason for my test program.
7 years ago
I know an if condition has to evaluate to a boolean. But the exact line used by the above mentioned book is ....

So, first i need to understand the operator and expected operands. Netbeans IDE gives error for the above statement (bad operand types for &). If both operands are boolean, then Netbeans accepts and does not give an error. If this is a bitwise & operator (like in C), having booleans does not make any sense to me.

(Note to "Bear" : I edited my original post, after you replied. please re-read if you are still in this thread).

7 years ago
I have conflicting info on this operator. The book by Esteban Herrera (Java practice questions - OCA) says that the left and right operands for & operator must be boolean (page 189, "the & operator only works with booleans........"). Netbeans IDE agrees and gives an error if i type in a int as one operand (INSIDE an if condition). But the following line compiles fine :

Also, tutorialspoint says & operator is "binary AND" and has an example (which i don't understand). I am familiar with bitwise AND operator in C language, which is denoted by &. Does JAVA give the same bitwise AND meaning to this operator ? Do all the other bitwise operators of C (like ~) have the same meaning in Java too ? Please elaborate (you can use the same example given in tutorialspoint. ) Thanks.

7 years ago