Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it legal for an assertion to create a new Class?

 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Assuming all other things are correct, will this cause the compilation for the code to fail?



If not, why not?

Thanks :-)
 
John Stark
Ranch Hand
Posts: 185
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression after the ':' is used to print a string to the screen and must evaluate to some value. So doing

with

does not compile.

compiles fine. Running it with asertions enabled will give something like 'java.lang.AssertionError: A@fjdhs' depending on the Car toString() method.

John
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just to be picky (you'll find that's quite common round here, but then, programming is all about attention to detail ), you aren't creating a new class there. You're creating a new instance of a class.
 
Tim Moores
Bartender
Pie
Posts: 2496
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm perennially bemused by this kind of question that can be answered with the help of a compiler a LOT quicker than posting here and waiting for an answer.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of assertions it's important to ask two questions:

- Is it legal? - which of course can be answered by the complier
- Is it appropriate, according to Sub / Oracle standards?

The "appropriate" question might be on the real exam.
 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:I'm perennially bemused by this kind of question that can be answered with the help of a compiler a LOT quicker than posting here and waiting for an answer.


Good lad Tim. Sorry to hear about your bemusement. Its just that the compiler cannot tell me if there is an error in the book or not. In order to cure you bemusement, I suggest you do a Google image search for lolcats.
 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, thanks for your reply. I suppose I should have posted the question in full instead of making my own variation...


The book states that compilation fails due to "assert(j==12):doStuff();" as it does not return a value. The book does not indicate that they're is anything wrong with the line after which is "assert(j==12):new Clumsy();".

As far as I can tell, "assert(j==12):new Clumsy();" will not return a value either. Why does the compiler not complain about this line?

Thanks.
 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:And just to be picky (you'll find that's quite common round here, but then, programming is all about attention to detail ), you aren't creating a new class there. You're creating a new instance of a class.


thanks Matthew, I was wondering how to phrase the question when I initially typed it ;-)
 
John Stark
Ranch Hand
Posts: 185
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell, "assert(j==12):new Clumsy();" will not return a value either. Why does the compiler not complain about this line?

Hm, yes. The compiler wants something that gives a string which can be printed out together with the assertion error. So I guess the toString() method of the created Clumsy object is called somehow. Like in System.out.println() where you can do

but you cannot do

with

John
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glen Iris wrote:As far as I can tell, "assert(j==12):new Clumsy();" will not return a value either. Why does the compiler not complain about this line?

It does have a value - the new Clumsy object. It's not a String, but that's not important - it will accept any value there. John's example demonstrates the difference quite nicely.
 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense, thanks Johan and Matthew
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic