Java - Tokens

Java

In this tutorial we will learn about Java tokens.

Character Set

It is a set of characters that we use to write Java code.

Java uses Unicode character set that supports 34,000+ characters from 20+ languages.

Token

Token is the smallest individual unit of a program.

Following are the tokens used in Java programming language.

  • Keywords
  • Identifiers
  • Literals
  • Operators
  • Separators

Keywords

Keywords are the words that conveys a special meaning to the language compiler and are reserved for special purpose and hence must not be used as normal identifier name.

Following are the keywords used in Java.

abstractcontinuefornewswitch
assertdefaultgotopackagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
constfloatnativesuperwhile

Identifiers

Identifiers are used as the general terminology for the names givens to different parts of the program like the name of the variables, methods, arrays etc.

Identifiers name are user-defined and must follow the given rules.

  • First character of the identifier must be a letter or underscore
  • Identifier names must only use letters (A-Z and a-z), digits (0-9) and underscore _
  • Can't use white spaces
  • Can't use keywords

Example: age, _score, isGameOver etc.

Literals

Literals are the sequence of characters using digits, letters and other symbols, to represent values that is stored in variables.

There are five types of literals in Java.

  • Integer literal
  • Floating Point literal
  • Character literal
  • String literal
  • Boolean literal

Integer literal

Integers are numbers without decimal parts and there are three types of integers namely decimal, octal and hexadecimal integers.

Decimal integers are in base 10 number system and uses 10 digits from 0 to 9.

Example: -9, 0, 10 etc.

Octal integers are in base 8 number system and uses 8 digits from 0 to 7 with a leading 0.

Example: 07, 017 etc.

Hexadecimal integers are in base 16 number system and uses 10 digits from 0 to 9 and 6 letters from A to F with leading 0x and 0X. Lowercase letters from a to f can also be used.

Example: 0x11, 0xAA, 0xbb, 0x12F etc.

Click here for tutorial series on conversion of numbers from decimal to binary, octal, hexadecimal number system.

Floating Point literal

These are the numbers having decimal part. Example -10.2, 0, 100.99 etc.

Character literal

A character literal is a symbol enclosed in single quotes.

Example: 'a', '1' etc.

String literal

A string literal is a sequence of characters enclosed in double quotes.

Example: "Hello World"

Boolean literal

Boolean literal are either TRUE or FALSE.

Operators

An operator is a symbol that takes one or more arguments and then performs some operations and returns some result.

Example: The addition operator + takes two numbers and returns the sum.

Separators

Separators are symbols that we use to group or arrange our code.

Example: We use the parenthesis () to enclose parameters of a method. We also use them to group variables and values in an expression like (10 + a) - b where, a and b are some variables.

Comments

We use comments to describe the code, highlight points that can be referred by the developer and to prevent a piece of code from getting compiled.

There are two type of comments in Java similar to other programming languages like Java, Php, C++ etc.

  • Single line comments
  • Multi line comments

We use the two forward slash to create single line comments.

// this is a single line comment

We use the /* to start a multi line comment and */ to end a multi line comment.

/**
 * this is a
 * multi line comment
 */