0

I have a line in file config.properties

clean=true

and use following code to get this property

private static String clean;
Properties prop = new Properties();
try {
    prop.load(new FileInputStream("config.properties"));
    clean = prop.getProperty("clean");
}

I use System.out.println(">"+clean+"<") to see the output and get ">true<", which indicates there is no blank, no \n

However, when I use

if (clean == "true") {          
    // program does not go here
}
else {
    // program goes here
}

what is the possible reason?...

Litchy
  • 355
  • 1
  • 4
  • 18

2 Answers2

0

Try the following:

== checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.

if (clean.equals("true")) {          
    // program does not go here
}
else {
    // program goes here
}
amrender singh
  • 7,949
  • 3
  • 22
  • 28
0

The Problem is you are using equality operator which doesn't compare literal instead references. So you have to use equals method to do literal check

if ("true".equals(check)) {          
    // Now Program will go here
}
else {
 // and now here
}
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62