The smallest individual unit in a program is known as token. Java is collection of tokens , There are five type of tokens in java:

  •  Reserve Keywords
  •    Identifiers
  •   Literals
  •  Operators
  •  Seperators

Keywords

Keywords are reserved words in java language which have predefined meaning . Java has reserved 60 words as key words.

Abstract
boolean
Break
byte
Case
catch
Char
class
Const
continue
Default
do
Double
else
Extends
final
Finally
float
For
future
Generic
goto
If
implements
Import
inner
Instanceof
int
Interface
long
Native
new
Null
operator
Outer
package
Private
protected
Public
rest
Return
short
Static
super
Switch
synchronized
This
throw
Throws
transient
Try
var
Void
volatile
While



Identifier

Identifier arer programmer designed tokens . They are used for naming classes, methods, variables, objects , Labels, Packages and interface in program.Identifiers have following rules:

  • All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($). The convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged.
  • After the first initial letter, variable names may also contain letters and the digits 0 to 9.  No spaces or special characters are allowed.
  • Uppercase characters are distinct from lowercase characters. Using ALL uppercase letters are primarily used to identify constant variables. Remember that variable names are case-sensitive. 
  • You cannot use a java keyword (reserved word) for a variable name.


Literals

 Literals in Java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables. Java language specifies five major types of literals. Literals can be any number, text, or other information that represents a value. This means what you type is what you get. We will use literals in addition to variables in Java statement. While writing a source code as a character sequence, we can specify any value as a literal such as an integer.
They are:

Integer literals

·      Floating literals
·      Character literals
·      String literals
·      Boolean literals

Integer literals:

Integer data types consist of the following primitive data types: int,long, byte, and short. byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well. Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals.
Examples:

int decimal = 100;
int octal = 0144;
int hexa = 0x64;

Floating-point literals:

Floating-point numbers are like real numbers in mathematics, for example, 4.13179, -0.000001. Java has two kinds of floating-point numbers: float and double. The default type when you write a floating-point literal is double, but you can designate it explicitly by appending the D (or d) suffix. However, the suffix F (or f) is appended to designate the data type of a floating-point literal as float. We can also specify a floating-point literal in scientific notation using Exponent (short E ore), for instance: the double literal 0.0314E2 is interpreted as:

0.0314 *10² (i.e 3.14).
6.5E+32 (or 6.5E32) Double-precision floating-point literal
7D Double-precision floating-point literal
.01f Floating-point literal

Character literals:

char data type is a single 16-bit Unicode character. We can specify a character literal as a single printable character in a pair of single quote characters such as 'a', '#', and '3'. You must know about the ASCII character set. The ASCII character set includes 128 characters including letters, numerals,punctuation etc. Below table shows a set of these special characters.

Escape
Meaning
\n
New line
\t
Tab
\b
Backspace
\r
Carriage return
\f
Formfeed
\\
Backslash
\'
Single quotation mark
\"
Double quotation mark
\d
Octal
\xd
Hexadecimal
\ud
Unicode character

If we want to specify a single quote, a backslash, or a non-printable character as a character literal use an escape sequence. An escape sequence uses a special syntax to represents a character. The syntax begins with a single backslash character. You can see the below table to view the character literals use Unicode escape sequence to represent printable and non-printable characters.
u0041'
Capital letter A
'\u0030'
Digit 0
u0022'
Double quote "
'\u003b'
Punctuation ;
'\u0020'
Space
'\u0009'
Horizontal Tab

String Literals:

The set of characters in represented as String literals in Java. Always use "double quotes" for String literals. There are few methods provided in Java to combine strings, modify strings and to know whether to strings have the same values.


""
The empty string
"\""
A string containing
"This is a string"
A string containing 16 characters
"This is a " + "two-line string"
actually a string-valued constant expression, formed from two string literals

Null Literals

The final literal that we can use in Java programming is a Null literal. We specify the Null literal in the source code as 'null'. To reduce the number of references to an object, use null literal. The type of the null literal is always null. We typically assign null literals to object reference variables. For instance
s = null;

Boolean Literals:

The values true and false are treated as literals in Java programming. When we assign a value to a boolean variable, we can only use these two values. Unlike C, we can't presume that the value of 1 is equivalent to true and 0 is equivalent to false in Java. We have to use the values true and false to represent a Boolean value.

Example
boolean chosen = true;

Operators

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Java supports a rich set of operators. Some of them are =, +, -, *. An Operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They usually form a part of mathematical or logical expressions.
Java operators can be classified into a number of related categories as below:

Arithmetic Operators

            +  Additive operator (also used for String concatenation)
            -  Subtraction operator
            *  Multiplication operator
            /  Division operator
            % Remainder operator

Equality, Relational Operators

            ==   Equal to
            !=   Not equal to
            >   Greater than
            >= Greater than or equal to
            <   Less than
            <= Less than or equal to

Logical Operators

            &&  Logical AND
            ||  Logical OR
            ! Logical NOT

Assignment Operators


=
Simple assignment operator, Assigns values from right side operands to left side   operand
C = A + B will assign value of 
A + B into C
+=
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
C += A is equivalent to          
  C = C + A
-=
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
C -= A is equivalent to           
 C = C - A
*=
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
C *= A is equivalent to         
   C = C * A
/=
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
C /= A is equivalent to           
 C = C / A
%=
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
C %= A is equivalent to
 C = C % A
<<=
Left shift AND assignment operator
C <<= 2 is same as                 
 C = C << 2
>>=
Right shift AND assignment operator
C >>= 2 is same as C = C >> 2
&=
Bitwise AND assignment operator
C &= 2 is same as C = C & 2
^=
bitwise exclusive OR and assignment operator
C ^= 2 is same as C = C ^ 2
|=
bitwise inclusive OR and assignment operator
C |= 2 is same as C = C | 2




Increment and decrement Operators


            ++ Adds 1 to the Operand
            --  Subtracts 1 from the Operand

Conditional Operators

?:  Ternary (shorthand for if-then-else statement)

Bitwise Operators


~     Unary bitwise complement
<<   Signed left shift
>>   Signed right shift
>>> Unsigned right shift
&     Bitwise AND
^      Bitwise exclusive OR
|      Bitwise inclusive OR

Special Operators


. (Dot Operator)  - To access instance variables
instanceof  - Object reference Operator

Separators

Separators help define the structure of a program.
The table lists the six Java separators .

( )
Encloses arguments in method definitions and calling; adjusts precedence in arithmetic expressions; surrounds cast types and delimits test expressions in flow control statements
{ }
defines blocks of code and automatically initializes arrays
[ ]
declares array types and dereferences array values
;
terminates statements
,
separates successive identifiers in variable declarations; chains statements in the test, expression of a for loop
.
Selects a field or method from an object; separates package names from sub-package and class names
:
Used after loop labels

1 comment:

Unknown said...

Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more .
java training in chennai | Best java training in chennai | java training | java training in chennai with placement

| Designed by Harhiktech