About Question enthuware.ocpjp.v11.2.3396 :
Moderator: admin
-
- Posts: 10
- Joined: Fri Nov 18, 2016 5:48 pm
- Contact:
About Question enthuware.ocpjp.v11.2.3396 :
I have a question., Does TYPE_USE can be applied like
var str = (@NonNull String) "";
and also like
@NonNull String str = "";
?
var str = (@NonNull String) "";
and also like
@NonNull String str = "";
?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
>var str = (@NonNull String) "";
No.
>@NonNull String str = "";
Yes.
But I suggest you to actually try it out by writing a simple test program.
No.
>@NonNull String str = "";
Yes.
But I suggest you to actually try it out by writing a simple test program.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Tue Jul 21, 2020 1:39 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
Option 6, explanation says that this is valid
In response to the original poster, these both compile
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 declarationsFunction<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val);
In response to the original poster, these both compile
Code: Select all
var str = (@NonNull String) "";
@NonNull String str2 = "";
-
- Posts: 16
- Joined: Sat Dec 16, 2023 7:22 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
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.61d14837 wrote: ↑Sun Apr 18, 2021 2:42 amOption 6, explanation says that this is validbut 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 declarationsFunction<Integer, String> f = ( @NonNull var val ) -> Integer.toHexString(val);
In response to the original poster, these both compileCode: Select all
var str = (@NonNull String) ""; @NonNull String str2 = "";
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
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
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
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Sat Dec 16, 2023 7:22 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
Actually, I had tried to create the @NonNull annotation myself, I had not used Spring one.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
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.
It has nothing to do with Spring.
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Sat Dec 16, 2023 7:22 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
Either I misunderstood something, or we are talking about different things. In the second image I added also ElementType.PARAMETER.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
I tested the following code (as given in the problem statement and the explanation):
It compiles and runs fine.
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));
}
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Sat Dec 16, 2023 7:22 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
I don't understand my problem.
I copied and pasted your code.. The result was like this.
I copied and pasted your code.. The result was like this.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
What is your JDK version?
Try compiling it from command line and post the error message.
Try compiling it from command line and post the error message.
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Sat Dec 16, 2023 7:22 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
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
by the way it compiled. but still looks like an error. I think it is an IntelliJ idea problem. thanks
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
Well, let's put it in the list of reasons to avoid IDEs while preparing for the certification exam
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Sat Dec 16, 2023 7:22 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3396 :
You're right...
Who is online
Users browsing this forum: Bing [Bot] and 107 guests