-
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;
}
-
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.
-
wow. you are awesome man. thanks a bunch
-
 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.
-
 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks