About Question enthuware.ocpjp.v8.2.1565 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1565 :

Post by schchen2000 »

Code: Select all

public void openSocket(int port){

        assert port > 1000;

        ... 

}
It says
Input params of a public method should not be validated using assertions.
It's not wrong to do assert on the parameter of a public method. I ran it and found no problem.

You are simply suggesting that It's just not a good practice to do so.

Is that correct?

If so, why is it a bad idea?

Thanks.

Schmichael

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

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

Post by admin »

That is correct. It is legal but not recommended. Here is a nice explanation:
http://stackoverflow.com/questions/1383 ... ic-methods

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

Thanks for your time.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

Same problem but different question
*******************************************
3. Should not be used for side effects. For example:

public boolean doSomething(){
...    

}

public void someMethod(){

assert doSomething();

}

This is not a proper use because here the assertion is used for its side effect of calling of the doSomething() method. The only case where this could be justified is when you are trying to find out whether or not assertions are enabled in your code:

boolean enabled = false;

assert enabled = true;

if(enabled) System.out.println("Assertions are enabled")

else System.out.println("Assertions are disabled")
Could you please tell me what you were trying to explain? I don't think I follow your explanation. Many thanks.

Schmichael

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

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

Post by admin »

Can you please tell me which part is not clear?
Paul.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

This is not a proper use because here the assertion is used for its side effect of calling of the doSomething() method. The only case where this could be justified is when you are trying to find out whether or not assertions are enabled in your code:
The part it says "....the assertion is used for its side effect of calling of the doSomething() method."

What does that part mean? In fact, I really did not follow the explanation.

"....to find out whether or not assertions are enabled in your code"????

How do we enable assertions in our code? To the best of my knowledge, assertions are disabled or enabled from the command prompt when the program is run, i.e. using -ea or lack thereof in order to enable or disable the assertion respectively from the command line on a terminal.

Thanks.

Schmichael

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

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

Post by admin »

schchen2000 wrote:
This is not a proper use because here the assertion is used for its side effect of calling of the doSomething() method. The only case where this could be justified is when you are trying to find out whether or not assertions are enabled in your code:
The part it says "....the assertion is used for its side effect of calling of the doSomething() method."

What does that part mean? In fact, I really did not follow the explanation.
Side effect simply means that you are killing two birds with one stone. You want to achieve one thing by an assert statement but while doing so, you are also achieving something else. This will work only if assertions are enabled. Explained here: https://docs.oracle.com/javase/7/docs/t ... ssert.html
"....to find out whether or not assertions are enabled in your code"????

How do we enable assertions in our code? To the best of my knowledge, assertions are disabled or enabled from the command prompt when the program is run, i.e. using -ea or lack thereof in order to enable or disable the assertion respectively from the command line on a terminal.

Thanks.

Schmichael
It is not talking about enabling or disabling assertions in the code. It is talking about checking whether they are enabled or disabled in the code. They are indeed enabled/disabled from the command line but you can check i.e. find out within your code.

HTH,
Paul.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

Thank you, Paul.

Schmichael

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

I understand the side-effects thing but just because you called the method doSomething does not mean it has side-effects.
Any (actually every) method is just refactored code that could exist in-place.So in that sense there is nothing magical or special about what's in a method.
doSomething could be "check the time; this code should not be running after 8am"
That is not a side-effect.
I appreciate that you are toughening us up for the exam, and you know a hell of a lot more than we do about what's actually on it, but , ah.. just saying.

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

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

Post by admin »

Invoking a method itself is the side effect of this assertion. That's a problem.
Whether that method checks time or inserts a row in the db is a different matter altogether.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests