About Question enthuware.ocpjp.v8.2.1859 :

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

Moderator: admin

Post Reply
lucian
Posts: 3
Joined: Tue Jul 05, 2016 10:44 am
Contact:

About Question enthuware.ocpjp.v8.2.1859 :

Post by lucian »

I have two questions:

1. Why can the reduce method from a Stream<Integer> can auto-unbox to a double, when it's returning an Integer ?
double sum = ls.stream().reduce(0, (a, b) -> a + b);

Code: Select all

    java.util.stream.Stream
    T reduce(T identity, BinaryOperator<T> accumulator);
    
2. Why can the sum method in IntStream return a double ?
double sum = ls.stream().mapToInt(x -> x).sum();

Code: Select all

    java.util.stream.IntStream
    int sum();
    

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

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

Post by admin »

1. It is not auto-unboxing to int. It is doing automatic widening of int to double. Remember that you don't need any cast to assign an int to double.

2. Same reason as above.
If you like our products and services, please help us by posting your review here.

RobinDRG
Posts: 6
Joined: Sat Jun 23, 2018 10:17 am
Contact:

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

Post by RobinDRG »

Hi Paul, I made the following trial, but still don't understand why the compiler gives me an error since the variables sum3 and sum4 outside the lambda expression are not even initialized

Code: Select all

double sum3;ls.stream().peek(x->{sum3=sum3+x;}).forEach(y->{});
System.out.println(sum3);
double sum4;ls.stream().forEach(a->{sum4=sum4+a;});
System.out.println(sum4);

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

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

Post by admin »

What is the error message? It will tell you exactly what is wrong.
If you like our products and services, please help us by posting your review here.

RobinDRG
Posts: 6
Joined: Sat Jun 23, 2018 10:17 am
Contact:

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

Post by RobinDRG »

I tried to just declare the variable to go one step further and try to understand why the compiler consider the variable sum outside(initialized one single time/only declared) as non-effectively final

Code: Select all

Q4_Streams.java:18: error: variable sum3 might not have been initialized
double sum3;ls.stream().peek(x->{sum3=sum3+x;}).forEach(y->{});
                                      ^
Q4_Streams.java:19: error: variable sum3 might not have been initialized
System.out.println(sum3);
                   ^
Q4_Streams.java:20: error: variable sum4 might not have been initialized
double sum4;ls.stream().forEach(a->{sum4=sum4+a;});
                                         ^
Q4_Streams.java:21: error: variable sum4 might not have been initialized
System.out.println(sum4);
                   ^
Q4_Streams.java:18: error: local variables referenced from a lambda expression must be final or effectively final
double sum3;ls.stream().peek(x->{sum3=sum3+x;}).forEach(y->{});
                                 ^
Q4_Streams.java:20: error: local variables referenced from a lambda expression must be final or effectively final
double sum4;ls.stream().forEach(a->{sum4=sum4+a;});
/code]

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

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

Post by admin »

1. Well, local variables have to be initialized explicitly. In your code, sum3 isn't initialized. What value do you think the JVM will use while computing the expression sum3=sum3+x; for the first time? This is standard OCAJP stuff :)
2. If you want to use a local variable within a lambda expression, it also has be effectively final. Which means, you can't modify its value. What do you think sum3=sum3+x; is doing?
If you like our products and services, please help us by posting your review here.

RobinDRG
Posts: 6
Joined: Sat Jun 23, 2018 10:17 am
Contact:

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

Post by RobinDRG »

Okay, many thanks. I thought that effectively-final was referring to variables that didn't change its value outside the lambda expression. I understand it now, you can't reassign the value at all

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 77 guests