• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignment Operator and wrappers

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


How come in the above code Boolean and "(b1 = false)" are being accepted ? Please explain . I was expecting a equals operator in place of '='
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes its possible. All you need to remember the result of an expression within if, while, do while, for constructs must be a boolean.
Hence if you have
boolean b1 = false;
if((b1=true)) {System.out.print("Test");}
Here b1 will be assigned false and the output will be Test.
Also with Java 1.5
Boolean b1 = false;
if((b1=true)) {System.out.print("Test");}
will also work, Here b1 will be autoboxed to false during initialization and in the if statment true will be assigned to b1 via autoboxing and then its unboxed to boolean true and the result is "Test".
Hope this clears.
Thanks
Deepak
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siddu,

This is valid because of Java 1.5's autoboxing feature. Essentially, the compiler will automatically convert wrapper objects (Integer, Double, Boolean, etc) to their corresponding primitives (int, double, boolean) in most places where they're used. The same holds for converting primitives to wrapper objects.
reply
    Bookmark Topic Watch Topic
  • New Topic