File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes default value for Array elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "default value for Array elements" Watch "default value for Array elements" New topic
Author

default value for Array elements

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
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

Joined: Oct 13, 2005
Posts: 39394
    
  28
For a start, you should avoid putting lots of code in the main method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39394
    
  28
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

Joined: Sep 29, 2008
Posts: 12617

Rajiv Chelsea wrote:Is ther any better way to write the following piece of code?

Better in what way?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default value for Array elements