1

i have to perform some task by checking if a char have a zero value and if a char has null '\u0000 value but don't know java compiler is treating both as same here's the sample code

char nullTest = 0; 
System.out.println(nullTest == '\u0000'); //it is giving true

if i initialize it as nullTest = '0' then it is giving false, but in my program I frequently assigns numbers & null '\u0000' to this variable due to which it gives runtime errors,

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • https://stackoverflow.com/questions/12195628/understanding-the-difference-between-null-and-u000-in-java – Ivan Apr 18 '19 at 18:09
  • `if i initialize it as nullTest = '0' then it is giving false`. Because `'0'` is not `0`. It has some (Unicode?) code, which differs from `0`. On the other hand `'\u0000'` is, by definition, a character with code `0`. I've no idea why you call this `null`. –  Apr 18 '19 at 18:10
  • @dyukha it's 48. – Andy Turner Apr 18 '19 at 18:11
  • the question is why 0 and '\u000' are treated as same, how can i treat them differently we all know both are two different things, – Junaid Ahmed Apr 18 '19 at 18:15
  • \u000 would be the unicode representation of a character with ASCII code 0, Dont get confused with **\u**.Actually java reads unicode with this prefix – Hasnain Ali Bohra Apr 18 '19 at 18:17
  • `\u0000` specifies a *character* with the hex value `0000`. That's the *definition* of that syntax. See Java Language Specification [3.2. Lexical Translations](https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.2): *"A Unicode escape of the form `\uxxxx`, where `xxxx` is a hexadecimal value, represents the UTF-16 code unit whose encoding is `xxxx`."* – Andreas Apr 18 '19 at 18:22
  • @JunaidAhmed they are the same. The only difference in a representation. You can either assign a character to a `char` variable or its ASCII or Unicode code. `char c = 51;` and `char c = '3';` assign the very same value to variable `c`. – Ivan Apr 18 '19 at 18:23
  • now i got it, thanks @ivan – Junaid Ahmed Apr 18 '19 at 18:29

1 Answers1

2

The notation \uXXXX primarily only occures in .java and .properties files. There it is read as a Unicode code point. Unicode text (=using all kind of special characters) often uses the UTF-8 format (though also sometimes UTF16LE and UTF16BE are used).

So if you initialise a character in java using char nullTest = 0; without giving the single quotes it is internally treated as nullTest = unicode of 0 which is '\u0000' but if you initialize a character with quotes like char nullTest= '0' it is treated a Character of string and stored as '0'

Hasnain Ali Bohra
  • 2,130
  • 2
  • 11
  • 25
  • `\uXXXX` is not a Unicode code point. It is a UTF-16 code unit. See JLS [3.2. Lexical Translations](https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.2): *"A Unicode escape of the form `\uxxxx`, where `xxxx` is a hexadecimal value, represents the UTF-16 code unit whose encoding is `xxxx`."* – Andreas Apr 18 '19 at 18:26