Why string comparsion do not work?

Why this rule is always True?

var String newline = "s"
var String oldline = "s"
if (newline !== oldline) {
	logWarn ("Test", "True" )	
} else {
	logWarn ("Test", "False" )	
}
newline != oldline

Extra = in your rule

Thank you.
Some time ago openhab say to me that i need to change != to !== in my rules.
Suppose this message was a bug.

Was totally unaware of that, still happily on 2.1…

Ok, this is a tricky one to explain.

When you use !=, it is the same as calling !newline.equals(oldline). This calls the .equals method that compares each letter of the two strings to see if they are the same or not.

When you use !==, you are asking if these two objects are literally the same object in memory.

To use a metaphor, if you write “s” on two pieces of paper, != is asking if the word on both pieces of paper are different and !== Is asking if they are the same pieces of paper.

The tricky part is if newline or oldline can possibly be null, you will get a warning in the logs if you don’t use the operator with the extra =.

So absolutely do not replace all of your == with === because it will break your rules. Instead either live with the warnings when you know that both operands will never be null or switch to using .equals directly in place of ==.

2 Likes