0

my date format changes according to certain conditions, in order to have global access I initialize them and then assign a value according to condition.

But looking at De-bugger these values are not being re-assigned, they remain null as per initialization.

I am new to java what is the assignment behavior that is causing this ?

Here's code:

SimpleDateFormat df     = null;
        SimpleDateFormat df2    = null;
        SimpleDateFormat date_c = null;
        SimpleDateFormat t      = null;
        SimpleDateFormat t2     = null;
        SimpleDateFormat df5    = null;
        SimpleDateFormat df3    = null;

        if ( make == "NCR")
        {
        df     = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        df2    = new SimpleDateFormat("yyyyMMddHHmmss");
        date_c = new SimpleDateFormat("yyyyMMdd");
        t      = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        t2     = new SimpleDateFormat("yyyyMMddHHmmss");
        df5    = new SimpleDateFormat("yyyyMMddHHmmss");
        df3    = new SimpleDateFormat("yyyyMMddHHmmss");
        }

        else if ( make == "WINCORE")
        {
            df     = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            df2    = new SimpleDateFormat("yyyyMMddHHmmssS");
            date_c = new SimpleDateFormat("yyyyMMdd");
            t      = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            t2     = new SimpleDateFormat("yyyyMMddHHmmssS");
            df5    = new SimpleDateFormat("yyyyMMddHHmmssS");
            df3    = new SimpleDateFormat("yyyyMMddHHmmssS");
        }
user2385057
  • 537
  • 3
  • 6
  • 21
  • 4
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jon Skeet Jul 03 '13 at 11:57
  • 7
    If you really debugged through this, you'd see neither of the branches being executed - indicating that it's got nothing to do with assignment, and everything to do with string equality comparisons. – Jon Skeet Jul 03 '13 at 11:57
  • I don't see why you need all those date formats. You create 7 instances when 4 would do just fine. And I'd make all of them static & final; I'd also set lenient to false for all. – duffymo Jul 03 '13 at 11:59
  • @duffymo I can see a need for it in terms of modifiability - if you want to change one of them, it's simpler to just have to change it here rather than having to create a new variable for it since there was only 4 variables. However, just assigning one to the other should work - i.e. `t2 = df2` (unless this is what you meant). – Bernhard Barker Jul 03 '13 at 12:01
  • what is the value of your make variable ? – David Hofmann Jul 03 '13 at 12:02
  • 1
    I'd just create one DateFormat per format and reuse it. If I wanted a new one, I'd create it. – duffymo Jul 03 '13 at 12:06

2 Answers2

3

Because neither of two conditions fulfils. You use == for String comparison, and this is most likely an error. == compares objects identity and strings are objects. For value-based comparsion you should use equals. Putting literal to the left gives you a null-safe comparison (it is a common Java idiom): "NCR".equals(make). But you can also use make.equals("NCR") if make is not null or you want NullPointerException for null make.

Compare the following:

assert "NCR" == "NCR" : "String literals are interned, so they have the same identity";
assert "NCR" != new String("NCR") : "String with the same value but not the same identity"; 
assert "NCR".equals(new String("NCR")) : "But they are equal";
Rorick
  • 8,857
  • 3
  • 32
  • 37
  • Please use `make.equals("NCR")` instead of the other way around (unless `make` can be `null`). We don't use `1 == i` or say `1 equals to i` and this is the same as doing that. – Bernhard Barker Jul 03 '13 at 12:02
  • When you use "==" to compare two Strings, Java won't compare the value of those strings, but their identities. If you want to compare their value, use make.equals("WINCORE") instead. – Fabinout Jul 03 '13 at 12:02
  • well it depends on how you define make. if you define make as ` String make = "NCR";` then ` make == "NCR"` will return true – chetan Jul 03 '13 at 12:03
  • 1
    @Dukeling "NCR".equals(make) is better than make.equals("NCR") in my view. Since `make.equals("NCR")` can throw `NullPointerException` – chetan Jul 03 '13 at 12:05
  • 1
    "Yoda" coding. It's an old C/C++ idiom. I think it makes sense to do it when you have a static String. – duffymo Jul 03 '13 at 12:07
  • @chetan If you defined `make` as `String make = "NCR"` you probably shouldn't be using a string. An enum or int would be better IMO. – Bernhard Barker Jul 03 '13 at 12:11
  • int? No. I'd rather see "NCR" than 5. And enum is arguably okay, but this is someone who's new to Java, so I'd say a String is fine for now. – duffymo Jul 03 '13 at 13:54
  • @duffymo I was thinking something like `static final int NCR = 5`. Arguably an enum is probably better, but I usually find myself needing to hit it with a hammer to get it to work the way I want it to. – Bernhard Barker Jul 03 '13 at 14:10
2

When comparing strings in java, you do not use ==, you use equals()

if ("NCR".equals(make))
 ...
else if ("WINCORE".equals(make))

Also, put the string literal first, in case that make is null, so you don't get a NullPointerException.

Rorick
  • 8,857
  • 3
  • 32
  • 37
darijan
  • 9,725
  • 25
  • 38