1
public class Test {
    static char ch;

    public static void main(String[] args) {
        String str = Character.toString(ch);
        System.out.println("abc" + str + "def");
    }
}

Console output : abc
Expected output : abc def

Since default char value is 0(space). Any concept I'm missing here?

ernest_k
  • 44,416
  • 5
  • 53
  • 99
Buddy
  • 87
  • 7
  • actually char ch = 0, means NUL. I mis-understood it with a space. Thanks! https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html – Buddy Mar 22 '19 at 06:51

1 Answers1

1

According to standard -static variables are by default initialized to zero (0 and not '0')or blank. But it's a good practice to always initialize it explicitly.

static char ch = 0; //=>abcdef or static char ch = ' '; =>abc def

or if you want value to 0 use-

static char ch = '\0'; //=>abcdef
Jaja
  • 662
  • 7
  • 15
Onkar Musale
  • 909
  • 10
  • 25