About Question enthuware.ocajp.i.v8.2.2017 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
VijayTripathi
Posts: 8
Joined: Mon Jul 06, 2020 1:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.2017 :

Post 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.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.2017 :

Post 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).

VijayTripathi
Posts: 8
Joined: Mon Jul 06, 2020 1:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.2017 :

Post 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.

ignore.your
Posts: 2
Joined: Thu Dec 23, 2021 5:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.2017 :

Post 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------------------------------

:D

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.2017 :

Post 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.

Badem48
Posts: 29
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.2017 :

Post 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.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.2017 :

Post 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'.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests