Curriculum
Course: Complete Java Programming Course
Login

Curriculum

Complete Java Programming Course

Text lesson

Understanding Tokens in Java

[post-views]

 

In this lesson, you will learn.

  • Java Tokens

 

Java Tokens

In Java, tokens refer to the smallest units of a program that the compiler recognizes.

Java source code is made up of various types of tokens, which are the building blocks of the language. The basic types of tokens in Java include:

 

1. Identifiers

  • Identifiers are names given to variables, methods, classes, objects, arrays, and other program elements.
  • Identifiers must follow certain rules and conventions.

 

Rule to make identifiers in Java

The following are the rules for forming valid identifiers.

  1. An identifier can have uppercase and lowercase letters(A-Z or a-z), numbers(0-9), underscore( _ ), and dollar-sign($) characters.
  2. Identifiers must not begin with digits.
  3. It can be of any length.
  4. Keywords can’t be used for naming identifiers.
  5. Java is a case-sensitive language i.e. VALUE and value are different identifiers.

Examples: Valid identifiers

Mybooks, DATE_06_12, B1T019, AvgValue, result, $1_23_$a, _value, PRINTRESULT, _PRICE, _MY_JK_2, temp$, isAlphabet

 

Examples: Invalid identifiers

  1. 23Date – Invalid because Starting with a number
  2. Item-Price, yes/no, print.file – Invalid because it contains a character other than A-Z, a-z, digit, underscore( _ ), or dollar($)
  3. Print Result – Invalid because it contains whitespace.

 

2. Keywords in Java

  • Keywords are reserved words with specific meanings in the Java language.
  • Keywords cannot be used as identifiers (like variable names, function names, or class names).

 

There are 50 keywords present in Java as shown in the below table.

KeywordDescription
abstractIndicates that a class or method is abstract.
assertUsed for debugging purposes to make an assertion.
booleanDefines a variable to store the boolean value true or false.
breakExits a loop or a switch statement.
byteDefines a variable to store an 8-bit signed integer.
caseDefines a branch in a switch statement.
catchCatches exceptions generated by try statements.
charDefines a variable to store a 16-bit Unicode character.
classDeclares a class.
constNot used. Reserved for future use.
continueSkips the current iteration of a loop.
defaultSpecifies the default case in a switch statement.
doStarts a do-while loop.
doubleDefines a variable to store a double-precision 64-bit IEEE 754 floating point.
elseSpecifies the block of code that runs if an if statement’s condition is false.
enumDeclares an enumerated type.
extendsIndicates that a class is derived from another class or interface.
finalIndicates that a value cannot be changed or a method cannot be overridden.
finallySpecifies a block of code that will always be executed in a try-catch structure.
floatDefines a variable to store a single-precision 32-bit IEEE 754 floating point.
forStarts a for loop.
gotoNot used. Reserved for future use.
ifStarts an if statement.
implementsIndicates that a class implements an interface.
importImports other Java packages or classes.
instanceofTests whether an object is an instance of a specific class or implements an interface.
intDefines a variable to store a 32-bit signed integer.
interfaceDeclares an interface.
longDefines a variable to store a 64-bit signed integer.
nativeSpecifies that a method is implemented in native code using JNI.
newCreates new objects.
nullIndicates that a reference does not refer to anything.
packageDefines a package, a namespace for organizing classes and interfaces.
privateRestricts access to a member (variable or method) to the defining class.
protectedRestricts access to a member to subclasses and classes in the same package.
publicMakes a class, method, or variable accessible from any other class.
returnExits a method and optionally returns a value.
shortDefines a variable to store a 16-bit signed integer.
staticIndicates that a member (variable or method) belongs to the class, rather than instances of the class.
strictfpUsed to restrict the precision and rounding of floating point calculations to ensure portability.
superRefers to superclass (parent) objects.
switchSpecifies a switch statement.
synchronizedIndicates that a method or block of code is synchronized for threading.
thisRefers to the current object in a method or constructor.
throwThrows an exception.
throwsIndicates what exceptions a method can throw.
transientPrevents serialization of a field.
tryStarts a block of code that will be tested for exceptions.
voidIndicates that a method does not return a value.
volatileIndicates that a variable may be changed by threads other than the one that is executing.
whileStarts a while loop.
Keywords in Java

 

Note: The keywords const and goto are reserved but not used.

Note: In addition to the keywords, Java reserves the true, false, and null. These are values defined by Java and can’t be used as names of variables, classes, and so on.

 

3. Literals in Java

Literals represent constant values in Java code. The following are the Java literals.

 

3.1. Integer Literals

  • Integer literals are the whole numbers that do not contain any fractional part.
  • It must contain at least one digit and must not contain a decimal point.
  • It may have either a + or – sign, a number without a sign assumed to be positive. The comma (, ) is not allowed in integer constant.

 

Types of Integer Literals

  • Java has three types of integer literals

1. Decimal(base 10)

  • These are the integer literals that begin with a non-zero digit E.g. 123, -345.

2. Octal(base 8)

  • It is integer literals that begin with zero. Octal ranges are 0 to 7. E.g. to write octal number 21, you will write 021.

3. Hexadecimal(base 16)

  • It is integer literals that begin with 0x or 0X.
  • The range of a hexadecimal digit is 0 to 15.
  • The digits 0 through 9 represent values zero through nine, and the letters A through F (or a through f) represent values ten through fifteen.
  • Example: To write the hexadecimal number 2A9, you will write 0X2A9.

Example

The number 13 can be written either as 13(decimal), 015(octal), and 0XD(hexadecimal).

 

3.2. Floating-Point Literals

  • It is also known as real literals and represents decimal values with a fractional component.

Floating-Point Literals may be written in one of the following two forms

  1. Fractional and
  2. Exponent

1. Fractional Form

  • It consists of signed or unsigned digits including decimal points.

Example: A valid floating-point literals in fractional form

4.0, -34.05, 4.01234, 0.67567, -0.5674

Example: Invalid floating-point literals in fractional form

  1. 10 -> No decimal point.
  2. 6. -> No digit after the decimal point.
  3. 154.56.34 -> Two decimal points.
  4. 3,34,56.005  -> Comma is not allowed.
  5. +13/2 -> / is not allowed.

2. Exponent Form

  • The second form of real literals is the exponent form which consists of two parts first, mantissa, and second the exponent.

 

Example: Floating Point Literals of Exponent Form

2.4 can be written as 0.24 x 101 = 0.24E01, where 0.24 is the mantissa part(before E) and exponent part is 1(after E).

Valid Real literals in exponent form

234E04, 2.34E09, 0.234E04, 120.0E05, 234E+4, -0.132E-3

Invalid Real Literals in the exponent form

  1. 120.E3 (At least one digit after the decimal point),
  2. 1.3E (No digit after exponent),
  3. 0.13E4.3 (Exponent is not allowed fractional point),
  4. 23,456E5 (Comma is not allowed),
  5. .34E-3 (No digit before decimal points).

 

3.3. Character Literals

  • The character literals must contain one character and must be enclosed with single quotes. E.g. ‘a’ or ‘A’.
  • Java allows some nongraphic characters in the character constant for example backspace, tabs, single quotes, etc.

These characters are represented by the escape sequence(, backslash) followed by one or more characters.

The following table shows the escape sequences.

Escape SequenceNon-graphic Character
bBackspace
tHorizontal Tab
vVertical Tab
fFormfeed
nNewline or Line feed
rCarriage return
\Backslash
Single quote
’’Double quote
aAlert bell
?Question mark
dddOctal Number
uxxxxHexadecimal Number(Hn is Hexadecimal)

 

3.4. Boolean Literals

  • Boolean literals are the type of boolean that have two logical values i.e. true or false.
  • The true and false can’t be converted into numerical representations.

Note: The true literal in Java does not equal 1, nor does the false literal equal 0.

3.5. String Literals

  • String literals contain zero or multiple characters and must be enclosed by double quotes.
  • It is of class type String.

Example: String Literals

“Hello” -> Its size is 10 bytes (Each character takes 2 bytes).

“abc” -> It size is 6 bytes(a is one character)

“amit’ class” -> Its size is 24 bytes(’ is one character)

 

3.6 Null Literals

  • The null literal is always of null type and has one value i.e. the null reference.

 

4. Separators in Java

  • These include symbols like braces {}, parentheses (), semicolons ;, commas ,, etc.
  • It is used to structure and separate different parts of the code.

The separators in Java are shown in the following table.

SymbolNameUse
( )ParenthesesContains lists of parameters in method definition and invocation, used in typecasting, etc.
{ }BracesUse to initialized arrays, define a block of code, for classes, methods, and local scopes.
[ ]BracketsUsed to declare array types.
;SemicolonTerminates statements.
,CommaSeparates identifiers in a variable declaration.
.PeriodUsed to separate package names from sub-packages and classes.
::ColonsUsed to create a method or constructor reference. (Added by JDK 8.)

 

5. Operators in Java

Operators perform operations on variables and values.

Java provides many operators that are divided into the following groups.

  1. Arithmetics Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Shift Operators

 

The Java operators are described in detail in the upcoming lesson.

 

 


End of the lesson….enjoy learning

 

Student Ratings and Reviews

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review