hi!! i'm a beginner on java. i've read some books but have not come across default value for a char. if i declare a char without initialising the system exits at the point the value is get? what is the reason?
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Amit, The default value for a char is <code>\u0000</code>, a Unicode 0. Only class and instance variables are automatically initialized with their default values. If you are using an uninitialized local variable you will run into a compile error. Hope that helps. ------------------ Jane
Thanks Jane but why does the system exits at the point u try to get an unintialised char any body with answer's to that
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Amit, Can you post a copy of the code? ------------------ Jane
Zeba Mojez
Greenhorn
Joined: Oct 25, 2000
Posts: 10
posted
0
Originally posted by Jane Griscti: Amit, Can you post a copy of the code?
Here is the code which prints nothing (blank) for the default value of the char class Xyz { private int x; public void getX() { x = 10; System.out.println(x); } } public class Separation extends Xyz { /*private static int i; private static short sh; private static long l; private static byte b; private static boolean bl; private static double d; private static float f; private static Separation s1; */ private char c; private int i; private short sh; private long l; private byte b; private boolean bl; private double d; private float f; private Separation s1; public static void main ( String [] args ) { Separation s = new Separation(); /*System.out.println ("Hello World"); System.out.println ("int i = "+i); System.out.println ("short sh = "+sh); System.out.println ("long l = "+l); System.out.println ("byte b = "+b); System.out.println ("boolean bl = "+bl); System.out.println ("float f = "+f); System.out.println ("double d = "+d); ystem.out.println ("Object s = "+s1); */ int z=0; System.out.println ("Object z = "+z); s.getX(); s.abc(); s.getDefault(); } public void getDefault() { System.out.println ("Hello World"); System.out.println ("int i = "+i); System.out.println ("short sh = "+sh); System.out.println ("long l = "+l); System.out.println ("byte b = "+b); System.out.println ("boolean bl = "+bl); System.out.println ("float f = "+f); System.out.println ("double d = "+d); System.out.println ("Object s = "+s1); System.out.println ("Char = "+c); } public void abc() { getX(); } }
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
It didn't exit because the char was uninitialized. It ended because it was over. The uninitialized char c printed as a blank and that was the last command. Try adding this: System.out.println ("Char = "+c); System.out.println ("Unicode = "+"\u0000");
"JavaRanch, where the deer and the Certified play" - David O'Meara