2

I have doubt while taking a null or empty char check in Java. Is both have to be checked in any way.

For example in database the variable length for Char is 1 . It will allow null as well. So if you have empty char does it mean null? or we have to check as

if(someObject.getSomeCharValue()=='' && someObject.getSomeCharValue()==null) {
    //true
}
else{
    //dont compile
}
Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
BelieveToLive
  • 281
  • 1
  • 4
  • 18
  • you could check if the length is greater than `0` instead – Uma Kanth Jun 12 '15 at 09:22
  • 5
    Is `getSomeCharValue()` returning a `char` or `Character`? `char` can't be `null`. `Character`, it's boxed object, can. Also, the default for `char` is `\u0000`. – Kevin Cruijssen Jun 12 '15 at 09:23
  • @KevinCruijssen char primitive. – BelieveToLive Jun 12 '15 at 09:29
  • char primitive can't be null as well as it can't be empty. Just tried it myself and it doesn't compile – Arthur Eirich Jun 12 '15 at 09:30
  • @ArthurEirich in which java version you compiled i mean VM version? – BelieveToLive Jun 12 '15 at 09:33
  • 2
    `char` can't be `null` since it is primitive type. Also there is no such a thing as empty char `''` in Java. But even if previous statements would be false and you could get `null` or `''` then char can't be `null` AND `''` at the same time so your condition doesn't make sense. – Pshemo Jun 12 '15 at 09:34
  • If the value is not present in database, what value do you pass to `someCharValue` variable? – Naman Gala Jun 12 '15 at 09:35
  • @BelieveToLive my java.vm.version is '24.71-b01' and I use java '1.7.0_71' – Arthur Eirich Jun 12 '15 at 09:37
  • Thanks for the comments. – BelieveToLive Jun 12 '15 at 09:39
  • 2
    Your code can't compile for `someObject.getSomeCharValue()==''` since there is no `''` in Java. If `someObject.getSomeCharValue()==null` part compiles then it means that result of `getSomeCharValue` is wrapper type `Character`, not primitive `char` (as they can't be `null`s so `==null` would not compile here). In that case you can check for null like you are doing it now, or check against default value of char which is `'\u0000'` which can be also written as `'\0'` or simply `0` (char is also numeric type). In that case you should also remember to use OR operator, not AND. – Pshemo Jun 12 '15 at 09:44

3 Answers3

3

char has no value with ''. In char, null character (or empty char) is \0 or \u0000. You can't check with '' or null.

For example if you declare a char like this:

char c = '';//compilation error here

or

char c = null;//compilation error here

so if you want to check whether a char is null, then you need to use the following code:

char c = '\0';
if (c == '\0') System.out.print("char is null");//if(c == '\u0000') also can be possible
else System.out.print("char is not null");
Community
  • 1
  • 1
Estimate
  • 1,421
  • 1
  • 18
  • 31
  • `if (c == '\0' || c == '\u0000')` doesn't make much sense since as you already mentioned `'\0'` and `'\u0000'` are equal. – Pshemo Jun 12 '15 at 09:47
0
if(someObject.getSomeCharValue()=='' && someObject.getSomeCharValue()==null)

both someObject.getSomeCharValue()=='' and someObject.getSomeCharValue()==null are compilation error.

if there is no value in the DB that means it is null but not empty char.

There is nothing called empty char ('') in Java.
But there is empty string ("").

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Aradhya
  • 182
  • 1
  • 6
  • 17
-2

Empty chars are not null. They don't just hold any value.

Yes. You should check both if your column allow nulls. Also, your code might give you NullPointerException since you are first checking for empty char and then for null. Better to use below

if(someObject.getSomeCharValue()==null || someObject.getSomeCharValue()=='' )
Aakash
  • 2,029
  • 14
  • 22