• 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

Arrary Problem

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ArraryTest{
public static void main(String[]args){
byte[] b = {12,3};
short[] s = {2,3};
char[]c = {'a','g'};
int[] i = {12};
float[] f = {1.2f,4.1f};
Integer[]ob = {new Integer(0)};
System.out.println(b);
System.out.println(s);
System.out.println(c);//1
System.out.println(i);
System.out.println(f);
System.out.println(ob);
}

}

In the above program why (char[]) in line 1 produce different result?
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because char has an overridden toString() method which the other objects don't have.


toString()
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Remko, you're wrong. It is not because char[] has an overridden toString() method.

It is because class PrintStream, of which System.out is an instance, has several overridden println() methods. There is a special version of the println() method that takes a char[], and that produces the result you see in line 1.

For the other arrays, there is no overridden version of println(), so println(Object) is called. That method calls toString() on the object that's passed in.

(Note it's called an array, not "arrary").
[ December 24, 2007: Message edited by: Jesper Young ]
 
Remko Strating
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper. Almost true.

But it's always nice to learn some more detail and thanks for your explanation.
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper Young for your right explanation. But Jesper i want to known have any resons why only use println for char[]
[ December 25, 2007: Message edited by: Prasun Howlader ]
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is because Strings are represented internally as char[], and because Strings are immutable you might want to operate on them using this array and have them printed afterwards, String have toCharArray().
 
reply
    Bookmark Topic Watch Topic
  • New Topic