Java Fundemental

Java Tokens
Java Tokens are the smallest unit of a Java program.
The Java program is constructing expressions and statements using Tokens.
Java program is a group of different types of tokens, comments, and white spaces.
There are various tokens used in Java:
- Reserved Words
- Identifiers
- Literals
- Operators
- Separators
1. Reserved Words
Any language provide some Reserved words for the compiler to know the functionalities define by that language.
Every Reserved words have a special meaning that understand by the compiler.
Reserved words are define into two category:
- Keywords
- Literals
There are around 50 Keywords and 3 literals.
Keywords can never be used as an Identifiers because compiler treat as a Reserved words.
There are following Reserved words:
abstract | assert | boolean | break | byte |
case | catch | char | class | continue |
default | do | double | else | enum |
extends | final | finally | float | for |
if | implements | import | instanceof | int |
interface | long | native | new | ackage |
private | protected | public | return | short |
static | strictfp | super | switch | synchronized |
this | hrow | throws | transient | try |
void | volatile | while | true | false |
null |
2. Identifiers
Identifiers are used to identify the things.
Identifiers are used with class, method, variable, interface and enum.
There are following rules to define an Identifier:
- Valid Identifiers
- Identifier should be start with characters.
- Identifier can be start with $ symbol.
- Identifier can be start with underscore (_).
- Identifiers are the case-senstive like int and Int is different.
- Identifier name length should be minimum 3 or 4 and maximum 15.
- class Identifier name all word's first character should be in Upper Case like My, MyClass, HelloWorldExample etc.
- method Identifier name should be in Camel Case like get(), getName(), getByRoll() etc.
- variable Identifier name should be in small letters like roll, roll_number etc.
- In-Valid Identifiers
- Identifiers never start from number.
- Identifiers never have special symbols except of $. Because it represent a currency.
- Identifier never have hyphen (-).
- Identifier never have a space.
- Identifier never be Reserved Words.
- Identifier never have Operators like (+,-,*,/) etc.
3. Literals
Any constant value which is assigned to a variable that is called literal.
There are following types of literals:
- Integeral Literals
- Float Literals
- Char Literals
- Boolean Literals
- Null Literals
- Integeral Literals
- Decimal Literals (Base 10)
- Octal Literals (Base 8)
- Hexadecimal Literals (Base 16)
- Binary Literals
- Float Literals
- In this case allow only Decimal 0-9.
- You can not specify Octal and Hexadecimal in Float Literals.
- In this case Octal also act as Decimal.
- Example : float a=101.10f; float b=0101.9f; (Octal acts as Decimal) etc.
- Hexadecimal can be not secify. float c=0X1.4f;
- Char Literals
- Single Quote
- In this you can specify only single char within single quotes.
- For Example: char a='a'; char b='1'; etc.
- Integral Literals
- In this you can specify Integral Literals which represents UNICODE values.
- Integeral Literals can be specify by Decimal, Octal, Hexadecimal etc.
- This UNICODE range start from 0 to 65535.
- For Example: char a=012; char b=101; char c=0x2f; etc.
- UNICODE Literals
- We can specify char literals in Unicode representation ‘\uxxxx’.
- Here xxxx represents four Hexadecimal numbers.
- For Example: char a='\u0000'; char b='\u0076'; etc.
- Escape Literals
- We can specify Escape character as char literals.
- For Example: char a='\t'; char b='\n'; etc.
There are 4 way to use Integeral Literals
In this case allow digits are 0-9.
Example : int a=121; int b=101; int c=201; int d=643943; etc.
The Octal number always start from 0.
In this case allow digits are 0-7.
Example : int a=0121; int b=0101; int c=0201; int d=064343; etc.
In this case allow digits are 0-9 and allow characters are a-f either Lower or Upper case.
The Hexadecimal number always start from 0x or 0X.
Example : int a=0x121; int b=0X101; int c=0x201; int d=0X6d343; etc.
In this case allow digits are 0 and 1.
Literals value should be prefixed with 0b or 0B.
Example : int a=0b101; int b=0B0101; int c=0b111; etc.
There are 4 way to specify Char Literals.
4. String Literals
String is a group of characters.
String Literals always represent by double quotes ("").
For Example: String name="Sushil Sharma"; String msg="Welcome to \n GyanTeps.blogspot.com"; etc.
5. Boolean Literals
Boolean Literals supports only two values.
In Java boolean value can be true or false.
For Example: boolean status=true; boolean a=false;
4. Operators
Operators are used to perform mathematical or logical operations.
There are following Operators in Java:
- Unary Operators
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Ternary Operators
- Assignment Operators
- Instanceof Operators
- Shift Operators
5. Separators/Punctuator
Punctuator | Name | Description | Example |
---|---|---|---|
() | Small Bracket | Use for expression, parameter, argument etc. | int i=(1+3/5)+2; void add(int i,int j){} add(1,2); |
{} | Curly Bracket | Use for body, array initializer etc. | void add(int i,int j){} int [] rolls={1,2,3,4}; |
[] | Square Bracket | Use for array declaration etc. | int [] rolls={1,2,3,4}; |
, | Comma | Use for parameter separators or value separators etc. | void add(int i,int j){} int [] rolls={1,2,3,4}; |
: | Colon | Use for switch case separators, enhance for loop, ternary operator etc. | switch() case : for(String s:list){} (condition)?right:wrong; |
; | Semicolon | Use for statement separators etc. | System.out.println("Hello World"); int [] rolls={1,2,3,4}; |
... | var-args | Use for similiar like array type of declaration etc. | void add(int i,int...j){} |
0 Comments