Java Coding Standards
Source:
Medium In this article, you will learn how to properly adhere to coding standards in Java. Java is one of the most widely used programming languages and platforms. Therefore, it is highly likely that your code will be read by multiple people. It follows that the code should be readable and understandable by everyone - from beginners to senior developers. Our goal is to write code in such a way that the reader of the code fully understands it. This requires knowledge and adherence to coding standards.
Why do we need coding guides?
Coding guidelines are important because a significant portion of the cost of software is spent on maintaining the code. Additionally, software is not always developed by a single developer. Therefore, following software writing conventions improves the readability of a program.
1. Naming conventions : We generally follow the CamelCase (camel case) convention in Java programming.
2. Indentation : The indentation unit should be 4 spaces and 8 tabs.
- Apply indentation to similar items in a vertical list (such as end-of-line comments and identifiers in declarations).
- Surround binary operators (including assignment) with spaces.
- A semicolon or comma is followed by a space.
- Add a space between the keyword (“if”, “while”, “return”, “catch”, “switch”, “for”) and the following parenthesis.
- Insert blank lines to differentiate important parts of the code.
3. Spaces . Whitespace also plays an important role in readability:
- Operators must be surrounded by a space.
Операторы следует писать так:
a = (b + c) * d;
А не так:
a=(b+c)*d
- Java reserved words must be followed by a space. For example:
Цикл нужно объявлять так:
while (true) {...}
А не так:
while(true){...}
- Commas must be followed by a space. For example:
Функцию нужно объявлять так:
fun(a, b, c, d);
А не так:
fun(a, b, c, d);
- Colons must be surrounded by a space. For example:
case нужно объявлять так:
case 100 : break;
А не так:
case 100:break;
- Semicolons in for statements must be followed by a space character. For example:
Цикл for нужно объявлять так:
for (i = 0; i < n; i++)
А не так:
for(i=0;i<n;i++)
5. Comments : Java programs can have two types of comments.
- Implementation comments are delimited by the // symbol . For implementation comments, Java also allows you to use /*…*/ .
- Block comments are used to describe files, methods, data structures, and algorithms.
- Single-line comments can be placed on one line and indented to the level of subsequent code. If a comment cannot be written on one line, it must follow the block comment format.
- Trailing (very short) comments can appear on the same line of code that they describe, but must be separated from the code by a significant distance.
- Documentation comments describe Java classes, interfaces, constructors, methods, and fields. They are separated by /**…*/ . Note the double asterisk ** at the beginning with one comment per class, interface, or member. This comment must appear immediately before the declaration, with no space between the comment and the code it refers to. Documentation comments can be extracted into HTML files using the javadoc tool.
private int comments_;
HashMap in Java - usage features and interaction methods
Source:
FreeCodeCamp Today you will learn about the features of working with HashMap, methods of interacting with the data stored in them, and a number of other useful recommendations.
What are the features of HashMap in Java?
Before working with HashMap, it is important to understand some features:
- Items are stored in key/value pairs.
- Elements do not maintain order when added. The data remains unorganized.
- If there are duplicate keys, the last one takes precedence over the other(s).
- Data types are specified using wrapper classes instead of primitive data types.
How to create a HashMap in Java
To create and use a HashMap, you must first import the
java.util.HashMap package :
import java.util.HashMap;
The syntax when creating a new HashMap is:
HashMap<KeyDataType, ValueDataType> HashMapName = new HashMap<>();
Now let's understand some of the terms in the above syntax.
- KeyDataType denotes the data type of all keys that will be stored in the HashMap file.
- ValueDataType denotes the data type of all values that will be stored in the HashMap file.
- HashMapName denotes the name of the HashMap.
Here is an example for a simpler understanding of the terms:
HashMap<Integer, String> StudentInfo = new HashMap<>();
In this code, we have created
a HashMap called
StudentInfo . All the keys stored in
the HashMap will be Integers and the values will be Strings. Note that when specifying data types for keys and values in a HashMap, we are working with wrapper classes, not primitive types. Before we dive into the examples, here is a list of wrapper classes and their corresponding primitive data types in Java:
Wrapper classes and primitive types in Java
WRAPPER CLASSES |
PRIMITIVE DATA TYPES |
Integer |
int |
Character |
char |
Float |
float |
Byte |
byte |
Short |
short |
Long |
long |
Double |
double |
Boolean |
boolean |
When working with HashMap, we only use wrapper classes.
HashMap Methods in Java
Now we'll talk about some useful techniques you can use when working with HashMap. For example, you'll learn how to add, access, remove, and update elements in a HashMap file.
How to add HashMap elements in Java
To add elements to a HashMap, we use the
put() method . It takes two parameters - the key and the value of the element to be added. Here's an example:
import java.util.HashMap;
class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> StudentInfo = new HashMap<>();
StudentInfo.put(1, "Ihechikara");
StudentInfo.put(2, "Jane");
StudentInfo.put(3, "John");
System.out.println(StudentInfo);
}
}
In this code snippet, the HashMap is named
StudentInfo . We specified the keys as integers and the values as strings:
HashMap<Integer, String> . To add elements to the HashMap, we used the
put() method :
StudentInfo.put(1, "Ihechikara");
StudentInfo.put(2, "Jane");
StudentInfo.put(3, "John");
We've added three elements, each with an integer as the key and a string as the value.
How to access elements in a HashMap
You can use the
get() method to access the elements stored in the HashMap file. It takes one parameter - the key of the element being accessed. Here's an example:
import java.util.HashMap;
class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> StudentInfo = new HashMap<>();
StudentInfo.put(1, "Ihechikara");
StudentInfo.put(2, "Jane");
StudentInfo.put(3, "John");
System.out.println(StudentInfo.get(2));
}
}
In the above example,
StudentInfo.get(2) returns the value with key
2 . When output to the console, "Jane" is printed.
How to change the value of elements in a HashMap in Java
To change the value of elements in a HashMap, we use the
replace() method . It takes two parameters - the key of the element being changed and the new value that is assigned to it.
import java.util.HashMap;
class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> StudentInfo = new HashMap<>();
StudentInfo.put(1, "Ihechikara");
StudentInfo.put(2, "Jane");
StudentInfo.put(3, "John");
StudentInfo.replace(1, "Doe");
System.out.println(StudentInfo);
}
}
When the above
HashMap was assigned elements, the element with key
1 had the value “Ihechikara”. We changed its value to “Doe” using the
replace() method: StudentInfo.replace(1, "Doe"); .
How to remove elements in HashMap Java
To remove an element from a HashMap file, you can use the
remove() method . It takes one parameter - the key of the element to be deleted.
import java.util.HashMap;
class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> StudentInfo = new HashMap<>();
StudentInfo.put(1, "Ihechikara");
StudentInfo.put(2, "Jane");
StudentInfo.put(3, "John");
StudentInfo.remove(1);
System.out.println(StudentInfo);
}
}
Here we have removed the element with key
1 using the remove() method . If you want to remove all
HashMap elements at once, then use the
clear() method :
import java.util.HashMap;
class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> StudentInfo = new HashMap<>();
StudentInfo.put(1, "Ihechikara");
StudentInfo.put(2, "Jane");
StudentInfo.put(3, "John");
StudentInfo.clear();
System.out.println(StudentInfo);
}
}
Here are some more useful methods you can use when working with HashMap:
- containsKey returns true if the specified key exists in the HashMap file .
- containsValue returns true if the specified value exists in the HashMap .
- size() returns the number of elements in a HashMap .
- isEmpty() returns true if there are no elements in the HashMap .
Conclusion
In this article, we talked about
HashMap in Java. First, we looked at the features of the HashMap file and learned how to interact with the elements and stored data. We also looked at code examples and some methods for working with
HashMap . Happy coding!
GO TO FULL VERSION