Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

assertion

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Works because of auto boxing. Read auto-boxing and unboxing.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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).
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic