Lexeme: The logical individual unit in Java programming is called a Lexeme.Token : The Collection of Lexemes under a particular group called Token.
EX:
int a = b + c * d;
int, a, = , b, +, c, *, d, ; —-> 9 Lexemes
int —--------> Data Type
a,b,c,d —-----> Variables
=,+,* —-------> Operators
; —-----------> Special Symbol
4 types of tokens
To prepare Java applications, JAVA has provided the following types of Tokens.
- Identifiers
- Literals
- Keywords / Reserved Words
- Operators
Identifiers
Identifier is a name assigned to the java programming elements like variables, methods, classes, abstract classes, interfaces, …. which we are using in java applications.
Identifiers Rules
To provide identifiers to the programming elements we must follow a set of rules and regulations.
1. Every Java identifier must NOT start with a number, it may start with an alphabet, _ symbol, $ symbol, but the subsequent symbols may be a number , an alphabet, _ symbol, $ symbol.
int eno = 111; —------------> Valid
String 9eid = "E-999"; —----> Invalid
String _eaddr = "Hyd"; —----> Valid
float $esal = 2500.0f; —----> Valid
String emp9Name = "AAA"; —--> Valid
String emp_Email = "mail@rndayala.com"; —--> Valid
float esalIn$ = 2500.0f; —--> Valid
2. All Java identifiers must not have spaces in the middle:
String emp Name = "Raghu"; —----> Invalid
String empName = "Raghu"; —-----> Valid
float emp Sal = 50000.0f; —-----> Invalid
float empSal = 50000.0f; —------> Valid
3. All Java identifiers must not allow operators:
int emp+Name = "Raghu"; —---> Invalid
String emp-Addr = "Hyd"; —--> INvalid
String emp_Addr = "Hyd"; —--> Valid
String emp%Email = "raghu@rndayala.com"; —--> Invalid
4. All Java identifiers must not allow special symbols except _ symbol and $ symbol.
int empNo = 111; —--------------> Valid
String emp@Hyd = "Durga"; —-----> Invalid
String #eaddr = "Hyd"; —--------> Invalid
float esal@$ = 25000.0f; —-----------> Invalid
String emp_Email="mail@rndayala.com"; —---> Valid
5. All Java identifiers must not use java keywords.
int a = 10; —-----------> Valid
int break = 10; —-------> Invalid
float for = 22.22f; —---> Invalid
String string = "abc"; —> Valid
long lang = 20L; —------> Valid
6. In Java applications, we can use all predefined classes names and interfaces names as identifiers.
int a = 10; —-----------> Valid
int Exception = 10; —---> Valid
float Thread = 22.22f; -→ Valid
String String = "String"; —---> Valid
Example :
class Test{
public static void main(String[] args)
{
int Exception = 10;
System.out.println(Exception);
float Thread = 22.22f;
System.out.println(Thread);
String String = "String";
System.out.println(String);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10
22.22
String
EX2 :
class Test{
public static void main(String[] args)
{
int System = 10;
System.out.println(System);
}
}
Status: Compilation Error
Reason: If we declare ‘System’ as an integer variable in the java application then we must use that System as the same integer variables only, we must not use that System as like its original class name. If we use it like its original class name then the compiler will raise an error.
In the above context, after declaring System as an integer variable if we want to use System as class name then we have to use “Fully Qualified name” to the System class.
Note: If we provide Classes names and interfaces names along with their respective package names are called Fully Qualified Names.
EX:
java.lang.System
java.util.ArrayList
java.io.BufferedReader
Example –
class Test{
public static void main(String[] args){
int System = 10;
java.lang.System.out.println(System);// 10
System = System + 10;
java.lang.System.out.println(System);// 20
System = System + 10;
java.lang.System.out.println(System);// 30
}
}
Identifiers Naming Conventions
Along with the above rules and regulations, JAVA has provided the following suggestions to use identifiers in java applications.
1. In Java applications, it is suggestible to provide identifiers with a particular meaning.
String xxx = “abc123”; —----> No Suggestible
String accNo = “abc123”;----> Suggestible
2. In Java, there is no length restriction over the identifiers, we can write identifiers with any length, but it is suggestible to manage the length of the identifiers up to around 10 symbols.
String temporaryemployeeaddress = “Hyd”; —--> Not Suggestible
String tempEmpAddr = “Hyd”; —---> Suggestible
3. In Java, if we have multiple words in a single identifier then it is suggestible to separate multiple words with the special notations like ‘_’ symbols.
String tempEmpAddr = “Hyd”; —-> Not Suggestible
String temp_Emp_Addr = “Hyd”;---> Suggestible
Literals
Literal is a constant value assigned to the variables.
int a = 10;
int —----> Data Type
a —------> Variable / Identifier
= —------> Operator
10 —-----> Constant[Literal]
;--------> Special Symbol
To prepare Java applications, JAVA has provided the following literals.
1. Integral Literals / Integer Literals
byte, short, int, long : 10, 20, 30,....
char : ‘a’, ‘b’, ‘\n’, ‘\t’,....
Note: To represent character literals we have to use ” [Single quotations] with single character.
Note: To represent Long values we have to use either ‘l’ or ‘L’ as a suffix to the number.
class Test{
public static void main(String[] args){
byte smarks = 76;// here 75 value come under byte type
System.out.println(smarks);
short productCost = 356;// 356 value come under short type
System.out.println(productCost);
int age = 25;// Here 25 come under int value
System.out.println(age);
// Due to the suffix L, the provided value is under long type.
long mobile = 9988776655L;
System.out.println(mobile);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
76
356
25
9988776655
2. Floating point Literals
float : 22.22f, 34.45F, 567.234f,....
Double: 345.333d, 567.345D, 876.345
Note: To represent float values we have to use either ‘f’ or ‘F’ as a suffix.
Note: To represent double values we have to use either ‘d’ or ‘D’ or no suffix value.
class Test{
public static void main(String[] args){
float temp = 32.25f;
System.out.println(temp);
float aggregateMarks = 7.8f;
System.out.println(aggregateMarks);
double salary = 45678.2345d;
System.out.println(salary);
double balance = 12345678.456D;
System.out.println(balance);
double profit = 4567.2345;
System.out.println(profit);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
32.25
7.8
45678.2345
1.2345678456E7
4567.2345
3. Boolean Literals
boolean : true, false
class Test{
public static void main(String[] args){
boolean b1 = true;
boolean b2 = false;
System.out.println("Today is Sunday? : " + b2);
System.out.println("Today Class Is Core Java? : " + b1);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
Today is Sunday? : false
Today's Class Is Core Java? : true
4. String Literals
String : “abc”, “abc@123”,...
Note: In Java applications, to represent String literals, we have to use “” [Double Quotations].
Note: In JAVA / J2EE only String data type is able to represent all the types of data like characters, Digits, Special Symbols,….
class Test{
public static void main(String[] args){
String uname = "Raghu";
String upwd = "Raghu@12345";
String uemail = "raghu123@rndayala.com";
String umobile = "91-99xxxx6655";
String website = "http://www.rndayala.com";
System.out.println(uname);
System.out.println(upwd);
System.out.println(uemail);
System.out.println(umobile);
System.out.println(website);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
Raghu
Raghu@12345
raghu123@rndayala.com
91-99xxxx6655
http://www.rndayala.com
Note: In java, we are able to provide single quotations directly inside the double quotations, but if we want to provide double quotations inside the double quotations then we have to ‘\’ prefix for the double quotations. [escape characters]
class Test{
public static void main(String[] args){
String data1 = "Raghu'Software' Solutions";
System.out.println(data1);
String data2 = "Raghu \"Software\" Solutions";
System.out.println(data2);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
Raghu 'Software' Solutions
Raghu "Software" Solutions
Readability – Using underscore operator
In Literals, to improve readability, the JAVA7 version has provided a flexibility to the developers to include ‘_’ symbols inside the numbers.
class Test{
public static void main(String[] args){
double d = 12_34_56_789.3456787;
System.out.println(d);
}
}
If we compile the above program then the compiler will recognize all the ‘_’ symbols and the compiler will remove all the ‘_’ symbols from the literals and the Compiler and JVM will process that number as a normal number.
Number System
In All the programming languages to represent numbers we have to follow some standardizations called Number Systems.
There are four Number Systems.
- Binary Number Systems
- OCtal Number Systems
- Decimal NUmber Systems
- Hexa-Decimal Number Systems
In Java, all the above numbers are allowed, but the default number system in Java is “Decimal Number System”.
Binary Number System
Base : 2Alphabet: 0,1Prefix : 0b or 0B
class Test{
public static void main(String[] args){
// 0*2P0 + 1*2p1 ==> 0 + 1*2 ==> 2
int a = 0b10;
System.out.println(a);// 2
// 0*2p0 + 1*2p1 + 0*2p2 + 1*2p3 ==> 0 + 2 + 0 + 8 ==> 10
int b = 0B1010;
System.out.println(b);// 10
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
2
10
Note: Binary Number Systems was introduced by JAVA 1.7 version, it was not supported by JAVA up to 1.6 version.
Octal Number System
Base : 8
Alphabet : 0,1,2,3,4,5,6 and 7.
Prefix : 0[zero]
public class Test{
public static void main(String[] args) {
int a = 010;//0*8p0 +1*8p1 ==> 0+8 ==> 8
System.out.println(a);// 8
}
}
D:\java10>javac Test.java
D:\java10>java Test
8
Q) Find the Number Systems for the following numbers?
int a = 10;——–> Decimal Number Systemint b = O10;——-> Invalidint c = 00;——–> Octal Number Systemint d = 0;———> Decimal Number System
Decimal Number System
Base : 10
Alphabet : 0,1,2,3,4,5,6,7,8 and 9.
Prefix : No Prefix
It is a default Number System in Java.
Hexa-Decimal Number System
Base : 16
Alphabet : 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f
Prefix : 0x or 0X
public class Test{
public static void main(String[] args) {
int a = 0x10; //0*16p0 + 1*16P1 ==> 0+16 ==> 16
System.out.println(a);// 16
int b = 0X12; // 2*16p0 + 1*16P1 ==> 2 + 16 ==> 18
System.out.println(b);// 18
}
}
D:\java10>javac Test.java
D:\java10>java Test
16
18
Examples
Q)Find the valid statements from the following list?
int a = 10; —--------> Valid
int b = 0b1010; —----> Valid
int c = 0B1100; —----> Valid
int d = 0b1020; —----> Invalid
int e = ob1110; —----> Invalid
int a = 0123; —------> Valid
int b = O3456; —-----> Invalid
int c = 0O123456; —--> Invalid
int d = 05678; —-----> Invalid
int e = 00123;----> Valid
int a = 0x123456; —----> Valid
int b = 0X789abcd; —---> Valid
int c = 0x567efg; —----> Invalid
int d = OX1232abcdef;--> Invalid
Q) Find the Valid statements from the following List?
int a = 0b1010; —----> Valid
int b = 0678; —------> INvalid
int c = 10234; —-----> Valid
int d = 0x345gabcd; —> Invalid
Note: In Java applications, if we represent any number in any number system then the compiler will convert the provided number from its number system to decimal number system, where JVM will provide output values in the same decimal number system.
In Java applications, we can perform all Arithmetic operations over the numbers which are in any number system.
public class Test{
public static void main(String[] args) {
// 0*2P0+1*2P1+0*2P2+1*2P3 + 1*8P0+1*8P1 ==> 10 + 9 ====> 19
int result1 = 0b1010 + 011;
// 0*8P0+1*8P1 + 10*16P0+1*16P1 ==> 8 + 26 ======> 34
int result2 = 010 + 0x1a;
System.out.println(result1);// 19
System.out.println(result2);// 34
}
}
D:\java10>javac Test.java
D:\java10>java Test
19
34
Keywords / Reserved Words
Keyword: If Any predefined word has word recognition and internal functionality then that predefined word is called a Keyword.
To prepare java applications, JAVA has provided the following keywords.
- Data Types and Return Types:
byte, short, int, long, float, double, boolean, char, void
- Access Modifiers:
public, protected, private , static, final, abstract, native, volatile, synchronized, strictfp, transient....
- Flow controllers:
if, else, switch, case, default, form while, do, break, continue, return ,....
- Class / Object Related Keywords:
class, extends, interface, implements, new, this, super, package, import, enum....
- Exception Handling Related Keywords:
throws, throw, try, catch, finally
Reserved Words: If any predefined word that has only word recognition and that does not have internal functionality then that predefined word is called Reserved Word.
EX: goto, const
Note: In Java applications, we are able to use keywords as per the requirements, but we are unable to use Reserved Words, if we use Reserved words in Java applications then Compiler will raise an error.
Operators
Operator is a Symbol, it is able to perform a particular operation on the provided Operands.
To prepare Java Applications, JAVA has provided the following types of operators.
- Arithmetic Operators:
+, -, *, /, %, ++, – -
- Assignment Operators:
=, +=, -=, *=, /=, %=
- Comparison Operators:
==, !=, <, <=, >, >=
- Logical Boolean Operators:
&, |, ^
- Logical Bitwise Operators:
&, |, ^, <<, >>
- Ternary Operator
Expr1?Expr2: Expr3;
- Short-Circuit Operators:
&&, ||
Arithmetic Operators
public class Test{
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
}
}
D:\java10>javac Test.java
D:\java10>java Test
13
7
30
3
1
Increment And Decrement Operators
Increment Operators:
These operators are able to increment the value by 1. There are two types of Increment Operators.
- Pre-increment
- Post increment
Pre–increment: It is able to increment the value first then the incremented value will be utilized in the java expression.
Syntax: ++varName
Post Increment: It is able to increment the value after using the present value in the expression.
Syntax: VarName++
Decrement Operators:
These operators are able to decrement the value by 1. There are two types of Decrement operators.
- Pre Decrement
- Post Decrement
Pre Decrement: It is able to decrement the value first then the decremented value will be used in the expression.
Syntax: --VarName
Post Decrement: It is able to decrement the value after using the current value in the expression.
Syntax: varName–-
public class Test{
public static void main(String[] args) {
int a = 5;
System.out.println(++a);
System.out.println(a);
System.out.println();
int b = 5;
System.out.println(b++);
System.out.println(b);
System.out.println();
int c = 5;
System.out.println(--c);
System.out.println(c);
System.out.println();
int d = 5;
System.out.println(d--);
System.out.println(d);
}
}
D:\java10>javac Test.java
D:\java10>java Test
6
6
5
6
4
4
5
4
class Test{
public static void main(String[] args){
int a = 5;
System.out.println(++a-a++);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
0
class Test{
public static void main(String[] args){
int a = 6;
System.out.println(--a+--a-++a);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
4
class Test{
public static void main(String[] args){
int a = 10;
System.out.println(a);// 10
System.out.println(a++);// 10
System.out.println(--a);// 10
System.out.println(a--);// 10
System.out.println(++a);// 10
System.out.println(a);// 10
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10
10
10
10
10
10
Compound Assignment operators
EX:
class Test{
public static void main(String[] args){
int a = 10;
System.out.println(a);// 10
a += 2;// a = a + 2 ==> a = 10 + 2; ==> a = 12
System.out.println(a);// 12
a -= 2;// a = a - 2; ==> a = 12 - 2 ==> a = 10
System.out.println(a);// 10
a *= 2;// a = a * 2; ==> a = 10 * 2 ==: a = 20
System.out.println(a);// 20
a /= 2;// a = a / 2; ==> a = 20 / 2; ==> a = 10
System.out.println(a);// 10
a %= 2;// a = a % 2 ==> a = 10 % 2; ==> a = 0
System.out.println(a);// 0
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10
12
10
20
10
0
Logical Boolean Operators
A B A&B A|B A^B
—-----------------------
T T T T F
T F F T T
F T F T T
F F F F F
class Test{
public static void main(String[] args){
boolean b1 = true;
boolean b2 = false;
System.out.println(b1&b1);// true
System.out.println(b1&b2);// false
System.out.println(b2&b1);// false
System.out.println(b2&b2);// false
System.out.println(b1|b1);// true
System.out.println(b1|b2);// true
System.out.println(b2|b1);// true
System.out.println(b2|b2);// false
System.out.println(b1^b1);// false
System.out.println(b1^b2);// true
System.out.println(b2^b1);// true
System.out.println(b2^b2);// false
}
}
EX:
A B A&B A|B A^B
—-----------------------
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
class Test{
public static void main(String[] args){
int a = 10;
int b = 2;
System.out.println(a&b);
System.out.println(a|b);
System.out.println(a^b);
System.out.println(a<<b);
System.out.println(a>>b);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
2
10
8
40
2
int a = 10; —--> 1010
int b = 2; —--> 0010
—----------------------
a&b —----------> 0010 —--> 2
a|b —----------> 1010 —--> 10
a^b —----------> 1000 —--> 8
Shift operators
a<<b —> 10 << 2 —---> 00001010
00101000 —-> 40
a>>b —-> 10 >> 2 —→ 00001010
00000010 —-----> 2
Ternary Operator
Expr1? Expr2: Expr3;
class Test{
public static void main(String[] args){
int a = 5;
String str = a%2==0?"EVEN Number":"ODD Number";
System.out.println(str);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
ODD Number
class Test{
public static void main(String[] args){
int a = 10;
int b = 20;
int min = a<b?a:b;
int max = a>b?a:b;
System.out.println(a);
System.out.println(b);
System.out.println("MIN : "+min);
System.out.println("MAX : "+max);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10
20
MIN : 10
MAX : 20
Short-Circuit Operators
The main purpose of Short circuit operators is to improve Java applications performance.
EX: &&, ||
Q) What is the difference between | and || ?
In the case of the Logical-OR ‘||’ operator, if the first operand value is true then it is not required to evaluate the second operand value, directly we can decide the result of the Logical-OR operator is true.
In the case of bitwise-OR ‘|’ operator, even though the first operand value is true then JVM must evaluate the second operand value and JVM will decide the result of ‘| operator, in this case evaluating second operand value is unnecessary, it will increase application execution time, it will reduce application performance.
In the case of logical-OR ‘||’ operator, if the first operand value is true then JVM will not evaluate the second operand value , JVM will decide the result of the ‘||’ operator is true, it will reduce application execution time and it will improve application performance.
Note: If the first operand value is false then it is mandatory for JVM to evaluate both first operand and second operand inorder to find the result of the Logical-OR operator.
EX:
class Test{
public static void main(String[] args){
int a = 10;
int b = 10;
if(a++ == 10 | b++ == 10) {
System.out.println(a+" "+b);// 11 11
}
int c = 10;
int d = 10;
if(c++ == 10 || d++ == 10) {
System.out.println(c+" "+d);// 11 10
}
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
11 11
11 10
Q) What is the difference between & and && ?
In the case of ‘Logical-AND’ operation , if the first operand value is false then it is not required to evaluate the second operand value , directly we can get the result that the ‘Logical-AND’ operator is false.
IN the case of the bitwise-AND ‘&’ operator, JVM must evaluate both the operands in order to find the result of the bitwise-AND operator is false .
In the case of the ‘&&’ operator, if the first operand value is false then JVM will find the result of the Logical-AND operator is false without checking the second operand value.
class Test{
public static void main(String[] args){
int a = 10;
int b = 10;
if(a++ != 10 & b++ != 10){
}
System.out.println(a+" "+b);
int c = 10;
int d = 10;
if(c++ != 10 && d++ != 10){
}
System.out.println(c+" "+d);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
11 11
11 10