2

I'm sure this is going to be a head-slapper, but I've been battling with this for a couple days, and am failing to see a problem in the code.

This is a very minimal example of the code, necessary to demonstrate the issue. The code is supposed to parse css styles. I've added comments and println to help visualize the issue. In the "bold" switch case, the boolean is being assigned false, even though it's clear that the value is "bold". (I demonstrate it actually is a 4 character value.)

public class HtmlCssStyle {

    public static void main(String[] args) {
        var s = new HtmlCssStyle("font-weight:bold;");
        System.out.println(s.bold);
    }

    public Boolean bold = null;
    
    public HtmlCssStyle(String cssStyle) {
        Parse(cssStyle);
        System.out.println("Bold: " + bold);
    }
    
    public void Parse(String cssStyle) {
        String[] cssStyles = cssStyle.split(";");
        
        for(String style : cssStyles) {
            String[] parts = style.split(":");
            String name = parts[0].trim();
            String value = parts[1].trim();
            
            switch(name) {
            case "font-weight":
                System.out.println("bold value: *" + value + "* " + value.length()); // value debug (also demonstrates it is entering case
                this.bold = (value == "bold"); // never assigns the Boolean value
                break;
            }
        }
    }
}

My output is:

bold value: *bold* 4
Bold: false
false

In contrast, this test code works just fine:

public class test {

    public Boolean myBool = null;
    
    public static void main(String[] args) {
        System.out.println(new test().myBool);
    }

    public test() {
        String s = "bold";
        this.myBool = (s == "bold");
    }
}
SlowCoder74
  • 103
  • 12
  • 7
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Jul 14 '20 at 19:57
  • 2
    As explained in the link above, don't compare strings with `==` The only case it works is when you have two literal strings because they are referencing the same object. To test if two strings contain the same characters, use `this.bold = "bold".equals(value)` – Ruan Mendes Jul 14 '20 at 19:59
  • 2
    BTW: I just wanted to say how refreshing it is to see a question with an actual minimal reproducable code example to demonstrate the problem. Despite beeing a duplicate this was the best Stackoverflow question I read today. – OH GOD SPIDERS Jul 14 '20 at 20:08
  • Well, that's very confusing. As can be seen in the "test" example, it's almost exactly the same, but works. I guess I need to use equals() and !equals() for all my String comparisons? – SlowCoder74 Jul 14 '20 at 20:11
  • 2
    @SlowCoder74 Your test example works because in Java their is a so called [String Pool](https://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext). In very simple terms: The java compiler will try to reuse the String objects for String literals as much as possible. So if you write the code `String one = "myString"` and `String two = "myString"` the java compiler will realize that he only needs to create one String object to hold the String "myString" and will simply have the two variables point at the same object. – OH GOD SPIDERS Jul 14 '20 at 20:18
  • 1
    Nope it's not a "head-slapper" ;-) This relates to Java basics where it is important to know how Java works with objects (cf. https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) and understanding core Object methods like `equals()` and `hashCode()`. Keep it up. Regards – bsaverino Jul 14 '20 at 20:19

0 Answers0