← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 5 of 12

Java Syntax और Structure

Java के strict spelling और grammar rules हैं, careful punctuation वाली एक language जैसी। Capital letters मायने रखते हैं, curly braces steps को group करते हैं, और हर statement एक semicolon से खत्म होता है।
Syntax
java
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

java
// 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.");
	}
}

Curly Braces

हर class body, method body, loop, और conditional block curly braces की एक matching pair से delimited है, और एक missing या misplaced brace आपको मिलने वाले सबसे आम compiler errors में से एक है।

उदाहरण: Curly Braces

java
// 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");
		}
	}
}

Class Naming Conventions

Convention (tooling से enforced, compiler से नहीं) कहता है class names PascalCase उपयोग करते हैं, हर word का पहला letter capitalize करते हुए, जैसे BankAccount या HttpClient -- इसे follow करना आपके code को दूसरे Java developers के लिए readable रखता है।

उदाहरण: Class Naming Conventions

java
// 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");
	}
}

Method Naming Conventions

Method names इसके बजाय camelCase follow करते हैं, lowercase से शुरू होते हुए, जैसे calculateTotal() -- PascalCase class names से यह visual distinction आपको एक नज़र में बताने में मदद करता है कि एक identifier एक type है या एक behavior।

उदाहरण: Method Naming Conventions

java
// 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));
	}
}

Indentation and Whitespace

Compiler इसकी परवाह नहीं करता कि आप अपना code कैसे indent करते हैं, लेकिन consistent indentation (आमतौर पर प्रति nesting level 4 spaces) वह है जो deeply nested loops और conditionals को किसी human के लिए असल में readable बनाता है।

उदाहरण: Indentation and Whitespace

java
// 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);
        }
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. गलत case उपयोग करना, जैसे string के बजाय String, क्योंकि Java case-sensitive है।
  2. एक curly brace बंद करना भूल जाना, इसलिए compiler एक error report करता है file के आखिर में बजाय जहां brace गायब है।
  3. एक statement के आखिर में semicolon छोड़ना, जो ';' expected देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.