• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Can you post a copy of the code?
------------------
Jane
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
reply
    Bookmark Topic Watch Topic
  • New Topic