Skip to main content

 

Identifiers, Literals, Operators and Separators in Java Programming Language


Here is a clear introduction (exam-ready + student-friendly) to Identifiers, Literals, Separators, and Operators in programming (especially Java/C-style languages).

🔹 Identifiers

An identifier is the name given to a variable, method, class, array, or any user-defined item in a program. It helps identify these elements uniquely.

Rules for Identifiers (Java)

  • Must begin with a letter (A–Z or a–z), underscore (_), or dollar sign ($)
  • Cannot start with a number
  • Cannot be a reserved keyword
  • Case-sensitive (total and Total are different)
  • No spaces allowed

Examples

Valid Identifiers

int age;
double totalMarks;
String studentName;
int _count;

Invalid Identifiers

int 2value;      // starts with number
int total marks; // contains space
int class;       // keyword

👉 Identifiers make programs readable and meaningful.

🔹 Literals

         A literal is a fixed value written directly in the program. It does not change during execution. It is nothing but a synthetic representation of boolean, character, numeric, or string data

1.      Integral Literals

2.      Floating-Point Literals

3.      Char Literals

4.      String Literals

5.      Boolean Literals

6.      Null Literals

Types of Literals (Java)

  1. Integer Literal

Integer Literal doesn’t have the prefix characters.

Example: int x = 108;

2.  Octal Literal

Starts with ‘0’ (Zero)

Example: int x = 0745;

3.  Hexa decimal Literal:

Starts with ‘0X’ (zero & X)

Example: int x = 0X123Fadd;

4.  Binary Literal:

starts with zero and b

Example:int x = 0b1011;

Sample Program

public class Test_int_literal

{

public static void main(String[] args)

{

// decimal-form literal

int a = 101;

// octal-form literal

int b = 0100;

// Hexa-decimal form literal

int c = 0xFace;

// Binary literal

int d = 0b1111;

System.out.println(a);

System.out.println(b);

System.out.println(c);

System.out.println(d);

}}

Output



  1. Floating-Point Literal

Floating-point literals are literal in programming that represent real numbers (numbers with a decimal point) using the floating-point notation. 

 

Number N1= 89d;

Number N2= 3.14159e0; // it is in scientific notation. After ‘e’ is //power of 10 (100)

 

Number N3= 1.0e-6D; //it is in scientific notation 1.0 * 10-6

 

float f = 3.14f;
double d = 12.56;

 

public class Test_decimal_literal {

public static void main(String[] args)

{

// decimal-form literal

float a = 101.230f;

// It also serves the purpose of a decimal literal

float b = 0123.222f;

// Hexa-decimal form

float c = 0x1.23p4f; 

double d = 0xA.BCp2; 

float e = 0x123p0f;

System.out.println(a);

System.out.println(b);

System.out.println(c);

} }

Output:




  1. Character Literal

Character (Char) literals have the type char and are an unsigned integer primitive type. They are constant value character expressions in the Java program. These are sixteen-bit Unicode characters that range from 0 to 65535. Char literals are expressed as a single quote, a single closing quote, and the character in Java.    

 

Example.

char ch = 062;

char ch = ‘\n’;

char ch = ‘\u0061’;

char ch = 'A';
char symbol = '#';

 

public class Test {

public static void main(String[] args)

{

char ch = 'a'; // single character literal within a single quote

char b = 0771; // It is an Integer literal with an octal form

char c = '\u0061'; // Unicode representation

System.out.println(ch);

System.out.println(b);

System.out.println(c);

// Escape character literal

System.out.println("\" is a symbol");

}}

Output

  1. String Literal

A sequence of (zero or more including Unicode characters) characters within double quotes is referred to as string literals. 

 

For example,

String s = “Hello”;

String name = "Ravi";

Some character, its ACII and Unicode values

 

Name 

Character

ASCII

Hex

Single quote

\’

39

0x27

Double quotes

\”

34

0x22

Carriage control

\r

13

0xd

Backlash 

\\

92

0x5c

Newline 

\n

10

0x0a

NUL character

\0

0

0x00

Backspace

\b

8

0x08

TAB

\t

9

0x09

 

class Test_string_literals{

public static void main(String s[])

{

   String s1="Hello";

   String s2="Madam or Ma\'dam";

   String s3="Dear Madam\\Sir,";

   String s4="Hi! \n Bye..";

   String s5="Sno\tName";

   System.out.println("s1 -->"+s1);

   System.out.println("s2 -->"+s2);

   System.out.println("s3 -->"+s3);

   System.out.println("s4 -->"+s4);

   System.out.println("s5 -->"+s5);

}

}

Output:

 


8.     
Boolean Literal

 Boolean literals allow only two values and thus are divided into two literals:

 True: it represents a real boolean value

False: it represents a false boolean value 

 

For example, 

boolean b = true;

boolean d = false;

boolean flag = true;

🔹 Separators

Separators are symbols used to separate program elements and define structure.

Symbol of Separator

Name of Separator

Definition

;

Semicolon

A semicolon as a separator is used to mark the end of the statement in java. Whenever

 we encounter a semicolon after a statement, it means that the statement has ended and the next statement is a different one.

()

Parentheses

Parentheses are used to define the parameters of the methods in java. We use parentheses while typecasting also. Parentheses are also the most commonly used for containing the expressions in the if-else statements.

{}

Braces

Braces are used to define a code block. They are also used to define the scopes of variables. We often use braces for depicting the self-initialized arrays.

[]

Brackets

Brackets are used to declare an array in java.

.

Period

Period is used to distinguish the package names from sub-packages. They are also used to distinguish a variable from a reference variable.

,

Comma

It is used to select a field from an object. We often declare the variables in a single line. The comma is used here to distinguish these variable names from each other.

:

Colon

Colon is used to create a method reference. It is also used for the constructor reference. We often use colons after labels.

Example

int a = 10, b = 20;
if (a < b) {
    System.out.println(a);
}

👉 Separators organize the program and make it syntactically correct.

🔹 Operators

An operator is a symbol that performs an operation on one or more operands (values or variables).

Types of Operators

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

= += -= *= /= %= &= ^= |= <<= >>= >>>=

 1. Arithmetic Operators

Perform mathematical operations.

Operator

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus

int sum = a + b;

 2. Relational Operators

Compare values and return true/false.

Operator

Meaning

==

Equal to

!=

Not equal

> 

Greater than

< 

Less than

>=

Greater or equal

<=

Less or equal

 3. Logical Operators

Combine conditions.

Operator

Meaning

&&

AND

!

NOT

 4. Assignment Operators

Assign values to variables.

int x = 5;
x += 2;   // x = x + 2

 5. Increment / Decrement Operators

Increase or decrease value by 1.

x++;  // increment
x--;  // decrement

 Twisted interview questions

Twisted interview questions

int x = 5;
int y = x++ + ++x;
System.out.println(y); 

Output: 12

 

int a = 1;

int b = a++ + a++ + ++a;

System.out.println(b); 

Output: 7

 

int x = 0;

if (true || ++x > 0) { }

System.out.println(x); 

Output: 0

 

int x = 0;

if (true | ++x > 0) { }

System.out.println(x);                              1

int x = 5;

x = x++;

System.out.println(x);

Output: 5

 

String s1 = "java";
String s2 = "java";
System.out.println(s1 == s2);

Output: True

 

System.out.println("5 + 5 = " + 5 + 5);

System.out.println("5 + 5 = " + (5 + 5));

Output:

55

10

int x = 5;
System.out.println(x > 2 ? x < 4 ? 10 : 20 : 30);    

Output:

20

 

System.out.println(10 / 0);

System.out.println(10.0 / 0);

Output     

Runtime Error

Infinity

 

System.out.println(1 << 3 + 1);                    

Output: 16

System.out.println(true ^ false);
System.out.println(true ^ true);                   

Output: true
false

System.out.println(-10 % 3);                       

Output: -1

 

 


Comments

Popular posts from this blog

How to pass an object as an argument in Java programming? We know that the concept of calling a method as pass by value and pass by reference. Java allows us to pass primitive types of data when calling a method that is called as 'a method call by value' or 'passed by value'. In the same manner, we can create an object by passing primitive type of data using constructors.  Java allows us to create an object by passing reference of another object ie. One Object can be passed to another object. This is called as 'call by reference' or 'passed by reference'.  Syntax     <class name> <object name1>=new <class name>;      <class name> <object name2> ;     <object name2>=<object name1>; Example Program class test {      int i;      test(int a)      {      i=a;      }      int testmethod(test t)     ...
  Introduction to Java (For B.Tech/BCA Students) Java is a powerful, high-level programming language widely used in academic learning and professional software development. Developed in the mid-1990s, Java was designed with the goal of creating programs that can run on different types of computers without needing major changes. This platform independence is achieved through the concept of “write once, run anywhere,” making Java especially valuable in today’s diverse computing environment. For B.Tech students, Java serves as an excellent foundation for understanding core programming concepts. It fully supports object-oriented programming (OOP), including classes, objects, inheritance, polymorphism, abstraction, and encapsulation. These concepts are essential for building structured, reusable, and scalable software systems, which are central to modern computer science and engineering education. Java programs are compiled into an intermediate form called bytecode, which runs on th...