Tutorial by Examples

Package names Package names should be all lower case without underscores or other special characters. Package names begin with the reversed authority part of the web address of the company of the developer. This part can be followed a by project/program structure dependent package substructure. ...
All lines must be terminated with a line feed character (LF, ASCII value 10) and not for instance CR or CR+LF. There may be no trailing white space at the end of a line. The name of a source file must equal the name of the class it contains followed by the .java extension, even for fil...
Apart from LF the only allowed white space character is Space (ASCII value 32). Note that this implies that other white space characters (in, for instance, string and character literals) must be written in escaped form. \', \", \\, \t, \b, \r, \f, and \n should be preferred over corres...
package com.example.my.package; The package declaration should not be line wrapped, regardless of whether it exceeds the recommended maximum length of a line.
// First java/javax packages import java.util.ArrayList; import javax.tools.JavaCompiler; // Then third party libraries import com.fasterxml.jackson.annotation.JsonProperty; // Then project imports import com.example.my.package.ClassA; import com.example.my.package.ClassB; // Then stat...
Order of class members Class members should be ordered as follows: Fields (in order of public, protected and private) Constructors Factory methods Other Methods (in order of public, protected and private) Ordering fields and methods primarily by their access modifiers or identifier is not ...
class ExampleClass { // Access modifiers first (don't do for instance "static public") public static void main(String[] args) { System.out.println("Hello World"); } } interface ExampleInterface { // Avoid 'public' and 'abstract' since they are imp...
Indentation level is four spaces. Only space characters may be used for indentation. No tabs. Empty lines must not be indented. (This is implied by the no trailing white space rule.) case lines should be indented with four spaces, and statements within the case should be indented with another f...
Source code and comments should generally not exceed 80 characters per line and rarely if ever exceed 100 characters per line, including indentation. The character limit must be judged on a case by case basis. What really matters is the semantical “density” and readability of the line. Making l...
int someMethod(String aString, List<Integer> aList, Map<String, String> aMap, int anInt, long aLong, Set<Number> aSet, double aDouble) { … } int someMethod(String aString, List&...
If a line approaches the maximum character limit, always consider breaking it down into multiple statements / expressions instead of wrapping the line. Break before operators. Break before the . in chained method calls. popupMsg("Inbox notification: You have " + newMsgs + &...
Vertical Whitespace A single blank line should be used to separate… Package declaration Class declarations Constructors Methods Static initializers Instance initializers …and may be used to separate logical groups of import statements fields statements Multiple consec...
One variable per declaration (and at most one declaration per line) Square brackets of arrays should be at the type (String[] args) and not on the variable (String args[]). Declare a local variable right before it is first used, and initialize it as close to the declaration as possible.
Declaration annotations should be put on a separate line from the declaration being annotated. @SuppressWarnings("unchecked") public T[] toArray(T[] typeHolder) { ... } However, few or short annotations annotating a single-line method may be put on the same line as the method if...
Runnable r = () -> System.out.println("Hello World"); Supplier<String> c = () -> "Hello World"; // Collection::contains is a simple unary method and its behavior is // clear from the context. A method reference is preferred here. appendFilter(goodStrings::cont...
return flag ? "yes" : "no"; String cmp = (flag1 != flag2) ? "not equal" : "equal"; // Don't do this return (flag ? "yes" : "no"); Redundant grouping parentheses (i.e. parentheses that does not affect evaluation) may be used if t...
long l = 5432L; int i = 0x123 + 0xABC; byte b = 0b1010; float f1 = 1 / 5432f; float f2 = 0.123e4f; double d1 = 1 / 5432d; // or 1 / 5432.0 double d2 = 0x1.3p2; long literals should use the upper case letter L suffix. Hexadecimal literals should use upper case letters A-F. All other num...
class Example { void method(boolean error) { if (error) { Log.error("Error occurred!"); System.out.println("Error!"); } else { // Use braces since the other block uses braces. System.out.println("No error"); ...

Page 1 of 1