Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in the output.

 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Array
{
public static void main(String d[])
{
char ch[]={'s','u','n'};
System.out.println(ch); //output: sun
System.out.println("result="+ch); //output: result=[C@1fb6458
}
}

why in the 2nd output, it gives the address of character array ?

And if i want the output : result= sun
then what is the code for it ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you are getting the address of that array? For the explanation, start here.
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks in advance .
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why do you think you are getting the address of that array? For the explanation, start here.


Because when i run it, the out put was as above.
In java when any variable is concatenated with String then the variable is treated as character and the result is a new string object.
e.g.
String s=new String("34");
System.out.println(1+1+s); output : 234

similarly the 2nd output in my program should be: Result= sun
but after running it the output was : result= [C@q4e3rq4
why it is printing the address of the character array, instead of character sequence ?

And what is the code for getting the the output:
Result= sun ?

please help..
 
Greg Charles
Sheriff
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true that when a primitive, like your 1+1 int expression, gets concatenated to a String, it is first converted to a String representation its value. For an object type though, its toString() method is called to convert it to a String.

In Java, arrays are objects with the base class Object, so when you concatenate an array to a String, its toString() method as called. For arrays, all that does it print out the reference id for the array. If you want a different String representation of the array, then you have write that yourself. (Hint: the String class has a constructor taking a character array parameter. You might try that out.)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe:

When you have

the toString method is called on the object. In this case, the object is an array. So, the toString() method of an array returns a letter that indicates the array type ('C' for character in this case) followed by an 'at' sign, followed by the results of the hashCode() call on the array. It is NOT the address of the object.

now...when you call

the println() method is most likely overloaded. That means the JVM can pick which one it thinks is best. If you check the API, you'll find this:


println

public void println(char[] x)
Print an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().
Parameters:
x - an array of chars to print.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soumya Padhiary wrote:Because when i run it, the out put was as above.

but after running it the output was : result= [C@q4e3rq4
why it is printing the address of the character array, instead of character sequence ?

But that is not the address of the character array, what's after the @ is the hash code (in hexadecimal).
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:It's true that when a primitive, like your 1+1 int expression, gets concatenated to a String, it is first converted to a String representation its value. For an object type though, its toString() method is called to convert it to a String.

In Java, arrays are objects with the base class Object, so when you concatenate an array to a String, its toString() method as called. For arrays, all that does it print out the reference id for the array. If you want a different String representation of the array, then you have write that yourself. (Hint: the String class has a constructor taking a character array parameter. You might try that out.)


Yes .. i got it.
i have to use the constructor of String class and convert the character sequence into Strint, i.e.
String(char[] ch)

thank you Greg and Fred.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is one way to do it. If you look in the java.util.Arrays class you will find methods which do something similar.
 
Greg Charles
Sheriff
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is one way to do it. If you look in the java.util.Arrays class you will find methods which do something similar.


Huh. Well now I've learned something!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic