• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

assertion

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Assert{
private Boolean isvalid(int i)
{
if(i<1 || i>12)
return new Boolean("Incorrect value");
else
return new Boolean(true);
}

void testvalue(int val)
{
assert isvalid(val):"out of range value"; //1
}
public static void main(String args[])
{
Assert test=new Assert();
test.testvalue(12);
}
}


it compiles even when asserions are enabled but it should not as at 1 boolean expression is required not Boolean type. can anyone help me out.. why it is compiling?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works because of auto boxing. Read auto-boxing and unboxing.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yogvinder is correct. The return from isvalid() is auto(un)boxed. Furthermore assertions can only be "enabled" at runtime. You will not get assertion errors at compile time. However the compiler will complain if the argument to assert() is not boolean/Boolean (assuming you are using JDK 1.4 upwards).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic