Just a curious question, not important really. More for a discussion.

Isn't using

int i = 0;
boolean count = false;

pretty much the same thing when flagging? I mean when something goes right, change

i = 1;
count = true;

and get the same result. What's the advantage of boolean over a simple int change? example:

for (int i = 0; i < newArray.length; i++)
        {
            if (i == element)
            {
                count = true;
                newArray[i] = combinedInfo[i + 1];
            }
            else
                if (count)
                    newArray[i] = combinedInfo[i + 1];
                else
                    newArray[i] = combinedInfo[i];
        }

versus

for (int i = 0; i < newArray.length; i++)
        {
            if (i == element)
            {
                i = 1;
                newArray[i] = combinedInfo[i + 1];
            }
            else
                if (i == 1)
                    newArray[i] = combinedInfo[i + 1];
                else
                    newArray[i] = combinedInfo[i];
        }

thanks

Recommended Answers

All 3 Replies

The difference is that boolean makes more sense to use when all you need is true/false, and boolean probably takes only one byte of memory instead of 4 or bytes for an int. It makes your program a lot easier for us mortal humans to read if you use boolean to represent true/false conditions instead of int.

Moreover in Java you can only use expressions that evaluate to a value of type boolean to control statements like if, while, do-while [1] (unlike in C/C++).

int i = 1;
if (i == 1) { // valid since i == 1 evaluates to a boolean value
    //...
}

if (i) {      // valid in C/C++, invalid in Java
    // ...
}

boolean b = true;
if (b) {      // valid in Java
    // ...
}

Choosing the appropriate type for your variables contributes to code readability.

[1] The JLS includes the following line in each of their respective sections:

The Expression must have type boolean or Boolean, or a compile-time error occurs.

(14.9, 14.12, 14.13)

an int would be a logical choice if you were looking for an index, for instance,

String[] myArray = fillArray(); // let's assume this exists
String searchWord = "search";

int found = -1;
for ( int i = 0; i < myArray.length; i++){
  if ( myArray[i].equals(searchWord)){
    found = i;
    break;
  }
}

this way, you'll find the first occurence of searchWord in the array. want the last? just remove the break statement.
but it's quite logical that there are several possible values that can be returned, and you can't predict yourself what will be the correct answer, as long as it's a positive value.
a lot of developers 'll use -1 as a default value for when the searchWord is not found as an element of the array. it's easy to read, and while 0 would be a valid index, it's clear to anyone working with arrays in Java, that that is not a valid index.

now, if your question was just: find whether or not the String is present in the array, don't need to return the index, AncientDragon is right... why try to build an airplane when a skateboard 'll suffice for your needs? a boolean 'll give all the information, and, there is no invalid return value here anyway: either the word IS in the array (true), or it isn't (false), there is no such thing (or even need for) a 'maybe'.

for this, using the enhanced loop 'll make your code cleaner as well:

for ( String element : myArray ){
  if ( element.equals(searchWord ))
    return true;
}
return false;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.