| Author |
doubt in the output.
|
Soumya Padhiary
Greenhorn
Joined: Jan 10, 2013
Posts: 20
|
|
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
Joined: Oct 13, 2005
Posts: 32833
|
|
|
Why do you think you are getting the address of that array? For the explanation, start here.
|
 |
Soumya Padhiary
Greenhorn
Joined: Jan 10, 2013
Posts: 20
|
|
|
thanks in advance .
|
 |
Soumya Padhiary
Greenhorn
Joined: Jan 10, 2013
Posts: 20
|
|
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
Bartender
Joined: Oct 01, 2001
Posts: 2550
|
|
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
Joined: Oct 02, 2003
Posts: 10043
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12956
|
|
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).
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Soumya Padhiary
Greenhorn
Joined: Jan 10, 2013
Posts: 20
|
|
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
Joined: Oct 13, 2005
Posts: 32833
|
|
|
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
Bartender
Joined: Oct 01, 2001
Posts: 2550
|
|
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!
|
 |
 |
|
|
subject: doubt in the output.
|
|
|