wood burning stoves*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "debugging" Watch "debugging" New topic
Author

debugging

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)
System.out.println(mydebuginfo);
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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.


[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: 15299
    
    6

WELCOME TO JAVARANCH A. WOLF!!!


GenRocket - Experts at Building Test Data
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

i personally consider it lame.

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


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
WELCOME TO JAVARANCH A. WOLF!!!

NOOHOOHOHHOHOOO
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. ->
Sorry.
Welco... Uh, sorry. Don't want to be lame

He got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: debugging
 
Similar Threads
Array
AOOB exception in my CODE-PLEASE HELP!
Switch case doubt
multiplication
won't let me input integer values