Bitwise & Operator and This Boolean Method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Bitwise & Operator and This Boolean Method

  1. #1
    Join Date
    Feb 2006
    Posts
    2

    Bitwise & Operator and This Boolean Method

    Hi: Could someone tell me whats going on in this method? what is that bitwise '&' operator doing in a if conditional statement? does this method always return 'False'/'True'? Where is the 'Else' Statement? What Happens when the if statemet becomes False, would the rest of the code be processed? Wouldn't return be the last line of the code? Sorry I'm a java newbie.




    Code:
      private boolean hasWLMData(){
    	String curLine;  //current line to read
    
    	while (!in.eofIsAvailable()){                           //while not at the end of the file
    	  curLine = in.readLine();                              //read the first word of the current line
    	  if(curLine !="" && curLine != null & curLine.substring(0,3).equals("WLM")){             //first 3 chars of the word are "WLM"?
    		  in.close();                                       //close input file
    		  in = new TextInput(filePath);                     //reset the in so our read head is at the start of file.
    		  return true;                                      //yes, we have found WLM information in the nmon report
    	  }
    	}
    	in.close();                                            //close the input file
    	in = new TextInput(filePath);                          //open the input file again (restarts read head after scanning through)
    	return false;
      }

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    The way you are using it, it is not a bitwise operator. It is a conditional one. It means AND. There are two ways to say AND in Java, either && or &. && does something called short-circuit evaluation. This means that if the first one is false, it will break imidiately. eg:
    Code:
    String s;
    int len;
    if (s != null && s.length() > 0) {
        len = s.length();
    }
    What happens here, is if s is null, it imediately knows this condition is false. This code will always work. This, however, will not:
    Code:
    String s;
    int len;
    if (s != null & s.length() > 0) {
        len = s.length();
    }
    Here, if s is null, it will still evaluate the second part of the condition and a NullPointerException will be thrown.

    The bitwise operator & is used for computation with ints.

    Code:
      if(curLine !="" && curLine != null & curLine.substring(0,3).equals("WLM")){             //first 3 chars are WLM?
    This should be simplified to:
    Code:
    if (curLine != null && curLine.startsWith("WLM")) {
    Note that I used && rather than &, and I got rid of curLine != "". That's a silly comparison; it will never be true. It will check if the memory of the String "" that was just created has the same as curLine.

    Hope this helps.
    Last edited by destin; 02-09-2006 at 05:06 PM.

  3. #3
    Join Date
    Feb 2006
    Posts
    2
    wow. you are awesome man. thanks a bunch

  4. #4
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Quote Originally Posted by destin
    This should be simplified to:
    Code:
    if (curLine != null && curLine.startsWith("WLM")) {
    Note that I used && rather than &, and I got rid of curLine != "". That's a silly comparison; it will never be true. It will check if the memory of the String "" that was just created has the same as curLine.

    Hope this helps.
    I am sorry to interfere, but if it is useless; why it is there?
    Just I would like to learn.

  5. #5
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by Kinda Electroni
    I am sorry to interfere, but if it is useless; why it is there?
    Just I would like to learn.
    If what is useless?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center