Page 1 of 1

About Question enthuware.ocpjp.v11.2.3396 :

Posted: Thu Apr 08, 2021 9:29 pm
by alfredo.zuloaga
I have a question., Does TYPE_USE can be applied like
var str = (@NonNull String) "";
and also like
@NonNull String str = "";

?

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Thu Apr 08, 2021 10:27 pm
by admin
>var str = (@NonNull String) "";
No.
>@NonNull String str = "";
Yes.

But I suggest you to actually try it out by writing a simple test program.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Sun Apr 18, 2021 2:42 am
by 61d14837
Option 6, explanation says that this is valid
Function<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val);
but that doesn't work unless @Target is updated to include ElementType.PARAMETER. This question instead uses ElementType.TYPE_PARAMETER which can be used on parameterized types, generic declarations

In response to the original poster, these both compile

Code: Select all

var str = (@NonNull String) "";
@NonNull String str2 = "";

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Wed Jan 10, 2024 9:34 am
by hsnclk
61d14837 wrote:
Sun Apr 18, 2021 2:42 am
Option 6, explanation says that this is valid
Function<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val);
but that doesn't work unless @Target is updated to include ElementType.PARAMETER. This question instead uses ElementType.TYPE_PARAMETER which can be used on parameterized types, generic declarations

In response to the original poster, these both compile

Code: Select all

var str = (@NonNull String) "";
@NonNull String str2 = "";
I aggre with "61d14837", the question requires to be updated. ElementType.TYPE_PARAMETER has to be changed with ElementType.PARAMETER. I mean, in order for the explanation in part "E" to be fulfilled, this must be done.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Wed Jan 10, 2024 11:14 am
by admin
Not sure why you think so because both the examples given in explanation of option 6 compile fine (without any change in the problem statement):
Function<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val); //compiles fine
//or
Function<Integer, String> f = ( @NonNull Integer val ) -> Integer.toHexString(val); //also compiles fine

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Wed Jan 10, 2024 11:44 am
by hsnclk
Actually, I had tried to create the @NonNull annotation myself, I had not used Spring one.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Wed Jan 10, 2024 11:58 am
by admin
You may put the annotation code given in the problem statement in a java file and then use it in another java class. It compiles fine.
It has nothing to do with Spring.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Wed Jan 10, 2024 12:18 pm
by hsnclk
Screenshot 2024-01-10 at 20.03.21.png
Screenshot 2024-01-10 at 20.03.21.png (148.72 KiB) Viewed 2329 times
Screenshot 2024-01-10 at 20.02.41.png
Screenshot 2024-01-10 at 20.02.41.png (149.83 KiB) Viewed 2329 times
Either I misunderstood something, or we are talking about different things. In the second image I added also ElementType.PARAMETER.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Thu Jan 11, 2024 1:58 am
by admin
I tested the following code (as given in the problem statement and the explanation):

Code: Select all

import static java.lang.annotation.ElementType.*;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.util.function.*;

@Retention(value=RUNTIME)
@Target(value={TYPE_USE,TYPE_PARAMETER})
@interface NonNull{    
}
public class TestClass {
    public static void main(String[] args) {

        Function<Integer, String> f2 = ( @NonNull var val ) -> Integer.toHexString(val);
         //or 
        Function<Integer, String> f3 = ( @NonNull Integer val ) -> Integer.toHexString(val);
        System.out.println(f2.apply(10));
    }
}

It compiles and runs fine.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Sat Jan 13, 2024 12:04 pm
by hsnclk
I don't understand my problem.
Screenshot 2024-01-13 at 20.33.22.png
Screenshot 2024-01-13 at 20.33.22.png (149.84 KiB) Viewed 2091 times
Screenshot 2024-01-13 at 20.33.39.png
Screenshot 2024-01-13 at 20.33.39.png (252.88 KiB) Viewed 2091 times
I copied and pasted your code.. The result was like this.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Sat Jan 13, 2024 8:54 pm
by admin
What is your JDK version?
Try compiling it from command line and post the error message.

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Sat Jan 13, 2024 9:36 pm
by hsnclk
openjdk-17 version17.0.1

by the way it compiled. but still looks like an error. I think it is an IntelliJ idea problem. thanks

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Sun Jan 14, 2024 12:22 am
by admin
Well, let's put it in the list of reasons to avoid IDEs while preparing for the certification exam :D

Re: About Question enthuware.ocpjp.v11.2.3396 :

Posted: Sun Jan 14, 2024 7:20 am
by hsnclk
You're right...