All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
Moderators: Site Manager , fjwalraven
mrmuiz
Posts: 49 Joined: Mon Jul 27, 2015 4:34 am
Contact:
Post
by mrmuiz » Wed Dec 16, 2015 9:55 am
Explanation says
From Java 1.5 onwards, assert is treated as a keyword by default (as opposed to Java 1.4 version where it is treated as an identifier)
shouldn't it be
From Java 1.4 onwards, [...] (as opposed to Java 1.3 version [...]
instead?
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Wed Dec 16, 2015 10:45 am
No, "by default" is the key part here.
mrmuiz
Posts: 49 Joined: Mon Jul 27, 2015 4:34 am
Contact:
Post
by mrmuiz » Wed Dec 16, 2015 10:56 am
But in Java 1.4 assert is a keyword
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Wed Dec 16, 2015 10:57 am
It is. But it is not treated as a keyword "by default".
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Wed Dec 16, 2015 11:00 am
As per :
https://docs.oracle.com/javase/8/docs/t ... ssert.html
Unless you specifically request source mode 1.4 with the -source 1.4 flag, the compiler operates in source mode 1.3. If you forget to use this flag, programs that use the new assert statement will not compile. Having the compiler use the old semantics as its default behavior (that is, allowing assert to be used as an identifier) was done for maximal source compatibility. Source mode 1.3 is likely to be phased out over time.
mrmuiz
Posts: 49 Joined: Mon Jul 27, 2015 4:34 am
Contact:
Post
by mrmuiz » Wed Dec 16, 2015 11:00 am
I was missing this point, tanks for clarifications!
sahoo007tm
Posts: 9 Joined: Sat Jun 18, 2016 12:44 am
Contact:
Post
by sahoo007tm » Thu Jun 23, 2016 3:51 am
The question says
"You have just written a Java class named AssertTest.java that uses the assertion facility."
It does not mention anywhere that the code contains "assert" as an identifier in it.
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Thu Jun 23, 2016 8:22 am
I am not sure I understand your point.
Paul
sahoo007tm
Posts: 9 Joined: Sat Jun 18, 2016 12:44 am
Contact:
Post
by sahoo007tm » Thu Jun 23, 2016 8:27 pm
When I read this question I think possible content of AssertTest.java as following.
Do we need to provide the -source option while compiling below class to be able to use the assert?
public class AssertTest{
public static void main(String[] args){
int i=1;
assert i==1: "blah..";
}
}
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Thu Jun 23, 2016 8:40 pm
No, you don't need source option for java 1.4 and above. You should try it out.
badbishop
Posts: 27 Joined: Fri Jul 22, 2016 9:14 am
Contact:
Post
by badbishop » Sun Jul 31, 2016 7:18 am
Shouldn't the list of correct answers be updated or generalized? The correct answers end up at 1.6 currently.
Code: Select all
:~$ javac -source 1.8 asserttest/AssertTest.java
:~$ java -ea -cp /Users/tom asserttest.AssertTest
Exception in thread "main" java.lang.AssertionError
at asserttest.AssertTest.main(AssertTest.java:6)
admin
Site Admin
Posts: 10388 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Sun Jul 31, 2016 11:03 am
Yes, added. Although as noted in the explanation -source switch is not needed.
thank you for your feedback!
Paul.
Users browsing this forum: No registered users and 4 guests