• 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

another assert question

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
static String returnString(){
return "x is greater than 0";
}

public static void main (String[] args){
int x = 1;
assert x> 0 : returnString();
System.out.println("Done")
}
}

How come the method returnString is called? I thought you can only use strings after the colon.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Francis,

It's very legal to use any thing that returns a value in the assert statement.

So, the code you provided is perfectly legal, as the method returns a String object.

btw, all these are legal :

assert x> 0 : 6; // 6 is a value
assert x> 0 : "A String" // a string
assert x> 0 : returnString(); // a method that returns an object

assert x> 0 : voidMethod(); // WRONG, no value is returned

assert x> 0 : System.out.println(" whatever " ); // Another Illegal statement, nothing returned.

Hope you got the idea.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After the colon is an expression. Whatever is represented (returned) by that expression is passed to the constructor of the AssertionError. If you review the API for AssertionError, you'll see that the constructor is overloaded to accept numerous primitive types, as well as Object. This parameter is converted to String for the error message.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second expression after the colon can be anything which results in a value. It is like system.out.println() where you can pass in a primitive or an object and it will conver to a String representation.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys

To summarize what I put after a colon has to return a value(ie. values of variables, method return), hench void methods cannot work and since System.out.println("") does not return anything also it cannot be used as well. Correct?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Francis Palattao:
...void methods cannot work and since System.out.println("") does not return anything also it cannot be used as well. Correct?


Correct. An expression that returns void cannot be used here.
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic