Literals are constants but no variables. They can be called primitive values as well. Such literal can be rendered to a variable which must be capable of carrying the type of the value.
Beside rendering to variables, a literal can be passed to methods as well. At the receiver part a variable will receive the value.
A literal type can be:
logical
whole number
floating-point number
character
string
EXAMPLE FOR LITERALS:
In some cases a literal type can be converted to an other, usually from a narrower type to a wider one.
When a type is rendered to a variable of different type but conversion is possible, Java will proceed with automated literal conversion.
Similarly, when two literals with different types are put into a common equation (e.g. incrementing or adding), if possible, Java will convert one of the literals which will allow Java to proceed.
EXAMPLE FOR AUTOMATED LITERAL CONVERSION:
When dealing with long strings or strings with multiple lines, from Java 15, text blocks can be used for achieving the needed simplicity and readability.
A text block starts with """ (three quotation marks), the first character of the text starts in the next line.
The text block ends with the same three signs (""") right after the last character of the text block.
New line character (\n) will be inserted following the natural text flow. In case instead of \n the \r\n line break is needed, use the \r escaped character at the visible line end.
In case no \n character is needed at actual line end in the resulted string but for better readability line break is needed in the text block, use the escape (\) character at the virtual end of the line.
Inside the text block, no escape character is needed for single or double quotation marks (" and ""). Therefore, it is ideal to use it for native JSON data, SQL commands etc. For three quotation marks, escape the first one only (\""").
EXAMPLE FOR TEXT BLOCKS:
The text block indentation comes from the structure of the code. For every line of the text block, the empty spaces before the first character will be trimmed unless those spaces are after the code indent plus one indent. Exceptions are blank lines which contain only spaces and will be trimmed to zero length (empty lines).
EXAMPLE FOR TEXT BLOCK INDENTATION (DOTS MARK SPACES):
Trailing spaces will be ignored at line ends by default with two exceptions:
when the line ends with escape character (\), a space before the escape character will be preserved
when the line ends with escaped space "\s" character (special marker for text blocks)
EXAMPLE FOR TRAILING SPACES IN TEXT BLOCK (DOTS MARK SPACES):
Finally, Java allows to use a formatter method on text blocks in the same way as on string literals, namely, a %s marker can be used which the called method will replace with a passed value.
EXAMPLE FOR FORMATTER METHOD ON TEXT BLOCK: