Page 1 of 1
Re: About Question enthuware.ocajp.i.v8.2.2017 :
Posted: Sat Aug 01, 2020 11:01 am
by VijayTripathi
For char, the print/println methods translate the character into one or more bytes according to the platform's default character encoding. That is why while printing a char value of 0, a blank space is printed instead of 0 (even though the char's integral value is 0).
Can you please elaborate this. Unable to understand the mystry.
Re: About Question enthuware.ocajp.i.v8.2.2017 :
Posted: Sat Aug 01, 2020 11:08 am
by admin
Not really sure which part you don't understand. Like the ascii value of 'a' is 97, so, if you try to print a char variable containing 97, a will be printed. Similarly, whatever character is represented by value 0, that is printed (it is just a blank character).
Re: About Question enthuware.ocajp.i.v8.2.2017 :
Posted: Sun Aug 02, 2020 12:20 am
by VijayTripathi
admin wrote: ↑Sat Aug 01, 2020 11:08 am
Not really sure which part you don't understand. Like the ascii value of 'a' is 97, so, if you try to print a char variable containing 97, a will be printed. Similarly, whatever character is represented by value 0, that is printed (it is just a blank character).
So, though 0 don't have any character representation that's why it is printing space. Thanks for the explanation.
Re: About Question enthuware.ocajp.i.v8.2.2017 :
Posted: Thu Dec 23, 2021 5:49 pm
by ignore.your
Well, the correct answer for any operating system would be: "none of the above"
(char)0 is ascii NUL. How this is printed depends on the operating system shell.
On Linux it could be (char)32, "0" or even "\bf". So #2 and #3 could be correct answers as well.
E.g.: Emacs prints the NUL as ^@. And the last statement is a print, not a println, so actual the output from an Emacs shell window would be:
0.0 ^@ false------------------------------

Re: About Question enthuware.ocajp.i.v8.2.2017 :
Posted: Thu Dec 23, 2021 10:41 pm
by admin
No, what you are taking about is how programming environments use the value 0. For eg. 0 may be used to represent a null reference.
But is not what this question is about. It is very clearly creating a java char variable containing a value zero and how it is printed. The integral value of a char is represented as a character based on the default character encoding of the platform on which the JVM is running. So, the given answer and explanation are correct.
Re: About Question enthuware.ocajp.i.v8.2.2017 :
Posted: Tue Jun 04, 2024 5:43 am
by Badem48
Hi,
The default values of primitives are listed in this
https://docs.oracle.com/javase/specs/jl ... jls-4.12.5 documentation.
For type char, the default value is the null character, that is, '\u0000'.
In my machine which is a MacOS it prints a little square, meaning that it's not a printable character. Maybe on Windows it prints nothing, but still I think explanation is little missing.
Re: About Question enthuware.ocajp.i.v8.2.2017 :
Posted: Tue Jun 04, 2024 10:18 pm
by admin
Char is an integral type, whose values are 16-bit unsigned integers representing UTF-16 code units. So, char value of 0 is not null but is interpreted as a null character i.e. '\u0000'.