About Question enthuware.ocpjp.v8.2.1342 :

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

Post Reply
mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

About Question enthuware.ocpjp.v8.2.1342 :

Post by mrmuiz »

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:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by admin »

No, "by default" is the key part here.

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by mrmuiz »

But in Java 1.4 assert is a keyword

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by admin »

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:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by admin »

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:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by mrmuiz »

I was missing this point, tanks for clarifications!

sahoo007tm
Posts: 9
Joined: Sat Jun 18, 2016 12:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by sahoo007tm »

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:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by admin »

I am not sure I understand your point.
Paul

sahoo007tm
Posts: 9
Joined: Sat Jun 18, 2016 12:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by sahoo007tm »

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:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by admin »

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:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by badbishop »

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:

Re: About Question enthuware.ocpjp.v8.2.1342 :

Post by admin »

Yes, added. Although as noted in the explanation -source switch is not needed.
thank you for your feedback!
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests