May 29, 2008

Chapter: Everything is an Object

Exercise 1: (2) Create a class containing an int and a char that are not initialized, and print their values to verify that Java performs default initialization.

Java code:
public class Exercise1 {
static int i;
static char c;

public static void main(String[] args) {

System.out.println("Int: " + i);
System.out.println("Char:" + c);

Int: 0

Ints default to 0 and chars default to null. Note that you will get a complier error if you declare the variables within a method and try to use them without initializing them.