• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

doubt on char default value...

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am little confused how the "ch" value is being calculated at #2,#3.
When I print the "ch" value at #1, it prints nothing.

My doubt is when i print ba[ch] at#3 it should take as ba[null].Not
sure how it is taking the index value for ch is 0 and printing false.

I know that the default value for char is \u0000 (null character).

Any inputs?? Thanks..

import static java.lang.System.*;
public class TestClass12
{
static boolean b;
static int[] ia = new int[1];
static char ch;
static boolean[] ba = new boolean[1];

public static void main(String args[]) throws Exception
{
out.println(" ch :"+ch);//1
boolean x = false;
if( b )
{
out.println("1");
x = ( ch == ia[ch]);
}
else x = ( ba[ch] = b );//2
System.out.println(x+" "+ba[ch]);//3
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Konda Golamaru:
...My doubt is when i print ba[ch] at#3 it should take as ba[null]. Not sure how it is taking the index value for ch is 0 ...


A Java char is really a numeric value that corresponds to a character symbol (see ASCII table). The "null char" has a numeric value of zero. A Java int can obviously have a zero value, but it does not have a "null" value.

An array index is an int. So in the expression ba[ch], ch is converted to type int with a numeric value of zero.
[ June 09, 2007: Message edited by: marc weber ]
 
Konda Golamaru
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

Thank You. I think I am clear now.

--Konda Golamaru.
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic