• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

is this effectively final?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Came across a mock certification question:

Given:


is the following a valid way to compute the sum of integers correctly?



The answer said, it is incorrect since "sum" variable is not final or effectively final.

I am confused here, since sum is declared and initialized just once, and the value isnt changed. Is "sum" not effectively final?

Can someone please help me understand?


Thanks,
Preethi
 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Preethi Vallam wrote:and the value isnt changed


This changes the value.
 
Preethi Vallam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had thought that it was ok to change the value inside. So, was confused. Thank you for the quick reply!
 
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using doubles when you are adding integers? At least you didn't declare the result as type Double.
You can probably replace that λ with a method reference; there is a method in the Integer class which probably does what you want. Or change the λ to something like (i, j) -> i + j.
 
Saloon Keeper
Posts: 1317
40
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While something similar to this would also work, and be a much more recommended way to write the code:


or even:
jshell> int[] arr = new int[] { 1, 2, 3};
arr ==> int[3] { 1, 2, 3 }
jshell> java.util.Arrays.stream( arr ).sum();
$5 ==> 6


But the point of this question is to see if you recognize what effectively final means.

Perhaps this should have been in the OCJP or OCA forums, however, it is important for those taking the Certification Exams to note, that no local variable marked as final is effectively final.

That is, lambdas can capture final local variables and effectively final ones, but we don't count the ones marked final as effectively final.

The JLS and the Cert Exams are fussy about that point.
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic