• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Class

 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just wanna know why the following programme giving the value true at runtime?

'cause as far as I'm concerned i, which is a reference of Integer classs, pointing here a new object, and i1 is pointing a object on integer pool. So,why the result can be true?
thanks in advance for help.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a double equals (==) comparison of a primitive and a wrapper first unboxes the wrapper and then compares the two primitives.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it in Java 1.4 and see what happens:
javac Wrapper.java
[Campbell@queeg applications]$ java Wrapper
true
[Campbell@queeg applications]$ javac -source 1.4 Wrapper.java

That was before boxing and unboxing were introduced.
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thanks for that, I ve one more question. I wanna know why the following programme giving me the output 00030?

I'm confused here
for(int i:arr)
{

arr[i]=0;

}
Is not i here referring to value 1, which is the index number of the array, and at the end of loop, i referring to 4, which result in IndexOutOfBound Exception. And why the answer was 0030?
for(int i:arr)
{
System.out.println(i);
}

'cause here it is reading back the value from the array index, and I've found no value stored at index 2. So why the result was 0030?
Thanks in advance for help.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a tough question - I've seen it before somewhere. Where did you get it from?

The for(int i:arr) syntax iterates over the array assigning the value of the element at the current array position to the variable i.

First iteration of the loop (index 0), i=1 so element 1 in the array is set to 0.
1,0,3,4

Second iteration of the loop (index 1), i now equals zero so the element 0 in the array is set to 0.
0,0,3,4

Third iteration of the loop (index 2), i now equals 3 so the element 3 in the array is set to 0.
0,0,3,0

Fourth iteration of the loop (index 3), i now equals 0 so the element 0 in the array (already 0) is set to 0.
0,0,3,0

So as you can see, the variable i will never equal 4.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pawan Arora:
Hi,
thanks for that, I ve one more question.


Fascinating question, as Paul Beckett has told you, but it is independent of the original question, so might have been better in a new thread. Please maintain indentation inside the code tags; that is what they are designed for.

And what happened when you compiled your original program with the -source 1.4 options?
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:

And what happened when you compiled your original program with the -source 1.4 options?


It's was giving me compile-time error. Is that mean autoboxing doesn't exist in java 1.4?
[ October 15, 2008: Message edited by: Pawan Arora ]
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't. Like the enhanced for-loop and generics, auto(un)boxing was introduced in Java 5.
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic