permaculture playing cards
The moose likes Beginning Java and the fly likes debugging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "debugging" Watch "debugging" New topic


A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
I'm just wondering if when you do this:
int debug = 1;
and then somewhere down in your program you do:
if(debug == 1)
if you happen to want to set debug to 0 as in
int debug = 0;
and you compile your program, are the if statements automatically ignored by the compiler, or is the program going to check every time to see if debug == 1 or not and slow my app down?
P.S. Plz don't welcome me to the forums... i've been snooping around a while before registering, and i personally consider it lame. thanks for the consideration (and yes I know you weren't going to...)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Hi A.,
Welco... Uh, sorry. Don't want to be lame :roll:
If a constant boolean condition can be evaluated at compile time, Java compilers are indeed allowed to optimized out a branch to a block of unreachable code. That said, I'm not sure they do enough induction to optimize out something based on integer comparison to a constant. Jikes and javac definitely will optimize out tests of boolean constants, so if you use the more idiomatic

Then the test will definitely be optimized away.
You can answer this question for yourself, along with many related ones, using the "javap" tool that comes with the J2SDK. "javap -c MyClass" will disassemble MyClass to bytecode, and it's easy enough to check this kind of thing out.

[Jess in Action][AskingGoodQuestions]
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
Cool. thanks! yes it looks like i'll have to fix my if statements with booleans.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32317

It's not just an int vs boolean thing. In your example, the debug flag is not final. The compiler may not be able to guarantee that the flag does not change between assignment and the if statement.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302


GenRocket - Experts at Building Test Data
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

i personally consider it lame.

Which part do you consider lame? The welcome part, or just our entire site?

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57

Which part do you consider lame? The welcome part, or just our entire site?

the welcome part. what made you think i meant "our entire site?"
since I was talking about being welcomed, the "it" refers back to the subject although my punctuation may have been the cause for your confusion. The previous post makes me think I hurt someone's feelings and I hope this clears up any misunderstandings. ->
Welco... Uh, sorry. Don't want to be lame

He got it
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: debugging
jQuery in Action, 3rd edition