What's the default value of char?
The default value of a char
attribute is indeed '\u0000'
(the null character) as stated in the Java Language Specification, section §4.12.5 Initial Values of Variables .
In my system, the line System.out.println('\u0000');
prints a little square, meaning that it's not a printable character - as expected.
What is the default value of char in c
There is no default value which is assigned to it. Some values are not printable and you can assume that random value is one of them, so that is the reason why you are not able to see the result.
The C99 standard says that:
If an object that has automatic storage duration is not initialized
explicitly, its value is indeterminate.
On a side note:
As per C99
If an object that has static storage duration is not initialized
explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
What are the default values of the char array in Java?
It's the same as for any type: the default value for that type. (So the same as you'd get in a field which isn't specifically initialized.)
The default values are specified in JLS 4.12.5:
For type char, the default value is the null character, that is,
'\u0000'
.
Having said that, it sounds like really you want a List<Character>
, which can keep track of the actual size of the collection. If you need random access to the list (for example, you want to be able to populate element 25 even if you haven't populated element 2) then you could consider:
- A
Character[]
usingnull
as the "not set" value instead of'\u0000'
(which is, after all, still a character...) - A
Map<Integer, Character>
- Sticking with
char[]
if you know you'll never, ever, ever want to consider an element with value'\u0000'
as "set"
(It's hard to know which of these is the most appropriate without knowing more about what you're doing.)
I can't see default value for instance char in java
'\u0000'
(char c = 0;
) is a Unicode control character. You are not supposed to see it.
System.out.println(Character.isISOControl(s.c) ? "<control>" : s.c);
Default value for char?
The garbage value can be any combination of 8 bits, 0000 0000
to 1111 1111
. There is no "standard garbage value", it's whatever was in that memory region before it was passed along to your variable.
Why a char default value is not displayed on console?
There's a big difference between the character for "the decimal digit 0" which is U+0030... and U+0000, the null character, which is a control character and so has no printed output. You're printing out the latter, so I wouldn't expect to see 0.
To think about it another way, consider:
System.out.println("a\u0009b");
Would you expect that to print "a9b"? You shouldn't because U+0008 is the tab character, so you'd see something like "a b" with some variable number of spaces between depending on exactly what your output device does.
in C, what is default value for static char type
Per C99:
If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these
rules.
Related Topics
Differences Between Java 8 Date Time API (Java.Time) and Joda-Time
Accessing the Last Entry in a Map
How to Bind an Object List with Thymeleaf
Modifying Local Variable from Inside Lambda
How to Check for a Valid Url in Java
How to Copy File Inside Jar to Outside the Jar
Check If a File Is Locked in Java
How to Properly Use Java Executor
Does Java Support Inner/Local/Sub Methods
Jpa: What Is the Proper Pattern for Iterating Over Large Result Sets
What Does the Registernatives() Method Do
How Does the Spring @Responsebody Annotation Work
Calling Win32 API Method from Java
Creating Classes Dynamically with Java