• 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

char array

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is a question from one of the mock exams:
Q) After the declaration:
char[] c = new char[100];
what is the value of c[50]?
a) 50
b) 49
c) '\u0000'
d) '\u0020'
e) " "
f) cannot be determined
g) always null until a value is assigned
My answer was (e), since the char array would initialize all the elements to " ", when it is created. But the answer says (c). I tested with an example, and char array initializes all elements to " ". Can anyone clarify this please?
Thanks..
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could u cut and paste the code u used to verify this? the following code printed "Element is \u0000":
public class Test {
public static void main(String[] args) {
char [] a = new char [100];
if (a[50]=='\u0000') System.out.println("Element equals \\u0000");
}
}
In fact the compiler won't let u put '' in the if condition.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am displaying the value of the array element c[50] and it displays blanks (spaces).
public class test
{
public static void main( String args[] )
{
char[] c = new char[100];
System.out.println( "value of c[50]: " + c[50] );
}
}
Thanks..
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is not enough to visually verify the value of the variable. in fact if u said
System.out.println("This is the questionable character : \u0000");
that'd look like a blank too. u have to use a condition to verify the content of the variable.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is, "" is a String object, not a char. Any time you use double quotes, you are creating a String object. (In this case it's a String with 0 characters in it, but it's still a String.) All the other answers use single quotes, as appropriate for a char.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rolf, Jim.

[This message has been edited by Java2learner (edited March 01, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
One of the options given in the mock question is '\u0020'. I am curious to know what '\u0020' means? From the above discussion, I know blank spaces are represented in memory as '\u0000'. Also what form of values are these? Are they Hex or Octal values?
Can anyone clarify this please?
Thanks.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<CODE>0x20 ('\u0020')</CODE> is the blank character (space). And <CODE>0x0 ('\u0000')</CODE> is the NULL character. Most (if not all) systems display it as a blank.
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the numbers are hex.\u0020 is the character corresponding to ASCII 32, which, if i remember correctly, is the character '0'.
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tony's right, it is a space.
 
reply
    Bookmark Topic Watch Topic
  • New Topic