effectivejava | Run queries on your Java code
kandi X-RAY | effectivejava Summary
kandi X-RAY | effectivejava Summary
Effective java is a tool to examine your Java codebase. You can use it in three different ways: * as a Java linter. Just tells it which directory you want to examine, it will spit out a set of warnings and suggestions to improve your code * run queries from the command line. It could tell you which type of Singleton you are using in your codebase or how many constructors have 10 or more parameters. This modality can be easily integrated with other tools * run queries interactively. It permits to poke your codebase, parsing it once and running different queries to find out interesting facts about it. The project is named effectivejava because many queries/checks derive from reading the book Effective Java. Others will be implemented as well (feel free to suggest your favorite ones!). While reading that book I thought that yes, many principles are well known, but they are rarely applied to a large codebase. I thought that applying them in practice is much harder than it seems, and a tool like this one could help in improving constantly a codebase.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of effectivejava
effectivejava Key Features
effectivejava Examples and Code Snippets
Community Discussions
Trending Discussions on effectivejava
QUESTION
I am trying to fix double checked locking with Bloch's effective java recommendation. But a small variation in my code is that the field type is static and the method to create the field type is instance method. Will the below variation for creating a static field work?
...ANSWER
Answered 2021-Jan-20 at 23:59Assuming you are using Java 5 or later1, the above code is thread-safe2.
It doesn't matter that either the method or field is static
provided that:
- field being initialized is
volatile
and, - the code doing the DCL initialization is using the same mutex object for any given field.
The former is obviously true. The latter is true because all calls to getField4()
are locking the same Class
object.
1 - Prior to Java 5, the specified semantics of volatile
are not sufficient to guarantee that the code is thread-safe.
2 - Thread-safe but ugly. It is better to avoid the DCL idiom and use one of the other alternatives.
I cannot make the method static method ...
I don't see why not. It is private
so you should not be constrained as to whether it is a static or instance method. It should only affect the current class.
But as noted above, it doesn't make any difference to the idiom.
QUESTION
I found this example code for Joshua Bloch's book, Effective Java. It's meant to demonstrate why you should avoid unnecessarily creating objects:
...ANSWER
Answered 2018-Dec-09 at 09:09Your guess is probably right. The ^=
operator and the if statement at the end are both to prevent compiler/runtime optimisations.
Initially b
is false, b ^= true
assigns true to b
, then b ^= true
assigns false to b
, and the cycle continues.
By making b
cycle through true and false, it makes it harder for the compiler to optimise this because it doesn't see a constant value.
Another property of ^
is that both operands must be evaluated in order to evaluate the result, unlike ||
or &&
. The runtime can't take shortcuts.
The if statement at the end is telling the compiler and the runtime: "don't ignore b
! It is of important use later!".
QUESTION
I found this example code for Joshua Bloch's book, Effective Java. It's meant to demonstrate why you should avoid unnecessarily creating objects:
...ANSWER
Answered 2018-Dec-08 at 15:19The last two lines force the compiler to run the whole loop, to find the value of x
. Otherwise it might detect that x
is not being used at all and ignore the loop, given that inside of it no "real" work is being made. Even though sum()
is called repeatedly, the result of accumulating its returned value would be discarded in the end if we do nothing with x
.
Of course, this assumes that the println()
statement inside the loop can be safely ignored, I'm unsure if the compiler can make such a decision. That would be one aggressive compiler!
QUESTION
Documentation tell us the follow about open annotation
The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class. By default, all classes in Kotlin are final, which corresponds to Effective Java, 3rd Edition, Item 19: Design and document for inheritance or else prohibit it.
My classes
...ANSWER
Answered 2018-Aug-03 at 22:28For me there are two reasons:
First Kotlin takes many ideas from the functional programming world and uses immutability as often as it can to avoid all the known problems with mutation.
So declaring every class "final" by default is (at least for me) similar.
The class cannot be changed or altered (using something like reflection) during the runtime which would make the safetychecks of the Kotlin compiler useless.
So if you want to "mutate" the default implementation of a class you have to mark it as open explicitly.
The second thought which comes to my mind is that inheritance is often missused. Some examples for common traps are explained here
There is the principle "Favor composition over inheritance" as a guideline for better designs. So declaring every class as final by default forces the developer to at least stop for a moment and think about alternative ways to solve the problem instead of using inheritance for the wrong reasons.
But as longe as there are no official statements by the kotlin developers I can only give an opinionated answer.
QUESTION
I am looking to implement following requirement.
The method Set suggestBooks(Reader reader)
should return titles of all the books that meet all the following criteria:
ANSWER
Answered 2018-Jan-20 at 17:05class BookSuggestionService {
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install effectivejava
You can use effectivejava like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the effectivejava component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page