What is a keyword in Java

2.1 Keywords, literals, variable names, comments

On this page you will learn how to name the most important parts of the Java language.

For example the parts of an assignment:

Java keywords are:

abstractcontinuefornewswitch
assertdefaultifpackagesynchronized
booleandogotoprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
constfloatnativeSuperwhile

The keywords grouped in categories result in:

The key words const and goto are reserved even if they are not used at the moment.

true and false are technically Boolean literals. (Java 7 spec. §3.10.3). The same goes for that zero Literal (Java 7 spec. §3.10.7).

See Java specification: Identifier

Rules for names in Java:

  1. Valid names may only consist of letters, numbers, and the characters "_" and "$". They can be of any length.
  2. The first character must be a letter, "_" or "$" character.
  3. Spaces are not allowed
  4. Java language keywords may not be used. (Boolean literals and that zero Literal may not be used either)
  5. Letters are case-sensitive

Literals are used to describe concrete values ​​in the Java language.

The syntax was described using regular expressions.

syntaxTypeexample
numberint17, -17
number[l | L]long24l, -24L
0 [x | X]hexint in hexadecimal notation0x01234567890ABCDEF
0octalint in octal representation071
0b {0 | 1}int in binary representation (only since Java 7, JSR 334)0b101010101010
[+|-][number].numberdouble (floating point number)-3.1415
[+|-]number[f | F]float (floating point number)-3.1415F
[+|-]number[d | D]double (floating point number)-3.1415E13D
[+|-]numbersigned int-3, +3
[+|-]number.number[e | E][+|-]numberExponent representation-3.1415E10
'character'single character'a'
"characters"String"from 10"
""Empty string""
\ bCharacter position 1 to the left (back space)
\ ttab
\ nLine feed
\ fForm feed
\ rCarriage return
\"Double quotation mark
\'Single quotation mark
\\Backward slash
\ uNNNNUnicode characters (NNNN in hexadecimal)
trueBoolean valuetrue
falseBoolean valuefalse

numbers

  • Integers
    • Decimal numbers with the digits 0 to 9 and sign
    • Hexadecimal with the digits 0 to 9 and characters interpreted as digits a to f and a prefix 0x
      • Examples: 0x1, 0xffff, 0xcafebabe
    • Octal numbers with the digits 0 to 7 and one featured 0
      • Example 012 (= 1 * 81+2*80=1010 ,  077 (=7*81+7*80=6310)
  • Floating point numbers,
    • represented in the familiar decimal system
      • Part of the decimal point separated by a point
        • Example: 5.4, 6. (represents floating point number 6.0)
    • Powers of ten based on ten are denoted by the letter e or E.
      • Example: 4E-2 represents 4 * 10-2= 0.04 there.

From JDK 7: grouping in numerical literals

Since JDK 7, the integration of the "Coin" project (JSR 334) has made it possible to group groups of digits into numeric literals using the underscore '_'.

Examples of the new syntax taken from the JSR specification:

1234_5678 1_2_3_4__5_6_7_8L 0b0001_0010_0100_1000 3.141_592_653_589_793d 0x1.ffff_ffff_ffff_fP1_023 // Double.MAX_VALUE

are not allowed:

_1234 0x_1234 1234_ 0x1.0_p_-1022

Warning: Java 7,8,9 are not yet used everywhere:

  • When using javac JDK 5 or 6 Java translators will not accept this syntax
  • When using the Java translator with options to use old syntax standards such as javac -source 1.6 in JDK 7,8,9 the new syntax will not be accepted either!

Characters and strings

  • Character: A single character (char) is written in single quotation marks
    • Examples: 'a', '2', '!', ''
  • Strings: sequences of characters enclosed in double high kamma
    • Examples: "This is a string",
    • tip: Strings must not extend beyond the line margin!

Strings without names are called Literals.

Comments allow a program to be documented. They are ignored by the translator.

Java also uses the comment concept to generate documentation. Documentation comments are a special form of comments and are presented in the documentation section.

Line comments, line end comments

Line comments begin after the double slash //. The Java translator will ignore all characters after this comment character up to the end of the line.

Example:

int a = 17; // This is an end-of-line comment // This comment covers an entire line int b = 18;

Multi-line comments

Java allows a number of lines to be marked as comments. Multi-line comments are introduced with the characters "/ *" (slash asterisk) and ended with the character combination "* /" (asterisk slash). This can be used to mark entire areas as comments.

Example:

/ * A comment begins here this line belongs to the comment int i = 1; this line is not processed as a command but as a comment the comment ends in the next line * /