- Features of Java :-
A list of the most important features of the Java language is given below.
2. What is an object in Java
object in Java: –
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system.
An object has three characteristics:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
3. What is a clean code? The code that is less prone to errors and easily readable, modifiable, scalable(in some cases), maintainable by any developer (not just its programmer) is referred to as “Clean Code”.
Clean Code is easily readable, maintainable, and less prone to errors.
What are the signs of bad code (Unclean code)?
- Hard to read code
- Unnecessarily complex
- Not easily testable
- Hard to modify
How to write Clean Code in Java?
- Structure. …
- Proper Naming. …
- Source File Structure. …
- Code Formatting: Whitespaces and Indentation. …
- Method Parameters. …
- Avoid Hardcoding. …
- Code Comments. …
- Code Formatters: While writing code, proper formatting is essential, and the popular code editors for Java already have automatic code formatting. Due to automatic code formatting, programmers don’t have to think about the code formatting and all. If you don’t want the default code formatting that is available in the code editor, you can easily customize it as per your requirements.
- Static Analysis Tools: For Java, there are many static code analysis tools available out there such as Checkstyle, SonarQube, and SpotBugs, etc. which offer a set of rules that can be applied to your projects.
- Conclusion – how to write clean code in java ?
- Clean code can differentiate you from other programmers.
- The characteristics of clean code are: simple, readable, maintainable, and testable.
- The symptoms of bad code are: Hard to read, Unnecessarily complex, and hard to test and maintain.
- Hard coding should be avoided to write quality code. Hardcoding can lead to serious maintainability issues if the project grows in size.
- SOLID is a mnemonic for the five principles to write quality code:
- Single Responsibility Principle
- Open/Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
- DRY stands for “Don’t Repeat Yourself”. It focuses on code reusability.
- KISS stands for “Keep It Simple, Stupid”. It focuses on making to code simple to understand.
- We should write code that has low coupling and high cohesion.
- Various tools like code formatters and Static Analysis Tools are available out there to make our work easier.