Basic Syntax in Java♨️

Core Java ♨️

Before building big programs, let’s learn the building blocks of Java. Think of this like learning the ABC of a language before writing full sentences.

Variables – “The Containers”

A variable is like a box where you store information (numbers, words, etc.) to use later.

·         You give the box a name.

·         You put some data inside it.

Example in Real Life:
Think of a water bottle.

·         The bottle’s name is the variable name.

·         The water inside is the data stored.

·         If you pour out and refill, you change the value of the variable.

Rules for Declaring a Variable Name in Java

When you create a variable in Java, you must follow some rules so the computer understands it.
Think of it like
naming your pet 🐶—you can choose any name, but there are a few restrictions!

Rules:

1. Must start with a letter, $ or _

  • You can’t start with a number.
  • ✔️ Correct: age, _count, $salary
  • Wrong: 1number, @price

Real-Life Example:
Just like your name can’t start with a number (no one is called “3John”), variable names can’t either.

 

2. Can only contain letters, numbers, $ or _

  • Correct: student1, total_amount, $rate
  • Wrong: total-amount, price@

Real-Life Example:
Think of writing your name on a school form
📝—you can use letters, maybe numbers (like Roll No. 5), but not special symbols like @ or #.

3.    No spaces allowed

o    ✔️ Correct: firstName, totalMarks

o    Wrong: first name, total marks

Real-Life Example:
You can’t keep your Wi-Fi password with spaces in between randomly—it must be continuous. Same with variable names
.

4. Case-sensitive (upper vs. lower case matters)

  • Age and age are treated as two different variables.

Real-Life Example:
Just like “RAM” (the god) and “ram” (a boy’s name) mean different things, Java also treats uppercase and lowercase differently
.

 

5. Should not be a keyword

  • You can’t use reserved words like class, if, for.
  • Correct: studentAge
  • Wrong: class

Real-Life Example:
Imagine trying to name your shop “Police Station”
🚓—not allowed, because that name is already reserved for government use.

6. Use meaningful names (good practice)

  • Good: customerName, totalPrice
  • Bad: x, y, abc

Real-Life Example:
Naming your child “Kid1” and “Kid2” may confuse you later
😅. Better to use meaningful names like “Aman” or “Riya.”

👉 In short:

  • Start with letter, _, or $.
  • Use only letters, numbers, _, $.
  • No spaces.
  • Case-sensitive.
  • Don’t use reserved words.
  • Always choose clear, meaningful names.

Key Elements of Java Basic Syntax:
  • Case Sensitivity: 
    Java is case-sensitive.
  • MyVariable and myvariable are treated as different identifiers.
  • Class Names: 
    Class names typically start with an uppercase letter and follow PascalCase (e.g., MyClassHelloWorld).

  • Method Names: 
    Method names usually start with a lowercase letter and follow camelCase (e.g., myMethodcalculateSum).

  • File Naming: 
    If a Java file contains a public class, the file name must exactly match the public class name, including case, and end with the .java extension (e.g., HelloWorld.java for a public class HelloWorld).

  • public static void main(String[] args) 
    This is the entry point for every Java application.
    • public Makes the method accessible from anywhere.
    • static: Allows the method to be called without creating an instance of the class.
    • void: Indicates that the method does not return any value.
    • main: The name of the method.
    • (String[] args): Declares a parameter that accepts an array of strings, typically used for command-line arguments.
  • Curly Braces {}
    Used to define code blocks for classes, methods, loops, and conditional statements.
  • Semicolons ;
    Terminate statements in Java. Every executable statement must end with a semicolon.
  • Comments: 
    Used to add explanations to the code, ignored by the compiler.
    • Single-line comments: // This is a single-line comment
    • Multi-line comments: /* This is a multi-line comment */
    • Documentation comments: /** This is a documentation comment */
  • Variables: 
    Declared before use, specifying their data type and name (e.g., int age = 30;).
  • Keywords: 
    Reserved words with special meaning in Java that cannot be used as identifiers (e.g., publicclassintifelse).

  • Writer - Inderjit Singh ✍🏼



Post a Comment

Previous Post Next Post