Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

array junk ??

  Asked By: Rani    Date: Nov 06    Category: Java    Views: 1200
  

My teacher
always used to say that "there is no such thing called
a dumb question " and hence i post these queries.

Yesterday i tried arrays and found something
interesting and unlogical .

below is the program.

public class test1 {

public static void main(String[] args) {
int a[];
a=new int[2];
System.out.print(a);


}
}

when i print just a i get a junk value : [I@77d134

but when i print a[0] or a[1] i get 0.


My question is

What is this junk value ? is it the junk in the
memory location of a ? if so why should java allocate
3 memory locations one for a one for a[0] and one for
a[1] when i tell the jvm to allocate just 2 locations
of memory ?

Share: 

 

11 Answers Found

 
Answer #1    Answered By: Erin Dunn     Answered On: Nov 06

all that calling System.out.print on an object will do is print  the
toString() method of the object.
The default behaviour of toString() (from Object) is to give you the
handle of the object.
What you are seeing is the handle (or pointer except that java  doesn't
have pointers) to
the memory  that the array  occupies. When you give it a[0] it is showing
an int  and the default
is to print those directly - which means you get the value output.

 
Answer #2    Answered By: Ana Bradley     Answered On: Nov 06

The junk is the memory  address of the array  a. The memory is only
allocated for 2 locations a[0] and a[1]. The memory allocated for the
array a is internal and you cannot
specify that .

 
Answer #3    Answered By: Bernard Gutierrez     Answered On: Nov 06

public class  test1 {

public static  void main(String[] args) {
int a[];
a=new int[2];
[Arman Melkumyan] you vahe not initialized these a[0] and a[1]
elements, that is why you get a ‘0’ value.
In java, you get always ‘0’ default value for “int” type like you have
a default “null” for non initialized objects.

You can also write : a = new int[] { 26 , 33 };

System.out.print(a);


}
}

when i print  just a i get a junk value : [I@77d134
[Arman Melkumyan] junk is a location  of a


but when i print a[0] or a[1] i get 0.


My question  is

What is this junk value ? is it the junk in the
memory location of a ? if so why should java  allocate
memory  locations one for a one for a[0] and one for
a[1] when i tell the jvm  to allocate  just 2 locations
of memory ?

 
Answer #4    Answered By: Vilhelm Fischer     Answered On: Nov 06

The junk value is the hashCode of the object.
refer to toString method in Object class  in Java API.

 
Answer #5    Answered By: Kyle Fox     Answered On: Nov 06

can u tell me whats this hashcode..?

 
Answer #6    Answered By: Leonard Pierce     Answered On: Nov 06

every object in java  maintains a hashcode. this is just an integer.
java uses this code for efficient searchig and sorting algorithms.
By default the hashcode of the object will be the memory  address of the objcet.
this default value in not helpful.
Pls refer to hashcode and equals method Object class.

 
Answer #7    Answered By: Elisabeth Bell     Answered On: Nov 06

The value you got while printing a is not a junk. The part before the '@'
cahracter represents the class  to which 'a' belongs, since it is an integer you
get '[I' and the remaining part after '@' is the address of the array... you can
verify this, by creating an instance of a class and printing it... consider that
your object is 'ob' of class 'A' then you'll get the output of
System.out.println(ob) as A@(address of a)..

 
Answer #8    Answered By: Midissia Lopez     Answered On: Nov 06

what ever prints after @ is not the address of the object. It is the hash code
of the object. reffer toString method of Object class  java API

 
Answer #9    Answered By: Sebastien Anderson     Answered On: Nov 06

I think you haven't properly worked with the objects. When you create a
instance of a class  and print  it it gives the address of the object, and if you
want to see the hash code then you have to use the object.hashCode() method..
you can try this out

create an object of a class simply print it and print the object.hashCode()
value both will be different...

 
Answer #10    Answered By: Hu Chalthoum     Answered On: Nov 06

When you create a instance of a class  and print  it it gives the address of
the object,

>>>> you are absolutely wrong .... no addresses buddy it gives the hash code
........


and if you want to see the hash code then you have to use the
object.hashCode() method..

>>>> well this will definately give the hash code ...........


you can try this out create an object of a class simply print it and print
the object.hashCode() value both will be different...


>>>>> definitely they will be different but just print
Integer.toHexString(object.hashCode()) ... they will not be different ...
provided that toString() is not over ridden ......

also try printing

System.out.println(object);

and

System.out.println(object.getClass().getName() + '@' +
Integer.toHexString(object.hashCode()));

once again that toString() is not over ridden ......

 
Answer #11    Answered By: Danny Perkins     Answered On: Nov 06

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())

The above is the direct copy and paste from the java  API.

If you create an object of someother class, the overloaded hashcode method is
called. Create an Object of class Object. (ex Object ob = new Object();) now
call hashcode on this and just print  it. one will be printed in decimal and
other will be in hex. to change decimal to hex use
Integer.toHexString(ob.hashCode()). see the value and mail me back.

But in fact, the hashCode method in Object class returns the memory location  of
the object.

 
Didn't find what you were looking for? Find more on array junk ?? Or get search suggestion and latest updates.




Tagged: