Wednesday, 22 January 2020

Default values of primitive fields

Following table summarises the default values of primitive fields.

Primitive Type
Default Value
byte
0L
short
0
int
0
long
0
float
0.0f
double
0.0d
char
\u0000
boolean
false

DemoClass.java
package com.sample.app;

public class DemoClass {
 byte b;
 short s;
 int i;
 long l;

 float f;
 double d;

 boolean boolVar;

 char ch;
}

App.java
package com.sample.app;

public class App {

 public static void main(String[] args) {
  DemoClass obj = new DemoClass();
  
  System.out.println("byte : " + obj.b);
  System.out.println("short : " + obj.s);
  System.out.println("int : " + obj.i);
  System.out.println("long : " + obj.l);
  System.out.println("float : " + obj.f);
  System.out.println("double : " + obj.d);
  System.out.println("boolean : " + obj.boolVar);
  System.out.println("char : '" + obj.ch + "'");
  
  if(obj.ch == '\u0000') {
   System.out.println("ch is equal to '\\u0000'");
  }else {
   System.out.println("ch is not equal to '\\u0000'");
  }
 }

}

Output
byte : 0
short : 0
int : 0
long : 0
float : 0.0
double : 0.0
boolean : false
char : ''
ch is equal to '\u0000'

Reference


You may like

No comments:

Post a Comment