Java Fundamentals

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:

  1. Reserved Words
  2. Identifiers
  3. Literals
  4. Operators
  5. 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:

  1. Keywords
  2. Literals

Keywords define the functionalities and literals define a values.

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 package
private protected public return short
static strictfp super switch synchronized
this throw 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:

  1. Integeral Literals
  2. Float Literals
  3. Char Literals
  4. Boolean Literals
  5. Null Literals

1. Integeral Literals

There are 4 way to use Integeral Literals

i. Decimal Literals (Base 10)

  • In this case allow digits are 0-9.
  • Example : int a=121; int b=101; int c=201; int d=643943; etc.

ii. Octal Literals (Base 8)

  • 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.

iii. Hexadecimal Literals (Base 16)

  • 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.

iv. Binary Literals

  • 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.

2. 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;

3. Char Literals

  • There are 4 way to specify Char Literals.
  • i. Single Quote

    • In this you can specify only single char within single quotes.
    • For Example: char a='a'; char b='1'; etc.

    ii. 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.

    iii. 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.

    iv. Escape Literals

    • We can specify Escape character as char literals .
    • For Example: char a='\t'; char b='\n'; etc.

4. String Literals

String is a group of characters.

String Literals always represent by double quotes ("").

For Example: String name="Deep Singh"; String msg="Welcome to \n deepsingh44.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:

  1. Unary Operators
  2. Arithmetic Operators
  3. Relational Operators
  4. Bitwise Operators
  5. Logical Operators
  6. Ternary Operators
  7. Assignment Operators
  8. Instanceof Operators
  9. 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){}

1 comment:

Sarika A said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care. We are providing the best services click on below links to visit our website.
Oracle Fusion HCM Training
Workday Training
Okta Training
Palo Alto Training
Adobe Analytics Training