[go: up one dir, main page]

0% found this document useful (0 votes)
20 views17 pages

Module 1

The document discusses different types of tokens in Java including keywords, identifiers, literals, operators, and special symbols. It provides examples of each token type and explains some basic rules for identifiers.

Uploaded by

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

Module 1

The document discusses different types of tokens in Java including keywords, identifiers, literals, operators, and special symbols. It provides examples of each token type and explains some basic rules for identifiers.

Uploaded by

sumanthcm.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Module-1

Tokens in Java
• Tokens are the various elements in the java program that are identified by Java compiler. A
token is the smallest individual element (unit) in a program that is meaningful to the compiler.

• In simple words, a java program is a group of tokens, comments, and white spaces. For example,

• consider the below java statements:


final double p = 3.14 // A constant.
x = a + b; // An expression.
v = Math.pow(10, 1); // An inbuilt java function.

• Java language contains five types of tokens that are as follows:


1. Keywords
2. Identifiers
3. Literals
4. Special Symbols
5. Operators
Keywords
• Keywords are reserved words in programming languages. These are used to indicate
predefined terms and actions in a program.
• Keywords are not allowed to be used as names of variables or objects.
• Keywords are case-sensitive and always written in lowercase.
• Java has the following keywords:
Identifiers
• A method name, class name, variable name, or label is an identifier in Java. The user
typically defines these.
• The identifier names cannot be the same as any reserved keyword.
• Let's see an example to understand identifiers:
public class Test {
public static void main(String[]args)
{
int num = 10;
}
}
Identifiers present in the above program are:
Test: The name of the class.
main: The name of a method.
String: A predefined class name.
args: A variable name.
num: A variable name.
Cont..

Rules for naming identifiers.

• The characters allowed are [A-Z], [a-z], [0-9], _ and $.

• Identifiers are case-sensitive.


• That is, “java” is not the same as “JAVA”.

• Identifier names should not start with a digit.


• For example, “007IamFine” is an invalid identifier.

• Whitespace is not allowed inside an identifier.

• Keywords can’t be used as an identifier.


Literals
• Literals represent fixed values in a source code. These are similar to standard variables with the difference
that these are constant.

• These can be classified as an integer literal, a string literal, a boolean etc.

• The user defines these mainly to define constants.

• Syntax to define literals:

final data_type variable_name;

• There are five types of literals in Java:

• Integer
• Floating Point
• Boolean
• Character
• String
Example-1:
public class JavaIntegerLiterals {
public static void main (String[]args){
//initialize variables with integer literals
int decimalNum = 25;
int hexaNum = 0xa5;
int binaryNum = 0b1101;
int octalNum = 0172;

//print out the values of the literal


System.out.println("Decimal Integer: " + decimalNum);
System.out.println("Octal Integer: " + octalNum);
System.out.println("Hexadecimal Integer: " + hexaNum);
System.out.println("Binary Integer: " + binaryNum);
}
}

Output
Decimal Integer: 25
Octal Integer: 122
Hexadecimal Integer: 165
Binary Integer: 13
Example-2:
public class JavaBooleanLiterals {
public static void main(String[] args) {
boolean isTrue = true;
boolean isFalse =false;

System.out.println("The boolean value of isTrue: " + isTrue);


System.out.println("The boolean value of isFalse: " + isFalse);
}
}

Output
The boolean value of isTrue: true
The boolean value of isFalse: false
Example-3:
public class JavaFloatLiterals {
public static void main(String[] args) {
double piDoubleValue = 3.1415926535;
float piFloatValue = 3.1415926535F;
double piScientific = 3.1415926535e0;

System.out.println("Pi to ten decimal places: " + piDoubleValue);


System.out.println("A rounded value of Pi: " + piFloatValue);
System.out.println("Pi from a scientific notation: " + piScientific);
}
}

Output
Pi to ten decimal places: 3.1415926535
A rounded value of Pi: 3.1415927
Pi from a scientific notation: 3.1415926535
Example-4:
public class JavaCharLiterals {
public static void main(String[] args) {
char myFirstChar = 'a';
char mySecondChar = 'b';
char myThirdChar = 'c';
char plusInUnicode = '\u002b';

System.out.println("First three letters of the alphabet: "


+ myFirstChar + mySecondChar + myThirdChar);

//Escape sequence as character literals


System.out.println("The same three characters on separate lines: "
+ "\n" + myFirstChar
+ "\n\t" + mySecondChar
+ "\n" +myThirdChar);

//Unicode character literal


System.out.println("Letter a added to letter b: " +
myFirstChar + plusInUnicode + mySecondChar); Output
} First three letters of the alphabet: abc
} The same three characters on separate lines:
a
b
c
Letter a added to letter b: a+b
Operators in Java
• Operator in Java is a symbol that is used to perform operations.
• For example: +, -, *, / etc.
• There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative * / %
additive + -
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ? :
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Java Unary Operator

• The Java unary operators require only one operand. Unary operators are used to perform
various operations i.e.:
• incrementing/decrementing a value by one
• negating an expression
• inverting the value of a Boolean
public class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11) Output:
System.out.println(++x);//12 10
12
System.out.println(x--);//12 (11) 12
System.out.println(--x);//10 10
}
}
Cont..

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);
System.out.println(b++ + b++);
}
}

Output:
22
21
Cont..

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);
System.out.println(~b);
System.out.println(!c);
System.out.println(!d);
}
}
Output:
2-11
9
false
true
Java Arithmetic Operator

• Java arithmetic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
System.out.println(10*10/5+3-1*4/2); // 21
}
}

You might also like