Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: printing of array of characters vs printing out of numerical(primitive)arrays

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default printing of array of characters vs printing out of numerical(primitive)arrays

    int[] numSeq = {1,2,3};
     
    System.out.println(numSeq);


    char[] nameSeq = {'J','a','v','a'};
     
    System.out.println(nameSeq);

    why does the output of a NUMERICAL primitive array is different from printing a CHARACTER primitive array?, the current knowledge that i have right now is that the first code, when the int array is being called in a print statement, it returns the memory address from where it was created, but why does the second code, a character array is being printed/treated just like a simple String object? but the call is just the same..


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: printing of array of characters vs printing out of numerical(primitive)arrays

    Read the API doc for java.lang.Object.toString() for the format of toString() if you don't override it. For printing arrays there are a couple of handy methods in java.util.Arrays: toString(<type>[]) and toDeepString(<type>[]). I suspect the char[] is being automagically promoted to a String somehow.

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    chronoz13 (September 28th, 2011)

Similar Threads

  1. Printing an array
    By native in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 16th, 2011, 10:07 AM
  2. HELP: UNABLE TO USE NON-PRINTING CHARACTERS IN A LOOP
    By baraka.programmer in forum Loops & Control Statements
    Replies: 5
    Last Post: June 24th, 2011, 03:21 AM
  3. Strange problem printing Unicode characters
    By sophist42 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 29th, 2011, 11:53 AM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 03:41 AM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 05:49 AM