Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Casting
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Operators
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays
Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance
Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Polymorphism
Introduction
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions


Default Constructor provided by Compiler

When we do not explicitly provide a constructor to our class, the compiler provides a default constructor which initialises all the instance variables to their default values. The default constructor provided by the compiler is different from the no argument or default constructor which we provide ourselves. In the constructor that we provide, we are able to set the variables to any default values that we wish to do so which may be 0 ,-1 or some other different value, but this is not the case with the default constructor provided by the compiler. It sets variables of different data types to the default values given by the following table:

Data Type Variable Name
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0
boolean false
char 0
<All reference types> Null


To verify this fact, remove all the constructors from the Student class, create an object of that class and print the information stored in the variables.

public class Student {

   private int rollNumber;
   private String name;
   private int marks1;
   private int marks2;
   private int marks3;

   public void printDetails() {
      System.out.println("Roll Number: " + rollNumber);
      System.out.println("Name: " + name);
      System.out.println("Marks in first subject: " + marks1);
      System.out.println("Marks in second subject: " + marks2);
      System.out.println("Marks in second subject: " + marks3);
      }
   }

   public class StudentTest {

   public static void main ( String[] args ) {
      Student s=new Student();
      s.printDetails();
   }
   }

We get the following output:

Roll Number: 0
Name: null
Marks in first subject: 0
Marks in second subject: 0
Marks in second subject: 0
Average: 0.0

As you can see, all the variables were set to 0 or their equivalent. String which is also a reference type was set to null. (All classes are reference types). When a variable of a class type is set to null, it means that the variable wasn't initialized.

When we provide atleast one constructor to our class, then the compiler no more provides a constructor. It doesn't matter whether we are providing a no argument constructor or a parameterised constructor. The following code generates compilation errors since the class Student has no default constructor and we are trying to create an object using a default constructor.

class Student {

   public Student ( int m ) {
   }
}

class StudentTest {

   public static void main ( String args[] ) {
      Student s=new Student(); // Incorrect
   }
}

Also, if we do not initialise any variables in the constructors provided by us, then they too are set to their default values. This is not the case with local variables (those defined in methods).

class Student {

   int m;

   public Student () {
   }
}

class StudentTest {

   public static void main ( String args[] ) {
      Student s=new Student();
      System.out.println(s.m);
   }
}

The above program prints 0 in the output. This is because any variables that are uninitialized in the constructor are initialized with their default values. However, as the following program shows, this default initialisation does not occur with other types of variables.

public void print() {
   int x;
   System.out.println(x); // A compilation error would be generated saying that x was not initialized
}

Privacy Policy