What is the default value for a char in java.

The default value per Oracle documentation is char = '\u0000'



But if we compile a simple class to check what it prints, we get the following:


It seems like ' ' a simple space.

But let's add some more code to our coding:


Per the two lines of the new if we have added, you can notice that mychar==0 validation is getting true and as a result the code inside the if is being printed.

So we can conclude that char gets in its default value 0, but what is this 0, see the following table ( table taken from this link).


As you can see in above's ASCII table, the value of 0 is for Nul  (null), this is the first value of the ascii table and it seems it makes an space, but it is not an space.

Hopefully this helps to understand what is what we get as a default value for char.


No hay comentarios:

Publicar un comentario