• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default value for Array elements

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Is ther any better way to write the following piece of code?



And what will be the default value of char elements?
I tested it on two systems and got two different values.

On my currnt system i got the following output

charArray[0]=
charArray[1]=
charArray[2]=
charArray[3]=


 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start, you should avoid putting lots of code in the main method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is the output I obtained from your code, completely unchanged
campbell@********:~/java$ java TestArray
intArray[0]=0
intArray[1]=0
intArray[2]=0
intArray[3]=0
booleanArray[0]=false
booleanArray[1]=false
booleanArray[2]=false
booleanArray[3]=false
charArray[0]=
charArray[1]=
charArray[2]=
charArray[3]=
stringArray[0]=null
stringArray[1]=null
stringArray[2]=null
stringArray[3]=null
The default value of a char is \0 or the null character. This does not print anything.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Chelsea wrote:Is ther any better way to write the following piece of code?

Better in what way?
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Chelsea wrote:On my currnt system i got the following output

charArray[0]=
charArray[1]=
charArray[2]=
charArray[3]=

That seems quite alright. For char arrays the default value of '\0' is used, which doesn't have any printable value. It's what C uses to mark the end of strings for exactly this reason.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic