Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 27th, 2007, 11:48 PM   #11
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 889
Rep Power: 4 lectricpharaoh will become famous soon enough
Yes, you've got it, except Java does not interpret 01000000 as being true. You need an explicit comparison when testing any value that is not boolean. Otherwise, you get a compiler error:
int flag = 0;
if(flag != 0)
  System.println("flag is nonzero");
if(flag)  // error
  System.println("flag is nonzero");
The second if() is a problem because it evaluates to this:
if(flag == true)  // error
  System.println("flag is nonzero");
This is comparing an int and a boolean, so you have a type mismatch, and the compiler will choke. But on the boolean operations stuff, it seems you're clear.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Jul 28th, 2007, 4:36 AM   #12
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Kinda confusing. In C++ any non-zero integer means true, and zero means false. I think I did read in one of my books about Java that in Java the zero does not mean false and any non-zero integer does not necessarily mean true. I thought the if statement always tests for truth. So if I did this in Java:

int x = 1;

if (x)
   System.out.print("Welcome home.");

else
   System.out.print("Go away.");

That would be wrong? I would have to say:

int x = 1;

if (x == 1)
   System.out.print("Welcome home.");

else
   System.out.print("Go away.");

I'm going to have to play around with this stuff.
357mag is offline   Reply With Quote
Old Jul 28th, 2007, 5:22 AM   #13
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
So just by itself, the result 1000 0000 doesn't necessarily mean true to Java. You have to go one step further and include a comparison operator in your if statement like this:

if (result != 0)
   System.out.print("This line will execute.");

else
   System.out.print("You won't see this.");

The end result of explicitly adding the != operator to your if statement is that Java will actually compare the result(1000 0000) to 0(!= 0) and then the end result of that will in fact be either a true or a false, meaning the outcome will be in fact a boolean value. Then the compiler will go ahead and execute the appropriate line. God I hope I got this correct.
357mag is offline   Reply With Quote
Old Jul 28th, 2007, 7:32 AM   #14
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 889
Rep Power: 4 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by 357mag
So just by itself, the result 1000 0000 doesn't necessarily mean true to Java. You have to go one step further and include a comparison operator in your if statement like this:
Exactly. Without it, I believe Java will consider the code to be a compile-time error. Why not try and see?
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Attitudes Oddball Coder's Corner Lounge 29 Mar 18th, 2006 9:34 PM
C++ template and namespace question Soulstorm C++ 3 Jan 22nd, 2006 2:46 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 11:53 AM
How to post a question nnxion C++ 0 Jun 3rd, 2005 8:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 8:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC