Search code examples
javadefault-constructor

how default constructor in java gets called?


public class Hello {

  int i;
  char ch;
   Hello(int x){
    i=x;

   }
    Hello(char c){
     ch=c;
     }  

   public static void main(String[] args) {
  Hello h=new Hello(1);
   System.out.printf("value of i  is %d"+"value of ch is %c",h.i,h.ch);
   //Hello h=new Hello('T');
   //System.out.printf("value of i  is %d"+"value of ch is %c",h.i,h.ch);
    }

O/p is :value of i is 1value of ch is

my question is why ch value is not initialized ?? while if the other case O/p is:value of i is 0 value of ch is T why in this case i is initialized.??


Solution

  • As per this the default value for int is 0 and for char is '\u0000' (which is nothing but null) and that is what you are seeing.