• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Why said undetermined output?

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


01: public class Test {
02: public static void main(String[] args) {
03: Integer i = 700;
04: Integer j = rebox(i);
05: System.out.println(i == j);
06: }
07:
08: public static Integer rebox(int i) {
09: return i;
10: }
11: }



Given answer on that site:
The output is unspecified - it may be "true" or "false".


Source: JavaBlackBelt

Please tell is that correct! When I run this code, I get false obviously!



Regards,
cmbhatt
 
Ranch Hand
Posts: 37
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly it will gives false.see the code below.


public class Test {
public static void main(String[] args) {
Integer i = new Integer(700);
Integer j = rebox(i);
System.out.println(i == j);
}
public static Integer rebox(Integer i) {
return i;
}
}
Now you can understand the change.and where you have done the mistake.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops , why you assume its false always ?




since i & j refer to the same vaue in the constant bool its true
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eisa,

Constant Pool range for ints -128 to 127.

How come 700 will be pooled?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ramu,

But you see the code below too



Obvious result "true"
one is primitive and another is wrapper. wrapper is unboxed to primitive
to be compared, that is why yields "true" if equal.




Regards,
cmbhatt
 
Ramu Valivarthi
Ranch Hand
Posts: 37
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eisa
The code what you have given its won't work.
public class Test {
public static void main(String[] args) {
Integer i = 700;
Integer j = rebox(i);
System.out.println(i == j);
}
public static Integer rebox(int i) {
return i;
}
}
public class Test {
public static void main(String[] args) {
Integer i = 700;
Integer j = rebox(i);
System.out.println(i == j);
}
public static Integer rebox(int i) {
return i;
}
}
it will give incompatible types
int and java.lang.Integer
 
Ramu Valivarthi
Ranch Hand
Posts: 37
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra Still its not work,the same problem it will give.
How you are passing object value to primitive.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Ramu

Check the version of jdk you are using.

I think it is may be 1.4
[ April 24, 2007: Message edited by: anil kumar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which version of Java are using?
Autoboxing is added to Java 5.0 and it is on the exam 310-055 list;



both the lines give compiler error in Java 1.4, but ok with Java 1.5.


Regards,
cmbhatt
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic