Module 1
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,
• 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;
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;
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;
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';
• 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..
Output:
22
21
Cont..
• Java arithmetic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.