JAVA Tutorial



SYNTAX IN JAVA


📜 Java Syntax

Java syntax refers to the set of rules that define the structure of a Java program. Just like any other programming language, Java has its own set of syntax rules that must be followed to write valid programs. Java syntax is largely influenced by C, making it easy for developers who are familiar with C-based languages to learn.

🔸 Basic Structure of Java Program

A Java program typically consists of the following elements:

  • Class Declaration: All Java programs are written inside classes. The class name must match the filename.
  • Main Method: This is the entry point of every Java program. It contains the instructions to be executed.
  • Statements: These are the actual instructions or operations written inside the class/method.

🔸 Example of a Simple Java Program

Here's a simple example of a Java program that prints "Hello, World!" to the console:

// Simple Java Program
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}
  

In the above example:

  • public class HelloWorld: This defines a class named HelloWorld.
  • public static void main(String[] args): This defines the main() method where the program execution begins.
  • System.out.println("Hello, World!");: This line prints the string "Hello, World!" to the console.

🔸 Java Case Sensitivity

Java is case-sensitive, which means that myVariable and MyVariable are treated as two different identifiers. It is important to be consistent with naming conventions.

🔸 Semicolons in Java

In Java, every statement must end with a semicolon ;. This is used to indicate the end of a line of code. For example:

int number = 5; // Variable declaration with semicolon
System.out.println(number); // Print statement
  

🔸 Comments in Java

Java supports both single-line and multi-line comments:

  • Single-line comment: Starts with // and extends to the end of the line.
  • Multi-line comment: Starts with /* and ends with */.

// This is a single-line comment
/* This is a multi-line
   comment in Java */
  

🔸 Identifiers in Java

Identifiers are names given to variables, methods, classes, etc. In Java, identifiers must:

  • Begin with a letter (a-z, A-Z), underscore (_), or dollar sign ($)
  • Subsequent characters can include digits (0-9)
  • Not be a reserved keyword

🔸 Data Types in Java

Java supports several data types. A variable's data type determines the type of data it can store. The basic data types in Java include:

  • int: Integer data type (e.g., 5, -3, 100)
  • double: Double-precision floating-point numbers (e.g., 3.14, -0.0001)
  • char: A single character (e.g., 'A', 'z')
  • boolean: Boolean values (either true or false)
  • String: A sequence of characters (e.g., "Hello, World!")

🔸 Blocks and Curly Braces

Java code is enclosed within curly braces {}, especially for blocks like methods, loops, and conditionals. A block is a group of statements that work together. For example:

if (x > 0) {
    System.out.println("x is positive");
} else {
    System.out.println("x is negative");
}
  
💡 Tip: Pay attention to semicolons and curly braces. In Java, they are essential for proper syntax.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review