my thread of basic Java questions

D

denim_genes

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.
 
M

Mark Space

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.

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.
 
D

denim_genes

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.

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

int dinosaur = 0;

dinosaur == true ?
 
M

Mark Space

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

int dinosaur = 0;

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)
 
D

denim_genes

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)

I know the language, hence why I was confused as to this "reliable"
answer key.
 
A

Arved Sandstrom

Mark Space said:
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.:
[ 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
 
C

Chase Preuninger

don't you need a boolean or something that can be converted to a
boolean by the compiler for that to work?
 
A

Arne Vajhøj

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.

What is ok declared as ?

Arne
 
A

Arne Vajhøj

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

Arne
 
A

Arne Vajhøj

Lew said:
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.

Arne
 
M

Mark Space

Arne said:
Good point.

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.
 
A

Arne Vajhøj

Mark said:
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.

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

Arne
 
M

Mark Space

Arne said:
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.
 
D

denim_genes

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.
 
L

Lionel van den Berg

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.
 
M

Mark Space

Lionel said:
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.
 
A

Arne Vajhøj

Mark said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top