File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes doubt in the output. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "doubt in the output." Watch "doubt in the output." New topic
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: 38363
    
  23
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
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

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: 11230
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

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: 38363
    
  23
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

Joined: Oct 01, 2001
Posts: 2848
    
  11

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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubt in the output.