Default value of Fields :--
When we are declare field they get a default value even if we don't assign it, we can check them with a small example
public class DefaultValueTester {
int i;
double d;
short s;
long l;
char c;
boolean b;
Object o;
public static void main(String[] args) {
DefaultValueTester t = new DefaultValueTester();
System.out.println(t.i);
System.out.println(t.d);
System.out.println(t.s);
System.out.println(t.l);
System.out.println(t.c);
System.out.println(t.b);
System.out.println(t.o);
}
}
The out put of this program will be
0
0.0
0
0
false
null
But if we work with local variables things are going to be a bit different.
Lets now try
public static void main(String[] args) {
int i ;
System.out.println(i);
}
It fails to compile, so we can clearly see that no default value gets assigned to local variables
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment