Java Tokens
Java is a high-level, object-oriented programming language that is widely used for developing various types of software applications. To write programs in Java, we use a set of special symbols known as tokens. In this article, we will explore the concept of Java tokens, which are the basic building blocks of Java source code.
Understanding Java Tokens
In Java, a token is the smallest element of a program that the compiler can recognize. Java tokens are similar to the words and punctuation that form sentences in any natural language. Each token in Java has a specific meaning and serves a specific purpose in the program.
Java tokens can be divided into five main categories:
- Keywords
- Identifiers
- Literals
- Operators
- Separators
Let's delve into each of these categories in detail.
Keywords
Keywords are predefined, reserved words used in Java programming that have special meanings to the compiler. Keywords cannot be used as identifiers (names for variables, methods, classes, etc.) in your program. Examples of keywords in Java include int
, boolean
, void
, class
, if
, else
, while
, switch
, break
, etc.
Identifiers
Identifiers in Java are names given to elements in a program like variables, methods, classes, packages and interfaces. Identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). After the first character, identifiers can have any combination of characters. A key point to note is that Java identifiers are case sensitive.
Here are some valid identifiers: name
, Name
, NAME
, _name
, $name
, name1
, n1ame
.
Literals
Literals in Java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables. Java supports several types of literals including:
- Integer Literals (e.g., 123, -456)
- Floating-Point Literals (e.g., 123.4, -456.7)
- Boolean Literals (true and false)
- Character Literals (e.g., 'a', 'A', '1')
- String Literals (e.g., "Hello", "Java")
Operators
Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Java has a rich set of operators which can be classified into several groups:
- Arithmetic Operators (e.g., +, -, *, /, %)
- Assignment Operators (e.g., =, +=, -=, *=, /=)
- Comparison Operators (e.g., ==, !=, <, >, <=, >=)
- Logical Operators (e.g., &&, ||, !)
- Bitwise Operators (e.g., &, |, ^, ~, <<, >>, >>>)
Separators
Separators (also known as delimiters) are symbols used for separating a block of code or a set of statements. Java uses several separators including:
- Parentheses
()
- used for method declaration and call, control statements, type casting. - Braces
{}
- used to define blocks of code, such as class body, method body, and loop body. - Brackets
[]
- used for array declaration and to access an array element. - Semicolon
;
- used to separate statements. - Comma
,
- used to separate variables or expressions in a statement.
Understanding Java tokens is a fundamental step in learning Java programming. As you continue to learn, you'll get more familiar with these tokens and how to use them in your code. In the next section, we will look at how these tokens are used to create statements and expressions in Java. For now, practice identifying and using various Java tokens. Happy Coding!