Melissa Nikolic

Greenhorn
+ Follow
since Jan 17, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Melissa Nikolic

Winston Gutkowski wrote:

Campbell Ritchie wrote:I see they are calling it complement. Still probably in accurate. Negation is better.


Personally, I like just calling it "not", especially for a boolean. 'Compliment' is somewhat vague, and in the context of '~', it's actually a 1s-compliment op.

Winston



In reference to the documentation, "The type of the operand expression of the unary ! operator must be boolean, or a compile-time error occurs. The type of the unary logical complement expression is boolean. At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true."

In this context "complement" means the "boolean logical operator negation" or '!' or "not"? So, what does it mean that "the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true". I was thinking that the complement of true is false and vice versa (hence my original confusion with the stated example).

Thank you,

M
12 years ago

Greg Brannon wrote:Booleans can get our brains twisted the more we think about them.

2.5 >= 8 is false. 2.5 is clearly NOT greater than or equal to 8.
!( false ) = true. obvious?

b2 = b1 || true
b1 = false. a given
b2 = false || true. If either this OR that is true, the result is true.
b2 = true.



Thanks for your reply. I do get your explanation but I was confused by the documentation which said, " At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true". So, I thought the complement of the true value would be false (following my logic?).

I understand the example (re: why b2 is true) but what is the documentation referring to from the above quote?

Thanks again.

M
12 years ago
Hi,

Some feedback regarding my misunderstanding is appreciated.

1) The following is what the Java docs says about boolean logical negation ! operator:

"The type of the operand expression of the unary ! operator must be boolean, or a compile-time error occurs. The type of the unary logical complement expression is boolean. At run time, the value of the unary logical complement expression is true if the operand value is false and false if the operand value is true."
15.15.6 Logical Complement Operator !
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html

2) Example (answers given from my book):

boolean b1, b2, b3 = false, b4 = false;

b1 = (4==2) & (1<4) // false
b2 = b1 | !(2.5>=8) //true

3) Question:

Why is b2 true? My understanding is that the second operand has the value of true, because the truth value for a Boolean OR is "true if either or both operands are true, otherwise false" and !(2.5>=8) is true (unless it's not true and this is where my problem is). Therefore, since one of the operands is true then the complement expression should be false (unless I don't understand the idea of complement expressions), and b2 = b1 | !(2.5>=8) should be false.

Thank you for your assistance.

Regards,

M
12 years ago

Jeff Verdegan wrote:

Melissa Nikolic wrote:
Thank you for your clear explanation. Frankly, telling a newbie to go to the Java documentation is like telling an introductory biology student to go find the answer to a question in a peer reviewed journal. It seems a bit unhelpful. But since I am used to peer reviewed journals I should be able to muck through the documentation.

Regards,

M



Well, it's just two different approaches. There's a continuum of how much direct help can be offered vs. just pointing someone in the right direction and letting them try to figure it out on their own, and, other than not handing over full code solutions, there are as many different opinions on what kind of help should be given as there are people giving help. For beginners, I usually just provide the JLS as additional information, since a lot of it can be difficult reading, even for those with more experience. I guess Campbell felt it was better for you to read the relevant section of the JLS first, and then ask more specific questions if they arise from there. Either way, we're all just trying to help you learn.



Yes, I can see the merit in your argument. I did go to the documentation suggested and I have another question. In "Narrowing Primitive Conversions" http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363 the documentation states the following:

The following 23 specific conversions on primitive types are called the narrowing primitive conversions:
byte to char
short to byte or char
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float

Everything makes sense to me but the narrowing of a byte to a char. Why would a narrowing conversion take place from a byte (8 bits) to a char (16 bits)? And why would you narrow the conversion from a byte to a char but not from a byte to a short (which has the same bit width as the char)?

Thanks for your help.

M

12 years ago

Jeff Verdegan wrote:

Melissa Nikolic wrote:Hi again,

I wanted to confirm a particular conversion example and its meaning.

If I initialize the following:

short h = 40;

My Java book says that this is ok because int is converted to a short.

while...

h = h + 2; is not okay because "cannot assign an int to short"

Is this an error because the first example is explicitly assigned to a short while the second example is not explicitly assigned and therefore cannot be promoted implicitly?



Case 1: 40 is an int. Since it is a compile-time constant that fits into a short, a narrowing conversion is automatically applied and the value (short)40 is stored in h. No promotion occurs.

Case 2: h + 2 is short + int, so the value of h on the RHS is promoted to an int, and the result of h + 2 is an int. Since h + 2 is not a compile-time constant, no automatic narrowing is done, and it's an error because we're trying to stick an int into a container meant to hold only a short.



Thank you for your clear explanation. Frankly, telling a newbie to go to the Java documentation is like telling an introductory biology student to go find the answer to a question in a peer reviewed journal. It seems a bit unhelpful. But since I am used to peer reviewed journals I should be able to muck through the documentation.

Regards,

M
12 years ago

Campbell Ritchie wrote:I have already told you the Java Language Specification is difficult to understand, but that is where you will find the details.



Thank you for the information. I will take heed and look in the Java documentation before posting. I will also ensure to cite the relevant reference therein to further highlight my question (or perhaps at that point questions, as you have mentioned it is difficult to understand).

Ultimately, being able to answer all of my own questions is preferable.

Regards,

M
12 years ago
Hi again,

I wanted to confirm a particular conversion example and its meaning.

If I initialize the following:

short h = 40;

My Java book says that this is ok because int is converted to a short.

while...

h = h + 2; is not okay because "cannot assign an int to short"

Is this an error because the first example is explicitly assigned to a short while the second example is not explicitly assigned and therefore cannot be promoted implicitly?

Thank you for your assistance.

M
12 years ago

Campbell Ritchie wrote:What does your book say the positive operator does? Then check that against the Java Language Specification (JLS). The JLS is not easy to understand, however.



My book says that the positive operator is a unary operator. I was confused because there is another schematic that shows addition and subtraction as unary operators and subsequently as binary operators. But it looks like I'll just stick to positive and negative operators as unary operators and addition and subtraction as binary operators.

Thank you,

M
12 years ago

Jeff Verdegan wrote:

Melissa Nikolic wrote:Hello,

I wanted to first thank the moderators for a place where newbies can come and ask questions without being berated or chastised (sadly I have seen this on numerous sites).



To be fair, people are not usually chastised simply for asking newbie questions. They will be spanked a bit on some sites for failing to do any research on their own before posting, for asking others to do their work for them, or for copping an attitude of entitlement. This place just goes out of its way to be explicitly friendly more than most sites, while other sites tend to be more down to business without any frills.



I suppose my experience has been different (mind you this is when I was looking for answers and came across these various confrontations on different sites). I think the point is that this site is friendly, whether one has to go out of one's way is a different thing entirely. This is my impression but if no frills means lack of civility I'll pass.

M
12 years ago

Joanne Neal wrote:

Melissa Nikolic wrote:.unary arithmatic operators (addition + and subtraction -)


The unary arithmetic operators are ++ and --. They increment or decrement the variable by 1.
They are unary because they only require one operand e.g. i++, --x



Thank you. Yes, the unary operators also include the negative (e.g. -1) operator and positive (e.g. +1) operator in addition to those that you mentioned. My question was whether the addition and subtraction operators were also unary (as my books says, although as I mentioned I thought they were binary).

It seems that my book may be mistaken and the arithmetic addition (+) and subtraction (-) operators are binary, not unary.

M
12 years ago

Bear Bibeault wrote:Addition and subtraction are binary operators.



Thank you. Yes, this is what I thought. However, the Java Certification book I am reading also says they are both unary and binary. Hence my question.

M
12 years ago
Hello,

I wanted to first thank the moderators for a place where newbies can come and ask questions without being berated or chastised (sadly I have seen this on numerous sites).

So, my question is regarding unary operators. Specifically, can someone please clarify the difference between unary arithmatic operators (addition + and subtraction -) and negative (e.g. -1) and positive (+1) unary operators? I didn't think that addition and subtraction were unary operators; I thought they were binary operators.

Any clarification would be greatly appreciated.

Regards,

M
12 years ago