19
public boolean clearSelection() {
    int i = 0;
    if (!this.m_SelectedComps.isEmpty()) {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
            ((AnnotComponent) localIterator.next()).remove();
        this.m_SelectedComps.clear();
    }

    return i;
}

How to convert the integer to boolean?

2
  • 4
    Java is not like C++.In C++ 0 is false and other integers are true, in Java integer can't be used as boolean. Commented Feb 14, 2012 at 4:57
  • What is the purpose of the integer? There is no obvious reason to use anything else than boolean for your example. @shift66 That feature isn't C++ specific. It's a general rule. I can't remeber another language beside JAVA which can't convert int to bool. Commented Apr 6, 2017 at 12:55

7 Answers 7

46

Try using this return

return i == 1;

or just use a boolean to start (with a better name):

 public boolean clearSelection()
  {
    boolean flag = false;
    if (!this.m_SelectedComps.isEmpty())
    {
      flag = true;
      Iterator localIterator = this.m_SelectedComps.iterator();
      while (localIterator.hasNext())
        ((AnnotComponent)localIterator.next()).remove();
      this.m_SelectedComps.clear();
    }
    return flag;
  }

It continues to mystify me why people use i -- a horrible variable name. Looks like 1 and does not convey any meaning.

Sign up to request clarification or add additional context in comments.

16 Comments

Concerning i: "There are only two hard things in Computer Science: cache invalidation and naming things" -- Phil Karlton
@Hogan Not sure which font you're using, it sure don't look like a 1 in my IDE
i comes from Fortran.
IMO i comes from "iterate", "iterator"
Should we define ´true´ as 1 or everything that is not 0?
|
16

May be you can just modify your return statement without much change to code as below:

return i > 0 ? true : false ;

2 Comments

Nice but what if the int equal to 0! It won't work even if you use >= because when int is null this line of code will return true always.
The ternary operator is redundant. It's enough to write return i > 0;.
16

I know this thread is old but wanted add some code that helped me and might help others searching this...

You could use org.apache.commons.lang api to convert an int to boolean using the BooleanUtils class:

BooleanUtils.toBoolean(int value)

"Converts an int to a boolean using the convention that zero is false." (Javadocs)


Here are the Maven & Gradle dependencies, just make sure you check you're using the latest version on the link http://mvnrepository.com/artifact/org.apache.commons/commons-lang3

Maven Dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Gradle Dependency:

'org.apache.commons:commons-lang3:3.4'

4 Comments

It's things like this that give Java a bad name. Why would anyone use this instead of just value != 0? (Which results in a boolean using the convention that zero is false)
@SalmanvonAbbas I'm not sure I follow. In C, zero is false and non-zero is true. This is pretty widely accepted.
@SalmanvonAbbas There are many valid reason for Java is problematic, but this is just an util function. I see all fine here.
Here's a good use case: I have used this lib to parse a text config value into boolean, for flexibility. These would all return true: 'true', 'on', 'y', 't' , 'yes', 'TrUe'. I wouldn't ADD the lib to parse 1 and 0. But would definitely use it if it's already in my project.
5

Convert int to boolean:

return i > 0;

Comments

2

Declare i as a boolean:

public boolean clearSelection()
{
    boolean i = false;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = true;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
          ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
    }
    return i;
}

Comments

1
public boolean clearSelection(){
    int i = 0;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
            ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
     }
     return (i!=0);
}

Comments

1

You can't type cast between integer and boolean in Java but you could use the following technique which I use oftenly:

To convert boolean into integer:

int i;
return i != 0;

To convert integer into boolean:

boolean b;
return b ? 1 : 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.