4 Replies - 269 Views - Last Post: 15 October 2012 - 10:24 PM Rate Topic: -----

#1 wannabee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 12

Cant understand the output i have got!

Posted 13 October 2012 - 04:39 AM

Hello! I have written a following code lately in java, and i got rather some odd out put. Can any one please explain? Here is the code:
import java.util.*;

public class Challenge1
{
 public static void main(String args[])
 {
 Scanner in = new Scanner(System.in);
 System.out.println("enter the data");
 int x[] = new int[5];
 x[0] = in.nextInt();
 System.out.println(x);
 }
}



OutPut: [I@14318bb
I know that using
System.out.print(x[0])
would give me the required output, but what does the above output mean?

Is This A Good Question/Topic? 0
  • +

Replies To: Cant understand the output i have got!

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Cant understand the output i have got!

Posted 13 October 2012 - 04:43 AM

You are just printing out the reference to the array in memory so you just get the hashcode of the object.
Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Cant understand the output i have got!

Posted 13 October 2012 - 04:44 AM

It shows you the toString() implementation of Object

They describe the method pretty good in the API

Quote

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())

Was This Post Helpful? 1
  • +
  • -

#4 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Cant understand the output i have got!

Posted 13 October 2012 - 06:11 AM

So in short, if you want to print out an array, use a for-loop or use a premade method like "Arrays.toString(array);"
Was This Post Helpful? 1
  • +
  • -

#5 wannabee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 12

Re: Cant understand the output i have got!

Posted 15 October 2012 - 10:24 PM

Thank you guys! That was very helpful! thank you very much! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1