This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

defaul value for char?????????????

 
amit vaid
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit vaid
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit,
Can you post a copy of the code?
------------------
Jane
 
Zeba Mojez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic