Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why doesn't this fail at compile?

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Why won't the compiler complain that the bar() returns an int instead of the String that is required in the assert statement? This apparently compiles and runs just fine (since the assert is never thrown).
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assertion is disabled during runtime by default. You will need to use the -ea flag with java command. With assertion enabled, bar() will execute.

Oh since assertion is a feature with Java 1.4, try compiling the file with -source 1.4.
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote:



Why won't the compiler complain that the bar() returns an int instead of the String that is required in the assert statement? This apparently compiles and runs just fine (since the assert is never thrown).


Question... where did you read / learn that the second parameter must be a string? ... because as you just pointed out, that is not true.

Henry
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:assertion is disabled during runtime by default. You will need to use the -ea flag with java command. With assertion enabled, bar() will execute.


Well, assume that it has been compiled WITH THE -ea option. Also, t = 2. So I still do not understand why the compiler doesn't complain that the bar() function should return a String.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AHHH! THANK YOU! I understand. Returning an int is fine, since it will be implicitly converted to a String when displayed.

However, if the function had a void return type, then compilation would fail, correct?
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote:
K. Tsang wrote:assertion is disabled during runtime by default. You will need to use the -ea flag with java command. With assertion enabled, bar() will execute.


Well, assume that it has been compiled WITH THE -ea option. Also, t = 2. So I still do not understand why the compiler doesn't complain that the bar() function should return a String.


The assert statement is "assert expr1 : expr2" where expr1 is a boolean and expr2 is expression returning a value.

Now returning a value can be a String, an int or even an Object. Don't know why you expect it to be a string.

Check out the link http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
Rachel Glenn wrote:
K. Tsang wrote:assertion is disabled during runtime by default. You will need to use the -ea flag with java command. With assertion enabled, bar() will execute.


Well, assume that it has been compiled WITH THE -ea option. Also, t = 2. So I still do not understand why the compiler doesn't complain that the bar() function should return a String.


The assert statement is "assert expr1 : expr2" where expr1 is a boolean and expr2 is expression returning a value.

Now returning a value can be a String, an int or even an Object. Don't know why you expect it to be a string.

Check out the link http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html


You are correct!! I was wrong in thinking expr2 had to return a string. thanks for clarifying this for me!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic