10

If I allocate array of characters like this:

char[] buffer = new char[26];

what are the default values it is allocated with? I tried to print it and it is just an empty character.

System.out.println("this is what is inside=>" + buffer[1]);

this is what is inside=>

Is there an ASCII code for it? I need to be able to determine if the array is empty or not, and further on, when I fill say first five elements with chars, I need to find out that the sixth element is empty. Thanks.

6
  • default value \u0000. test like this buffer[1] == '\u0000' that will return true if it's not initialized or has default value. Commented Feb 25, 2016 at 7:01
  • That's not an ASCII code but a Unicode code point. This one to be precise. Commented Feb 25, 2016 at 7:03
  • better use collection List to check for empty list that auto grow and shrink based on no of elements because checking each and every element in array for empty array will result into O(n) complexity which is not good. Commented Feb 25, 2016 at 7:04
  • Its an interview question, and they want to see two solutions: with and without using data structures. Working here on the one that does not use linked list Commented Feb 25, 2016 at 7:09
  • isn't \u0000 the same as \0? Commented Aug 1, 2019 at 1:36

3 Answers 3

15

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[] using null 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.)

Sign up to request clarification or add additional context in comments.

8 Comments

Its an interview question, and they want to see two solutions: with and without using data structures. Working here on the one that does not use linked list (or hash table).
Need to determine if the string has all unique characters
What would be the difference between char and Character data types?
@user1090944: That sounds like you only need a boolean[]. It sounds like you're effectively using a character as a key, not a value.
@user1090944: char is a primitive type, Character is the wrapper class for it. That's a bigger topic...
|
1

You can also convert it to int and then compare.
for ex - Suppose I declare my array as

char[] arr = new char[10];
arr[3]='1';

Then, the below code

for(char c:arr){
    if((int)c!=0){
        System.out.println(c);
    }
}

Will output

1

1 Comment

there even is no need to "convert it to int" - just c != 0 will work fine (in Java, due to binary numeric promotion)
0

Hope the output to below program clears the doubt.

    static char ch;
    public static void main(String[] args)
    {
        char chArr[] = new char[5];
        System.out.println(chArr[1] == 0);
        System.out.println(chArr[1] == '\u0000');
        System.out.println(chArr[1] == ch);
//      System.out.println(chArr[1] == null); doesn't compile
    }

Output:
true
true
true

Also note that char with ascii value 0 is not assigned any printable symbol.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.