Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

my thread of basic Java questions

Thread view: 
denim_genes@hotmail.com - 13 Apr 2008 04:59 GMT
Why is the following true?:

(ok || (n1 < 0))

And what the heck is "ok"?

My guess: any String or character or whatever is "true" on its own,
since it exists. Thus, "ok" could be replaced by "dinosaur", and the
answer to the question would still be true.
Mark Space - 13 Apr 2008 05:18 GMT
> Why is the following true?:
>
[quoted text clipped - 5 lines]
> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
> answer to the question would still be true.

It isn't true, and strings in Java have double quotes around them.

ok is obviously a variable that's been declared elsewhere, just like n1.
denim_genes@hotmail.com - 13 Apr 2008 05:23 GMT
> denim_ge...@hotmail.com wrote:
> > Why is the following true?:
[quoted text clipped - 10 lines]
>
> ok is obviously a variable that's been declared elsewhere, just like n1.

It's true... I'm looking at the answer key right now...

int dinosaur = 0;

dinosaur == true ?
Mark Space - 13 Apr 2008 07:18 GMT
>> denim_ge...@hotmail.com wrote:
>>> Why is the following true?:
[quoted text clipped - 12 lines]
>
> dinosaur == true ?

It's false.  I don't care about your answer key.  Get a compiler and try
it, or better yet learn the language.

Your latest one here is wrong too, ints aren't even comparable to booleans.:

init:
deps-jar:
Compiling 1 source file to
C:\Users\Brenden\Dev\misc\FinalizeTest\build\classes
C:\Users\Brenden\Dev\misc\FinalizeTest\src\finalizetest\Main.java:31:
incomparable types: int and boolean
        if( dinosaur == true )
1 error
BUILD FAILED (total time: 2 seconds)
denim_genes@hotmail.com - 13 Apr 2008 08:04 GMT
> denim_ge...@hotmail.com wrote:
> >> denim_ge...@hotmail.com wrote:
[quoted text clipped - 28 lines]
> 1 error
> BUILD FAILED (total time: 2 seconds)

I know the language, hence why I was confused as to this "reliable"
answer key.
Arved Sandstrom - 13 Apr 2008 15:30 GMT
>>> denim_ge...@hotmail.com wrote:
>>>> Why is the following true?:
[quoted text clipped - 18 lines]
> Your latest one here is wrong too, ints aren't even comparable to
> booleans.:
[ SNIP ]

This question brings up the always popular programming trivia contest...what
are the truth values in various programming languages? Not always easy to
remember...

For example, in awk "0" is true because it's a non-null string value.
However, in Perl "0" is false (but "0.0" is true). Apparently in REBOL the
integer 0 is true.

Does the language in question (1) use two boolean values, or (2) does it
define truth values for all values of datatypes, or (3) does it define two
boolean values *and* also truth values for values of other datatypes?
Example of #3 being Javascript.

Then there's the convention in UNIX that a program is considered to have
succeeded if it sets an exit code of 0, which can catch the novice shell
script programmer by surprise...

I think there was an attempt to design a bool class in C++ quite some back
but they couldn't quite design one that was completely correct, hence the
bool datatype. Of course you can still initialize a bool with 0...

AHS
Roedy Green - 24 Apr 2008 10:04 GMT
On Sat, 12 Apr 2008 23:18:07 -0700, Mark Space
<markspace@sbc.global.net> wrote, quoted or indirectly quoted someone
who said :

>I don't care about your answer key.

Sounds like that Monty Python English phrase book for Bulgarians.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 24 Apr 2008 10:03 GMT
>dinosaur == true ?

That is equivalent to asking if 1 is true.  It is a meaningless
question in Java..  In C, any int != 0 is considered true.  In java an
int  has no meaning as a boolean.

You would have to say something like
int dinosaur;
...
 boolean isBaby = dinosaur < 1;

which might or might not be true.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Chase Preuninger - 13 Apr 2008 15:59 GMT
don't you need a boolean or something that can be converted to a
boolean by the compiler for that to work?
Arne Vajhøj - 13 Apr 2008 16:50 GMT
> Why is the following true?:
>
[quoted text clipped - 5 lines]
> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
> answer to the question would still be true.

What is ok declared as ?

Arne
Arne Vajhøj - 13 Apr 2008 16:51 GMT
>> Why is the following true?:
>>
[quoted text clipped - 7 lines]
>
> What is ok declared as ?

And what is its value ?

Arne
denim_genes@hotmail.com - 13 Apr 2008 17:50 GMT
> > denim_ge...@hotmail.com wrote:
> >> Why is the following true?:
[quoted text clipped - 12 lines]
>
> Arne

boolean ok = true;
int n1 = 100;
int n2 = 0;

They forgot to put that in the question. I guess it makes sense now.
Arne Vajhøj - 13 Apr 2008 17:54 GMT
>>> denim_ge...@hotmail.com wrote:
>>>> Why is the following true?:
[quoted text clipped - 11 lines]
>
> They forgot to put that in the question. I guess it makes sense now.

Yes.

(ok || (n1 < 0)) = (true || (100 > 0)) = (true || false) = true

Arne
Lew - 13 Apr 2008 18:00 GMT
denim_genes@hotmail.com wrote:
>> Why is the following true?:
>> (ok || (n1 < 0))

>> boolean ok = true;
>> int n1 = 100;
>> int n2 = 0;
>>
>> They forgot to put that in the question. I guess it makes sense now.

> Yes.
>
> (ok || (n1 < 0)) = (true || (100 < 0)) = (true || false) = true

Nitpick: Since || is an early-out operator, the (100 < 0) clause is never
evaluated once 'ok' is found to be true.

 (ok || (n1 < 0)) == (true || <unevaluated>) == true

Signature

Lew

Arne Vajhøj - 13 Apr 2008 18:04 GMT
> denim_genes@hotmail.com wrote:
>>> Why is the following true?:
[quoted text clipped - 14 lines]
>
>  (ok || (n1 < 0)) == (true || <unevaluated>) == true

Good point.

If it is one of those Java language test, then that could be
considered important.

Arne
Mark Space - 13 Apr 2008 18:57 GMT
>> denim_genes@hotmail.com wrote:
>>>> Why is the following true?:
[quoted text clipped - 19 lines]
> If it is one of those Java language test, then that could be
> considered important.

Yup, if( ok || (n1++ < 0) ){}, what is the value of n1 after this
statement?

Still 100 because the ++ never got evaluated.

I'd like to see a link to this "answer key" the OP is referring too.  If
it's really Java and not some other language, I don't think it's a very
good one.  But I'd like to check it out myself.
Arne Vajhøj - 13 Apr 2008 19:34 GMT
>>> denim_genes@hotmail.com wrote:
>>>>> Why is the following true?:
[quoted text clipped - 28 lines]
> it's really Java and not some other language, I don't think it's a very
> good one.  But I'd like to check it out myself.

That type of code is not good code, but is exactly what some tests
like to check.

Arne
Mark Space - 13 Apr 2008 21:19 GMT
> That type of code is not good code, but is exactly what some tests
> like to check.

Yup, exactly what I was thinking.  Not a good pattern, but something
that you would expect to see on a test.
denim_genes@hotmail.com - 14 Apr 2008 04:54 GMT
> > That type of code is not good code, but is exactly what some tests
> > like to check.
>
> Yup, exactly what I was thinking.  Not a good pattern, but something
> that you would expect to see on a test.

This is an introductory programming course, hence the poor quality of
questions (if that's any excuse).

I found myself handing in assignments with increased efficiency (not
having variables I didn't need) and error-checking when they weren't
even on the grading criteria. Really a basic course.

Which is why I'm pissed... I totally didn't study anything related to
making classes (no time), so I may have lost marks on that part of the
exam I just wrote. I assumed I would handle their basic questions, but
they did ask some rather weird ones.

Anyway, thnx.
Lew - 13 Apr 2008 20:28 GMT
Lew wrote:
>> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
>> never evaluated once 'ok' is found to be true.
>>
>>  (ok || (n1 < 0)) == (true || <unevaluated>) == true

> Good point.
>
> If it is one of those Java language test, then that could be
> considered important.

It could also signify if the expressions under evaluation have side effects.

if ( isSecurityFine() || destroyAllFiles() )

vs.

if ( isSecurityFine() | destroyAllFiles() )

Either one will ensure that spies cannot read the contents.  Only one will
give you the opportunity to do so absent a breach.

Signature

Lew

Lionel van den Berg - 14 Apr 2008 08:03 GMT
> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
> never evaluated once 'ok' is found to be true.
>
>   (ok || (n1 < 0)) == (true || <unevaluated>) == true

Partly related I often use:

if (object != null && object.hasSomeCondition) {
   executeFooBaa();
}

I rely on the fact that the second part of the condition never gets
executed  if object == null. This seems a little sloppy on my behalf but
it looks nicer than:

if (object != null) {
   if (object.hasSomeCondition) {
       executeFooBaa();
   }
}

What does everyone else do?

Lionel.
Lew - 14 Apr 2008 12:27 GMT
>> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
>> never evaluated once 'ok' is found to be true.
[quoted text clipped - 9 lines]
> I rely on the fact that the second part of the condition never gets
> executed  if object == null. This seems a little sloppy on my behalf

How in the world do you rate something with such economy of expression yet
with no sacrifice in clarity "sloppy"?  Especially when it's such a universal
idiom?

Signature

Lew

Mark Space - 14 Apr 2008 17:24 GMT
> if (object != null && object.hasSomeCondition) {
>     executeFooBaa();
> }

When Arne called my code "not good," I think he was referring to the use
of side effects and the post increment ++ operator in a comparison.  Or
at least I assumed he was.

Short circuit operators like above are fine.  Side effects can be
questionable.  ++ and -- are both especially subject to abuse.
Arne Vajhøj - 15 Apr 2008 01:47 GMT
>> if (object != null && object.hasSomeCondition) {
>>     executeFooBaa();
[quoted text clipped - 6 lines]
> Short circuit operators like above are fine.  Side effects can be
> questionable.  ++ and -- are both especially subject to abuse.

I use that construct myself.

Extremely strictly speaking then crashing versus not crashing could
be considered a side effect.

But I don't think this is a real problem.

The ++ and -- could be a problem, but even those are
not the worst.

It is:

if(something || sometmethodthatchangestate())

that really make it hard to follow the logic.

Arne
Roedy Green - 24 Apr 2008 10:07 GMT
>boolean ok = true;

Why the expression  ok || n1 <0 is true regardless of the value of n1
is a property of || and |.

See http://mindprod.com/jgloss/mccarthyoroperator.html

http://mindprod.com/jgloss/oroperator.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 24 Apr 2008 10:00 GMT
>Why is the following true?:
>
>(ok || (n1 < 0))

it isn't. It is true if and only if the boolean variable ok is true or
if n1 < 0.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.