This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why doesn't this fail at compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why doesn Watch "why doesn New topic
Author

why doesn't this fail at compile?

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95




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

Joined: Sep 13, 2007
Posts: 2563
    
    9

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
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

Joined: Oct 24, 2012
Posts: 95
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

Joined: Sep 13, 2007
Posts: 2563
    
    9

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

Joined: Oct 24, 2012
Posts: 95
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!
 
 
subject: why doesn't this fail at compile?