6. Initial Values for Variables

6.1. Default Values for Fields

If a static variable or instance variable is not initialized, then it is initialized with default value. Table below shows the default values for primitive data types and reference types.

See the below table for default values of fields Java.

Let's create a class FieldDefaultValues to prinout the default values of fields as below

			package javatutorial;

			/**
			 * Example class: printing the default values in Java
			 * 
			 * @author f5java.com
			 */
			public class FieldDefaultValues {
				
				// 1. Uninitialized fields
				
				boolean boolDefaultValue; // boolean type
				
				int intDefaultValue; // Integer types
				long longDefaultvalue;
				
				float floatDefaultValue; // Floating point types
				double doubleDefaultValue;
				
				char charDefaultValue; // Char type
				
				Car carObject; // Reference type
				
				/**
				 * This method prints the default values of the fields
				 */
				public void printDefaultValues(){
					System.out.println("Printing the default value of boolean " + boolDefaultValue);
					
					System.out.println("Printing the default value of int " + intDefaultValue);
					System.out.println("Printing the default value of long " + longDefaultvalue);
					
					System.out.println("Printing the default value of float " + floatDefaultValue);
					System.out.println("Printing the default value of double " + doubleDefaultValue);
					
					System.out.println("Printing the default value of char " + charDefaultValue);
					
					System.out.println("Printing the default value of reference type " + carObject);
				}

				public static void main(String[] args) {
					// Instantiate and call the method
					FieldDefaultValues defaultValue = new FieldDefaultValues();
					defaultValue.printDefaultValues();
				}
				
			}