This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on char array

 
Ram Murthy
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

I chose the answer as e " ". When I added the code to a program and SOP
C[50] I got spaces as well, but the answer in the answer key(MINDQ mock exam) has been given as c '\u0000'. Does anyone one know how, is it because I am having a different version of JAVA.

Cheers,
JP
 
Joseph A Alexander
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" " is a String literal. char cannot hold it. '\u0000' is unicode for space.

You can try this
public class test {
public static void main(String [] args) {
System.out.println("-"+ '\u0000' + "-");
}
}
 
Ram Murthy
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it cannot hold spaces, how is it I am getting spaces when I do a SOP, please execute the below code...and you will see for urself..

class Test1 {
public static void main(String[] args) {
char[] c = new char[100];
System.out.println("-"+ c[50] + "-");
}
}

Am I missing something ..

Cheers,
JP
 
Usha Hanumolu
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,

You are getting space because the char array will be initialized with '\u0000'(unicode for space) but not with " ". So when u try to print c[50] it will print space in place of '\u0000'.

Hope you got it..

Regards,
Usha.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to try this:

System.out.println((int)c[50]);

This will print out the numerical Unicode value of the char.

Layne
 
Usha Hanumolu
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JP Ravi:
If it cannot hold spaces, how is it I am getting spaces when I do a SOP,


char array can hold spaces but not String literals such as " "
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic