• 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

default value for Array elements

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]=


 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a start, you should avoid putting lots of code in the main method.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Better in what way?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Just the other day, I was thinking ... about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic