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 15 of 15

Thread: Please solve my problem

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Please solve my problem

    anyone can explain me the output of this code line by line...


    class Example{
    	public static void main(String args[]){
    		char cr[]=new char[3];
    		int l[]=new int[3];
    		System.out.println(cr);	//L1
    		cr[0]='A';
    		cr[1]='B';
    		System.out.println(cr);		//L2
    		l[0]=78;
    		System.out.println(l+" "+cr);//L3
    		int x=cr[0];
    		int y=cr[1];
    		System.out.println(cr);//L4
    		System.out.println(cr[1]);//L5
    		int m=0x12996d6;
    		System.out.println(m);	//L6
    	}
    }


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Please solve my problem

    first line is a array of characters that is size 3
    second line is a array of int of size 3
    third line prints out array of characters which is right now empty
    fourth line adds the character A to the first index in the array
    fifth line adds B to the second index
    sixth line prints out the array which has two elements right now.
    that pretty much keeps on happening.

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

    Supun Pramoda (December 8th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please solve my problem

    What about the code don't you understand? Try commenting it yourself, post the results, and we'll let you know if we agree.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  6. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Please solve my problem

    How come your the only member that posts the majority of the time?

  7. The Following User Says Thank You to Stark20 For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  8. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Please solve my problem

    I think,
    L1: It's default value of char(null character ('\0'))
    L2: first two array element when we create it in heap.
    L3: I can't explain about that..please help me.
    L4 : This is the same in L2.
    L5: second element of array object..
    L6 : I can't explain about that one also...

    please correct my answers & give your explanation....

  9. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Please solve my problem

    Quote Originally Posted by Supun Pramoda View Post
    L1: It's default value of char(null character ('\0'))
    Prints 3 null (0 value) characters. The println choosen by compiler is the println(char[] x) that correctly prints each character in the array, then a newline. Default value in char array elements is 0.

    Note that in a real console (like that in Windows), you don't see a null character, it's invisible.

    Quote Originally Posted by Supun Pramoda View Post
    L2: first two array element when we create it in heap.
    Prints characters 'A' then 'B', then a null character.

    Quote Originally Posted by Supun Pramoda View Post
    L3: I can't explain about that..please help me.
    l+" "+cr is a string concatenation, which under the hood uses StringBuffer (or StringBuilder, from Java 5). On l and cr, the compiler invokes the toString() method. l and cr are arrays, and they derive from Object. However array does not redefine the toString(), so remains the toString() from Object, that can return only something like "[I@41675ec4" that is a "reference" of the array.

    Quote Originally Posted by Supun Pramoda View Post
    L4 : This is the same in L2.
    Yes

    Quote Originally Posted by Supun Pramoda View Post
    L5: second element of array object..
    That, at that moment, is 'B'

    Quote Originally Posted by Supun Pramoda View Post
    L6 : I can't explain about that one also...
    0x12996d6 is an hexadecimal literal value. It's value, in decimal, is 19502806 (what is printed).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  10. The Following User Says Thank You to andbin For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  11. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please solve my problem

    I'm reposting the code with your explanations. I'm glad we did that, because I know I didn't understand your question until you did:
    class Example{
    	public static void main(String args[]){
    		char cr[]=new char[3];
    		int l[]=new int[3];
    		// L1: It's default value of char(null character ('\0'))
    		System.out.println(cr);	//L1
    		cr[0]='A';
    		cr[1]='B';
    		// L2: first two array element when we create it in heap.
    		System.out.println(cr);		//L2
    		l[0]=78;
    		// L3: I can't explain about that..please help me.
    		System.out.println(l+" "+cr);//L3
    		int x=cr[0];
    		int y=cr[1];
    		// L4 : This is the same in L2.
    		System.out.println(cr);//L4
    		// L5: second element of array object..
    		System.out.println(cr[1]);//L5
    		int m=0x12996d6;
    		// L6 : I can't explain about that one also...
    		System.out.println(m);	//L6
    	}
    }
    Have you tried running this to see what happens? Yes, I think that's what your question is really about.

    It's interesting you commented all of the println() statements, so I think it's not the raw code you're asking about. Instead, you want to know why the results are what they are. Is that right? Assuming so, here's my output from the code you posted:
    <blank line>
    AB
    [ I@de6f34 [ C@56ee8e // I added spaces
    AB
    B
    19502806
    I had to add spaces to the third line, because the output was being confused as code tags, so my original effort disappeared from that point. Now I'm frustrated, but I'll give you the short version:

    My answers to your assessment:
    L1: Yes, except I think both elements are being printed, not just one
    L2: The values of cr[]'s elements, A and B
    L3: These are references to l[] and cr[], a twist on the toString() method
    L4: Of course
    L5: Yes
    L6: The value assigned to m in the previous line is being printed, but that value is crazy difficult. Try simplifying that line to:

    int m = 0xFF;

    and running that. Now do you see what's happening?
    Last edited by GregBrannon; December 7th, 2013 at 05:31 AM. Reason: Half my post disappeared.

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  13. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Please solve my problem

    Is the char array must override with toString method??????why the integer array doesn't match in that concept???

  14. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please solve my problem

    Can you explain your question? What do you mean by "match in that concept?"

  15. The Following User Says Thank You to GregBrannon For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  16. #10
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Please solve my problem

    Quote Originally Posted by Supun Pramoda View Post
    Is the char array must override with toString method??????
    Arrays are implemented internally in the JVM, you can't extend or override anything about arrays.
    Arrays derive from java.lang.Object but they don't override (redefine) the toString() method. So they have the toString() inherited from Object, that can return only something like "[I@41675ec4".
    Stop, that's all.

    Quote Originally Posted by Supun Pramoda View Post
    why the integer array doesn't match in that concept???
    Where is the difference you are saying?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  17. The Following User Says Thank You to andbin For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  18. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please solve my problem

    If you want to see what was output by the println() method try this:
          ByteArrayOutputStream baos = new ByteArrayOutputStream();    //  Capture println() here
          PrintStream ps = new PrintStream(baos);
          System.setOut(ps);
    		char cr[]=new char[3];
    		int l[]=new int[3];
    		System.out.println(cr);	//L1
    		cr[0]='A';
    		cr[1]='B';
    		System.out.println(cr);		//L2
    		l[0]=78;
    		System.out.println(l+" "+cr);//L3
    		int x=cr[0];
    		int y=cr[1];
    		System.out.println(cr);//L4
    		System.out.println(cr[1]);//L5
    		int m=0x12996d6;
    		System.out.println(m);	//L6
          //  now show what was printed
          System.err.println("baos="+Arrays.toString(baos.toByteArray()));
    Here are each of the outputs (end with 13, 10)
    // baos=[0, 0, 0, 13, 10,
    65, 66, 0, 13, 10,
    91, 73, 64, 56, 55, 56, 49, 54, 100, 32, 91, 67, 64, 52, 50, 50, 101, 100, 101, 13, 10,
    65, 66, 0, 13, 10,
    66, 13, 10,
    49, 57, 53, 48, 50, 56, 48, 54, 13, 10]
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  20. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Please solve my problem

    I mean when we change L2 like this:

    l[0]=78;
    System.out.println(l);

    in that case the output is different from char array.So I want to know what's the between char array & integer array???

  21. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please solve my problem

    See what I did in post#11 to display what is printed. Change the code, execute it and copy the print out and paste it here with your questions.

    what's the between char array & integer array???
    A char uses 2 bytes, an int uses 4 bytes.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  23. #14
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Please solve my problem

    Quote Originally Posted by Supun Pramoda View Post
    I mean when we change L2 like this:

    l[0]=78;
    System.out.println(l);

    in that case the output is different from char array.
    Yes, the output is different but it doesn't depend on the intrinsic differences between a char[] and a int[].

    System.out.println(cr); invokes the println(char[]) of java.io.PrintStream. This println "knows" how to print all characters in the array.

    System.out.println(l); invokes the println(Object) of java.io.PrintStream. This println doesn't know anything particular about the object. It simply invokes toString() on the object. And toString() for arrays returns something like "[I@41675ec4" (for int[]), "[J@697eb767" (for long[]), etc...
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  24. The Following User Says Thank You to andbin For This Useful Post:

    Supun Pramoda (December 8th, 2013)

  25. #15
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Please solve my problem

    ok..I think I can got your ideas. Thanks for all of you.

Similar Threads

  1. please solve my problem
    By anarica in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 12th, 2013, 07:35 AM
  2. please solve my problem
    By joy1604 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 8th, 2013, 08:34 PM
  3. please solve my problem
    By hapini in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2012, 08:00 AM
  4. Please solve my problem
    By Nurhafizah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2012, 10:11 PM
  5. Need help to solve this problem
    By ahanf in forum Object Oriented Programming
    Replies: 1
    Last Post: June 3rd, 2012, 05:00 AM