Introduction
Java program छोटे-छोटे meaningful parts से मिलकर बनता है। इन छोटे parts को tokens कहा जाता है। Token Java program की सबसे छोटी unit होती है जिसे compiler पहचानता है।
Java में statements instructions होते हैं जिन्हें computer execute करता है। किसी भी Java program में tokens की help से statements बनते हैं और statements की help से पूरा program तैयार होता है।
Simple Definition of Tokens
Tokens are the smallest individual units of a Java program that are identified by the compiler.
Hindi Meaning
Tokens Java program की सबसे छोटी meaningful units होती हैं जिन्हें compiler पहचानता है।
Simple Definition of Statements
Statements are instructions written in a program to perform a specific task.
Hindi Meaning
Statements program में लिखे गए instructions होते हैं जो किसी specific task को perform करते हैं।
Types of Java Tokens
Java में मुख्य रूप से ये tokens होते हैं:
- Keywords
- Identifiers
- Literals
- Operators
- Separators
- Comments
Keywords
Keywords Java के reserved words होते हैं। इनका meaning पहले से fixed होता है। Keywords को variable name, class name या method name के रूप में use नहीं किया जा सकता।
Examples of Keywords
class
public
static
void
int
if
else
for
while
return
new
Example Program
class KeywordDemo {
public static void main(String[] args) {
int age = 20;
System.out.println(age);
}
}
Output
20
Explanation
इस program में class, public, static, void और int Java keywords हैं।
Identifiers
Identifiers वे names होते हैं जो programmer variables, classes, methods और objects को देता है।
Examples of Identifiers
Student
age
marks
showData
studentName
Rules for Identifiers
- Identifier letter, digit, underscore
_या dollar sign$से बन सकता है। - Identifier digit से start नहीं हो सकता।
- Identifier में space नहीं हो सकता।
- Java keywords को identifier के रूप में use नहीं कर सकते।
- Java case-sensitive है, इसलिए
ageऔरAgeअलग-अलग identifiers हैं।
Valid Identifiers
name
studentName
marks1
_total
$amount
Invalid Identifiers
1name
student name
class
int
roll-no
Example Program
class IdentifierDemo {
public static void main(String[] args) {
int rollNo = 101;
String studentName = "Rahul";
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + studentName);
}
}
Output
Roll No: 101
Name: Rahul
Literals
Literals fixed values होते हैं जो program में directly लिखे जाते हैं।
Examples of Literals
10
25.5
'A'
"Java"
true
false
Types of Literals
- Integer Literal
- Floating-point Literal
- Character Literal
- String Literal
- Boolean Literal
- Null Literal
Integer Literal
Integer literal whole number value होती है।
int age = 20;
यहाँ 20 integer literal है।
Floating-point Literal
Floating-point literal decimal value होती है।
double price = 99.50;
यहाँ 99.50 floating-point literal है।
Character Literal
Character literal single character value होती है जो single quotes में लिखी जाती है।
char grade = 'A';
यहाँ 'A' character literal है।
String Literal
String literal characters का group होता है जो double quotes में लिखा जाता है।
String name = "Rahul";
यहाँ "Rahul" string literal है।
Boolean Literal
Boolean literal केवल दो values रखता है:
true
false
Example
boolean isPass = true;
यहाँ true boolean literal है।
Literal Example Program
class LiteralDemo {
public static void main(String[] args) {
int age = 20;
double marks = 85.5;
char grade = 'A';
String name = "Rahul";
boolean pass = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
System.out.println("Grade: " + grade);
System.out.println("Pass: " + pass);
}
}
Output
Name: Rahul
Age: 20
Marks: 85.5
Grade: A
Pass: true
Operators
Operators symbols होते हैं जो variables और values पर operations perform करते हैं।
Examples of Operators
+
-
*
/
=
==
>
<
&&
||
Example Program
class OperatorDemo {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
}
}
Output
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Separators
Separators symbols होते हैं जो program के different parts को अलग करने के लिए use होते हैं।
Common Separators in Java
| Separator | Use |
|---|---|
( ) | Method parameters या expressions के लिए |
{ } | Class, method और block के लिए |
[ ] | Array के लिए |
; | Statement end करने के लिए |
, | Multiple values separate करने के लिए |
. | Class या object members access करने के लिए |
Example
System.out.println("Hello Java");
इसमें . , ( ) और ; separators हैं।
Comments
Comments program को समझाने के लिए लिखे जाते हैं। Compiler comments को execute नहीं करता। Comments से program readable बनता है।
Single Line Comment
// This is a single line comment
Multi Line Comment
/*
This is a multi-line comment
*/
Documentation Comment
/**
* This is a documentation comment
*/
Comment Example Program
class CommentDemo {
public static void main(String[] args) {
// This statement prints a message
System.out.println("Comments are useful in Java");
}
}
Output
Comments are useful in Java
Java Statements
Java statements complete instructions होते हैं जो program में कोई task perform करते हैं। Java में generally हर statement semicolon ; से end होता है।
Example
int a = 10;
System.out.println(a);
यह दोनों Java statements हैं।
Types of Statements in Java
Java में मुख्य रूप से ये statements होते हैं:
- Declaration Statement
- Assignment Statement
- Expression Statement
- Control Statement
- Output Statement
- Block Statement
Declaration Statement
Declaration statement variable declare करने के लिए use होता है।
int age;
String name;
Assignment Statement
Assignment statement variable में value assign करने के लिए use होता है।
age = 20;
name = "Rahul";
Declaration with Assignment
Variable declare और assign एक साथ भी किया जा सकता है।
int age = 20;
String name = "Rahul";
Expression Statement
Expression statement किसी calculation या operation को perform करता है।
sum = a + b;
count++;
Control Statement
Control statement program के flow को control करता है। जैसे if, else, switch, for, while आदि।
if (age >= 18) {
System.out.println("Eligible to vote");
}
Output Statement
Output statement screen पर result print करने के लिए use होता है।
System.out.println("Hello Java");
Block Statement
Curly braces { } के अंदर लिखे गए statements के group को block statement कहते हैं।
{
int a = 10;
System.out.println(a);
}
Statement Example Program
class StatementDemo {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println("Sum = " + sum);
}
}
Output
Sum = 30
Explanation
इस program में int a = 10;, int b = 20; और int sum = a + b; statements हैं। System.out.println() output statement है।
Difference Between Tokens and Statements
| Tokens | Statements |
|---|---|
| Tokens program की smallest units होती हैं। | Statements complete instructions होते हैं। |
| Compiler tokens को identify करता है। | JVM statements को execute करता है। |
| Keywords, identifiers, literals, operators tokens हैं। | Declaration, assignment और control instructions statements हैं। |
| Tokens मिलकर statements बनाते हैं। | Statements मिलकर program बनाते हैं। |
Example: Tokens in a Statement
int age = 20;
इस statement में tokens हैं:
int keyword
age identifier
= operator
20 literal
; separator
Common Mistakes
Keyword को Identifier बनाना
Wrong:
int class = 10;
Correct:
int marks = 10;
Semicolon भूल जाना
Wrong:
int age = 20
Correct:
int age = 20;
String में Double Quotes भूलना
Wrong:
String name = Rahul;
Correct:
String name = "Rahul";
Character में Single Quotes भूलना
Wrong:
char grade = A;
Correct:
char grade = 'A';
Exam-Oriented Short Answer
Q. What are Java tokens and statements?
Java tokens program की smallest meaningful units होती हैं जिन्हें compiler identify करता है। Java में keywords, identifiers, literals, operators, separators और comments tokens कहलाते हैं। Statements complete instructions होते हैं जो किसी specific task को perform करते हैं। Java statements generally semicolon से end होते हैं। Tokens मिलकर statements बनाते हैं और statements मिलकर पूरा Java program बनाते हैं।
Important Points to Remember
- Token Java program की smallest unit होती है।
- Compiler tokens को identify करता है।
- Keywords reserved words होते हैं।
- Identifiers programmer द्वारा दिए गए names होते हैं।
- Literals fixed values होती हैं।
- Operators operations perform करते हैं।
- Separators program parts को separate करते हैं।
- Comments program explanation के लिए use होते हैं।
- Statement complete instruction होता है।
- Java statements generally semicolon से end होते हैं।
Practice Questions
Short Answer Questions
- Java tokens क्या होते हैं?
- Java statements क्या होते हैं?
- Keywords और identifiers में अंतर लिखिए।
- Literals क्या होते हैं?
- Java में separators के examples लिखिए।
- Java comments कितने प्रकार के होते हैं?
- Tokens और statements में अंतर लिखिए।
Long Answer Question
Java tokens and statements को examples के साथ explain कीजिए।
Homework Program
एक Java program लिखिए जिसमें student का name, roll number, marks और grade print हो। Program में comments भी use कीजिए।
Hint
class StudentTokens {
public static void main(String[] args) {
// Student details
String name = "Amit";
int rollNo = 101;
double marks = 88.5;
char grade = 'A';
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
System.out.println("Grade: " + grade);
}
}