0% found this document useful (0 votes)
2 views8 pages

Java Tokens

The document explains Java tokens, which are the smallest meaningful elements in a Java program, categorized into five types: keywords, identifiers, literals, operators, and punctuators. It details the rules for creating valid identifiers, the types of literals including numeric and non-numeric, and various operators used in Java for arithmetic, relational, logical, and assignment operations. Additionally, it discusses operator precedence and provides examples for better understanding.

Uploaded by

binduannthomas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Java Tokens

The document explains Java tokens, which are the smallest meaningful elements in a Java program, categorized into five types: keywords, identifiers, literals, operators, and punctuators. It details the rules for creating valid identifiers, the types of literals including numeric and non-numeric, and various operators used in Java for arithmetic, relational, logical, and assignment operations. Additionally, it discusses operator precedence and provides examples for better understanding.

Uploaded by

binduannthomas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Tokens

Token
The smallest meaningful element of a Java program is called a [Link] tokens are
basic building blocks,which can be put together to construct [Link] the tokens
that comprise Java are known as Java token sets.
Java has the following 5types of tokens.
● Keywords
● Identifiers
● Literals
● Operators
● Punctuators

1. Keywords

Keywords are the words that convey a special meaning to the language
[Link] are reserved for special purpose and must not be used as normal
identifier names.
Eg. public,void,class,int,switch,break,continue etc.

[Link](variables)

They are fundamental building blocks of a program. Identifiers are the


names given to identify different building blocks of a program such as
variable,class,method(function) etc.
Identifier forming rules(naming rules)

● Identifiers can have alphabets,digits , underscore and dollar sign ($) characters
● They must not be a keyword or boolean literal or null literal.
● They must not begin with a digit.
● They can be of any length.
● Java is case sensitive, that is upper case letters and lower case letters are
treated differently.(ie,total,TOTAL,Total,ToTal …..all these are different
identifiers)

Some egs: of Valid identifiers-:​ ​some egs: of invalid identifiers-:


Myfile [Link]
A_to_Z A-to-Z
Tot12$ 12$
$CHK 5CHK$
$1_to_$10 1$-TO
[Link](constants)
Constants mean the fixed values that do not change during the execution of a
[Link] can be of 4 types.

1​.Numeric literals-
(a)Integer literal -​ represents whole numbers [Link] can be positive or negative
values.
eg 15,-7,7654,-98 etc
Some invalid integer literals are-
676 78 space not allowed
675,789 commas are not allowed.

Java allows three types of integer literals.


1)Decimal integer literal(base 10)
2)Octal integer literal(base 8)
3)Hexadecimal integer literal(base 16)
Decimal integer literal
An integer literal consisting of a sequence of digits is taken to be a decimal
integer literal unless it begins with [Link]-1234,-87,42,+97 etc are decimal
integer literal
Octal integer literal
A sequence of digits starting with zero is taken to be an octal integer literal.
For eg decimal integer 8 will be written as 010 as octal integer literal.(8​10​=10​8​)
and decimal integer 12 will be written as 014 as octal integer literal
​ =14 8​​ ).But make sure that when an integer starts with 0(zero) it must not
(12 10​
contain 8 and 9, as there are invalid octal digits.
Hexadecimal integer literal
A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer.
For eg:decimal integer 12 will be written as 0XC as hexadecimal integer
[Link] with hexadecimal constants only 0 to 9 and A to F can be [Link]
other letters are illegal.
Thus number 12 will be written either as 12(as decimal),014(as octal) and
0XC(as hexadecimal )
(b​)Real(floating) literal/constant​ -​ represents values with a decimal [Link]
literals are of two types real literal in fractional form and real literal in exponential
form.
Real literal in fractional form​ consists of signed or unsigned digits including a
decimal point between the digits.
eg 67.7 , -56.98, 678.324 , 0.9876 etc.
Some invalid real literals in fractional form are -
233 it must have decimal values.
233. it must have one digit after the decimal point
.24 it must have at least one digit before the decimal point.
34.67.89 two decimal points not allowed.
34,567.98 - comma not allowed
A real literal in exponential form ​consists of two components mantissa and
[Link].7.04*10​ 34​ can be written in the exponential form as 7.04E34
Where 7.04 is the mantissa(component before E) and 34 is the
exponent(component after E)

Some invalid real literals in exponential form are -
55.5E no digit specified after the exponent ie after E
52.3E9.6 the exponent must be an integer value.
.23E7 it must have at least one digit before the decimal point
45,22E25 comma is not allowed.
2.​Non-Numeric literals-

(a) ​Character literal​ ​ - represents one character enclosed in single quotes.


eg ‘A’,’&’, ‘5’, ‘n’ etc.

(b)​String literal ​ - one or more characters enclosed in double quotes .


eg ”java” ,”maya’s house”,”D”, “2+6” ,”8” etc.

3.​ ​Boolean literal​ ​-Logical values true and false are the boolean [Link]
a logical evaluation,the true/false literals represent on/off,yes/no, and
presence/absence of something.

4.​Null literal​-special kind of literal which is represented by the keyword null or


escape sequence’\0’.
Classify the following into valid or invalid literals if valid name it:

1)’A’ -character literal


2)7 -decimal integer literal
3)17 26.23 -invalid(space not allowed)
4)172.E03 - real literal in exponent form
5)-17.5 -real literal/floating literal
6)0.17E25 - real literal in exponent form
7) true - boolean literal
8)+17/2 -invalid
9)1.7E -invalid
10)’9’ -character literal
11)”TVM” -string literal
12)false -boolean literal
13)0XB12 -hexadecimal integer literal
14)073-octal integer literal

[Link]
Operators are special symbols,that are used to perform [Link] are
applied to the variables and constants to form an expression.
Eg. k=a+10;
Here k and a are variables , 10 is a constant, + symbol is an arithmetic operator and
= symbol is an assignment operator.

In the expression, k=a+10


a and 10 are called operands, + is an operator and a+10 is an operation.

​ Operator works on operand to do an operation.

Different types of operators

a) Arithmetic operators

Arithmetic operators are used to perform basic arithmetic operations such as


addition,subtraction,multiplication,division and modulus.
Sy Format Description Result
Operator mb (if a=12,b=4)
ols

addition + a+b used to add 2 operands 16

subtraction - a-b used to find the difference of 2 8


operands

multiplication * a*b used to find the product of 2 48


operands

division / a/b used to find the quotient when do 3


the division of 2 operands

modulus % a%b used to find the remainder of a 0


division

b) Relational operators.

They are used to test the relation between two variables.A relational expression
returns either true or false (boolean result).these operators are used to evaluate
any condition.

Ope Meaning Example Result


rator if(x=24, y=8)

== It is used to check the equality b/w 2 x==y false


operands.

!= It checks the non-equality b/w 2 x!=y true


operands.

> It checks whether the 1​st ​value is x>y true


greater than the 2​nd​ value.

< It checks whether the 1​st ​value is less x<y false


than the 2​nd​ value.

>= It checks whether the 1​st ​value is x>=y true


greater than or equal to the 2​nd​ value.

<= It checks whether the 1​st ​value is less x<=y false


than or equal to the 2​nd​ value.

c)​Logical operators

Logical operators are used for logical operations which compare the result of relational
expressions and give the result as true or [Link] operators join two or more
expressions.
Operator Symbol Expression Result Reason

AND && (5<8 )&& (35>25) true both expressions are


true

OR || (25>=25) || (20<16) true


first expression is true

NOT ! !(21>7) false 21is greater than 7 ,it is


a true statement but
when ! symbol is there
,we have to negate the
result. so final answer
is false

&&(AND) operator will give the result true if both the expressions are true but ||(OR)
operator will give the result true if any one of the expressions is true or both the
expressions are true.

d)Unary operators
Unary operators work on single operands.
Eg : ++a ,a++ ,--a, a--
These operators are used to increase or decrease the value stored in the variable.
++a is a unary increment operator means a=a+1 (if a=8,++a =9)
--a is a unary decrement operator means a=a-1 (if a=8,--a =7

​ inary operators work on 2 operands. All arithmetic operators are binary


B
operators.
Eg:
k=5+7
p=a*b

e)​Assignment operator
It is used to assign a value to a variable
Eg a=5

What is the difference between = sign and ==sign?


= sign is an assignment operator and it assigns values to a variable.
Eg a=5 means the value 5 is assigned to a
== is a relational operator and it checks whether the value is equal to that variable
Eg if(a==5) it checks whether a is equal to 5

e)​Shorthand operators

Operator Assignment operator Result[a=8,n=4]

a+=1 a=a+1 9

a-=1 a=a-1 7

a*=n a=a*n 32

a/=n a=a/n 2

a%=n a=a%n 0

Precedence of operators.

If we have more than one operator in the entire expression,the order in which
different operators in an expression is evaluated is known as precedence of
operators
We use the rule of BODMAS to evaluate the precedence of operators.

Eg 30+20*10
30+(2*10)=30+200=230

The following order shows the precedence of operator


precedence operator type

Highest precedence () parentheses

++.-- Unary operator

^ exponent

* , /, % Multiplication,division,
modulus

+,- addition,subtraction

<,<=,>,>=,==,!= relational

&&,|| logical

Lower precedence =,+=,-=,*=...... Assignment and


shorthand

You might also like