aspose file tools*
The moose likes Beginning Java and the fly likes Wrapper Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper Class" Watch "Wrapper Class" New topic
Author

Wrapper Class

Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
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

Joined: Jun 14, 2008
Posts: 96
a double equals (==) comparison of a primitive and a wrapper first unboxes the wrapper and then compares the two primitives.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
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

Joined: Sep 14, 2008
Posts: 105
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

Joined: Jun 14, 2008
Posts: 96
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

Joined: Oct 13, 2005
Posts: 38881
    
  23
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

Joined: Sep 14, 2008
Posts: 105
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

It doesn't. Like the enhanced for-loop and generics, auto(un)boxing was introduced in Java 5.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper Class