Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java interpretation help

2 views
Skip to first unread message

mike

unread,
Dec 28, 2009, 1:44:02 PM12/28/09
to
Hi,

I have some code snippets that I need help to understand:

1.
public static final int VIEW_PRIVATE = 1 << 103;

What is VIEW_PRIVATE equal to?

2.

boolean newHasRemote = newState.isElement();
changed |= hasNewRemote != this.hasRemote();
setFlag(HAS_REMOTE, newHasRemote);

How do I interpret the second line?

3.

private static final int HAS_REMOTE = 0x1;

What does 0x1 mean is it an int?

4.

Here is how setFlag is called:

setFlag(HAS_REMOTE, newHasRemote);

HAS_REMOTE is int
newHasRemote is boolean.

void setFlag(int flag, boolean value) {
if (value)
flags |= flag;
else
flags &= ~flag;
}

What happens in the method?

br,

//mike


Eric Sosman

unread,
Dec 28, 2009, 2:45:56 PM12/28/09
to
On 12/28/2009 1:44 PM, mike wrote:
> Hi,
>
> I have some code snippets that I need help to understand:

Homework? Some of the questions look that way; others
don't seem so blatant. I'll give you the benefit of the
doubt, but I'll be brief.

> 1.
> public static final int VIEW_PRIVATE = 1<< 103;
>
> What is VIEW_PRIVATE equal to?

128. (Why not try it for yourself?)

> 2.
>
> boolean newHasRemote = newState.isElement();
> changed |= hasNewRemote != this.hasRemote();
> setFlag(HAS_REMOTE, newHasRemote);
>
> How do I interpret the second line?

It's a compound assignment, where the new value assigned
to `changed' is the logical OR of the old value and the right-
hand side. That RHS is a not-equals comparison, true if the
two operands are unequal, false if they are equal.

> 3.
>
> private static final int HAS_REMOTE = 0x1;
>
> What does 0x1 mean is it an int?

1, yes.

> 4.
>
> Here is how setFlag is called:
>
>
>
> setFlag(HAS_REMOTE, newHasRemote);
>
> HAS_REMOTE is int
> newHasRemote is boolean.
>
> void setFlag(int flag, boolean value) {
> if (value)
> flags |= flag;
> else

> flags&= ~flag;


> }
>
> What happens in the method?

The value of `flags' is (potentially) changed. Assuming
the same `HAS_REMOTE' value shown earlier, and assuming that
`flags' is an `int' (or a `long'), the outcome when the method
finishes is that the low-order bit of `flags' is 1 if `value'
is true, or 0 if it is false. More generally, each bit of
`flags' corresponding to a 1-bit in `HAS_REMOTE' is either
set or cleared depending on whether `value' is true or false.

--
Eric Sosman
eso...@ieee-dot-org.invalid

Roedy Green

unread,
Dec 28, 2009, 3:41:33 PM12/28/09
to
On Mon, 28 Dec 2009 10:44:02 -0800 (PST), mike
<mikaelp...@hotmail.com> wrote, quoted or indirectly quoted
someone who said :

>1.
>public static final int VIEW_PRIVATE = 1 << 103;
>
>What is VIEW_PRIVATE equal to?

see http://mindprod.com/jgloss/jgloss/shift.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.

Roedy Green

unread,
Dec 28, 2009, 3:44:17 PM12/28/09
to
On Mon, 28 Dec 2009 10:44:02 -0800 (PST), mike
<mikaelp...@hotmail.com> wrote, quoted or indirectly quoted
someone who said :

>2.


>
>boolean newHasRemote = newState.isElement();
>changed |= hasNewRemote != this.hasRemote();
>setFlag(HAS_REMOTE, newHasRemote);
>
>How do I interpret the second line?

http://mindprod.com/jgloss/precedence.html
http://mindprod.com/jgloss/homework.html


a |= b; is shorthand for a = a | b;

Roedy Green

unread,
Dec 28, 2009, 4:25:25 PM12/28/09
to
On Mon, 28 Dec 2009 10:44:02 -0800 (PST), mike
<mikaelp...@hotmail.com> wrote, quoted or indirectly quoted
someone who said :

>


>private static final int HAS_REMOTE = 0x1;
>
>What does 0x1 mean is it an int?

You need an introductory text book for these sorts of questions.
See http://mindprod.com/jgloss/literal.html
http://mindprod.com/jgloss/gettingstarted.html
http://mindprod.com/jgloss/homework.html

Roedy Green

unread,
Dec 28, 2009, 4:26:07 PM12/28/09
to
On Mon, 28 Dec 2009 10:44:02 -0800 (PST), mike
<mikaelp...@hotmail.com> wrote, quoted or indirectly quoted
someone who said :

>setFlag(HAS_REMOTE, newHasRemote);


>
>HAS_REMOTE is int
>newHasRemote is boolean.
>
>void setFlag(int flag, boolean value) {
> if (value)
> flags |= flag;
> else
> flags &= ~flag;
>}
>
>What happens in the method?

see http://mindprod.com/jgloss/boolean.html

Lew

unread,
Dec 28, 2009, 8:58:27 PM12/28/09
to
mike wrote:
> boolean newHasRemote = newState.isElement();
> changed |= hasNewRemote != this.hasRemote();
> setFlag(HAS_REMOTE, newHasRemote);
>
> How do I interpret the second line?

As a compiler error, unless there's a declaration for 'hasNewRemote' that you
aren't showing us.

Why don't you a) read your textbook, b) read your class notes, and/or c) read
the Java tutorials?
<http://java.sun.com/developer/onlineTraining/>

--
Lew

mike

unread,
Dec 29, 2009, 12:35:03 PM12/29/09
to

Hi,

Thanks for all the answers.

I find it amusing to hear that you think I ask questions for some kind
of class I am taking.
I have been working a lot with shell script and perl and some with
java. I have also been taking classes in java but the weight has not
been on these things I ask about. I have seen a lot of java code but
not found code like this.

So I thought I would be good to post a question in a forum like
comp.lang.java.help, where more experienced nice people in the area
could give me a quick answer on ( with kids and travel we all have
limited time) my questions. When I have the short answer I am quite
convinced, since I have some education ( believe it or not),that I can
dig deeper into the basics.
And I am interested in learning but with limted time and my goal was
to understand flow in the code.

Thanks!

//mike

Lew

unread,
Dec 29, 2009, 2:03:51 PM12/29/09
to
mike wrote:
> And I am interested in learning but with limted time and my goal was
> to understand flow in the code.

Exactly why I recommended the tutorials.

<http://java.sun.com/docs/books/tutorial/java/nutsandbolts/
operators.html>
explains the operators | & ~ != << and other respondents have
explained |= and similar constructs. If you want the ultimate
authority on these, read the Java Language Specification (JLS) and
more explanation is available at the various mindprod.com links Roedy
provided.

<http://java.sun.com/docs/books/jls/third_edition/html/
expressions.html#15.15>
and following sections.

IBM Developerworks and javapassion.com are also excellent sites for
Java information.

Really, you have to make some effort to learn at least the basics
yourself. You aren't going to learn the language from scratch just
from the newsgroup. The tutorials and other explanatory material at
java.sun.com are the best starting place, and GIYF, too. You'll get
more complete information much more quickly and comprehensively there
than here.

If your time is as limited as you claim, then you will want to take
the advice offered by everyone who's responded to your post.

--
Lew

Lew

unread,
Dec 29, 2009, 2:18:25 PM12/29/09
to
On Dec 28, 1:44 pm, mike <mikaelpetter...@hotmail.com> wrote:
> I have some code snippets that I need help to understand:
>
> 1.
> public static final int VIEW_PRIVATE = 1 << 103;
>
> What is VIEW_PRIVATE equal to?

1 (one) left shifted by (103 mod 32) bits.

> 2.
>
> boolean newHasRemote = newState.isElement();
> changed |= hasNewRemote != this.hasRemote();
> setFlag(HAS_REMOTE, newHasRemote);
>
> How do I interpret the second line?

As stated earlier, likely as a compiler error. Assuming appropriate
declarations for those that you don't show for 'changed', 'hasRemote
()', 'newState', 'newState.isElement()', 'hasNewRemote', etc., then
you interpret that line as "assign to 'changed' the logical OR of the
current value of 'changed' with the result of the inequality
comparison of 'hasNewRemote' with 'this.hasRemote()'." (The use of
'this' with the 'hasRemote()' call is utterly redundant.)

> 3.
>
> private static final int HAS_REMOTE = 0x1;
>
> What does 0x1 mean is it an int?

'int' is one of the most fundamental types, arguably the most
fundamental type in Java, and its literals are among the most basic
things to know about the language. How much have you studied Java so
far? The tutorials and various mindprod.com articles and elsewhere
explain these basics quite thoroughly. Yes, '0x1' is an 'int'
literal. See the JLS:
<http://java.sun.com/docs/books/jls/third_edition/html/
lexical.html#3.10.1>

>
> 4.
>
> Here is how setFlag is called:
>
> setFlag(HAS_REMOTE, newHasRemote);
>
> HAS_REMOTE is int
> newHasRemote is boolean.
>
> void setFlag(int flag, boolean value) {
>    if (value)
>       flags |= flag;
>    else
>       flags &= ~flag;
>
> }
>
> What happens in the method?

The passed-in value of 'value' is used to decide which of the two
assignments to execute. Assuming the appropriate definition for
'flags' that you don't show, the first assignment assigns to 'flags'
the bitwise OR of its current value with the value of 'flag'; the
second one assigns the bitwise AND of the current value of 'flags'
with the ones complement of the value of 'flag'.

All of this is explained in introductory material on the Java
language.

--
Lew

0 new messages