Data Types Default Values Java


Java does not support garbage values. An unassigned local variable gives compilation error and an instance (global) variable takes a default value. Following program illustrates the default values given by JVM for unassigned instance variables.

String is not a data type. If not assigned, String takes null as the default.

public class DataTypeDefaults
{
   byte byteValue;  
   short shortValue;  
   int intValue;  
   long longValue;  
   float floatValue;  
   double doubleValue;  
   char charValue;
   boolean booleanValue;  
   String stringValue;
   
   public static void main(String args[])
   {
      DataTypeDefaults dtd = new DataTypeDefaults ();
      System.out.println("byte default value: " + dtd.byteValue);
      System.out.println("short default value: " + dtd.shortValue);
      System.out.println("int default value: " + dtd.intValue);
      System.out.println("long default value: " + dtd.longValue);
      System.out.println("float default value: " + dtd.floatValue);
      System.out.println("double default value: " + dtd.doubleValue);
      System.out.println("char default value: " + dtd.charValue);
      System.out.println("boolean default value: " + dtd.booleanValue);
      System.out.println("String default value: " + dtd.stringValue);
   }
}

Data Types Default Values Java

Observe the screenshot on Data Types Default Values Java. The default values for whole numbers is 0, for floating-point numbers is 0.0 and for primitive data type boolean it is false. String takes null and char does not print any value. Actually, it prints a value equivalent to Unicode value of /u0000 which prints nothing.

14 thoughts on “Data Types Default Values Java”

  1. Think you sir for your guidence. I like your explanation with simple example, which i understand very fast and remember it easilly.

    thank you! thank you!! thank you!!!

  2. sir ,this website is very useful for us .i have sugested this website to many and all are saying so.
    sir one more thing can u upload the same kind of website for c and c++ also

  3. This site is very helpful for us to learn everything very easily Sir.Thank you for developing such a nice site.Thanks alot Sir.

Leave a Comment

Your email address will not be published.