May 29, 2008
at
Thursday, May 29, 2008
Labels:
Computer Science,
Java,
Thinking in Java
Posted by
Billy
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);
}
}
Result:
Int: 0
Char:
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.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment