Java Syntax और Structure
In this page:
public class ClassName {
public static void main(String[] args) {
statement; // each statement ends with ;
}
}
Case Sensitivity
Java uppercase और lowercase letters को पूरी तरह अलग characters मानता है, इसलिए myVariable और MyVariable दो distinct identifiers हैं -- यह case-insensitive languages से आने वाले beginners को trip करता है।
उदाहरण: Case Sensitivity
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare myVariable and set it to 1
int myVariable = 1;
// Declare MyVariable and set it to 2
int MyVariable = 2;
// Print a line to the console
System.out.println(myVariable + " and " + MyVariable + " are different variables.");
}
}
Login to try C/C++/Java/PHP code in the editor
Curly Braces
हर class body, method body, loop, और conditional block curly braces की एक matching pair से delimited है, और एक missing या misplaced brace आपको मिलने वाले सबसे आम compiler errors में से एक है।
उदाहरण: Curly Braces
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Check whether true
if (true) {
// Print a line to the console
System.out.println("Inside the if block");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Class Naming Conventions
Convention (tooling से enforced, compiler से नहीं) कहता है class names PascalCase उपयोग करते हैं, हर word का पहला letter capitalize करते हुए, जैसे BankAccount या HttpClient -- इसे follow करना आपके code को दूसरे Java developers के लिए readable रखता है।
उदाहरण: Class Naming Conventions
// Define the class BankAccount
class BankAccount {
double balance;
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new BankAccount object and store it in account
BankAccount account = new BankAccount();
// Print a line to the console
System.out.println("Class named in PascalCase: BankAccount");
}
}
Login to try C/C++/Java/PHP code in the editor
Method Naming Conventions
Method names इसके बजाय camelCase follow करते हैं, lowercase से शुरू होते हुए, जैसे calculateTotal() -- PascalCase class names से यह visual distinction आपको एक नज़र में बताने में मदद करता है कि एक identifier एक type है या एक behavior।
उदाहरण: Method Naming Conventions
// Define the class Main
public class Main {
static int calculateTotal(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Method named in camelCase: " + calculateTotal(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Indentation and Whitespace
Compiler इसकी परवाह नहीं करता कि आप अपना code कैसे indent करते हैं, लेकिन consistent indentation (आमतौर पर प्रति nesting level 4 spaces) वह है जो deeply nested loops और conditionals को किसी human के लिए असल में readable बनाता है।
उदाहरण: Indentation and Whitespace
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 0; i < 2; i++) {
// Print a line to the console
System.out.println("Consistently indented nested block: " + i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
- गलत case उपयोग करना, जैसे
stringके बजायString, क्योंकि Java case-sensitive है। - एक curly brace बंद करना भूल जाना, इसलिए compiler एक error report करता है file के आखिर में बजाय जहां brace गायब है।
- एक statement के आखिर में semicolon छोड़ना, जो
';' expectedदेता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: