Basic Syntax and Data Types in Java

Basic Syntax in Java

Java is a high-level, object-oriented programming language known for its simplicity and robustness. Understanding its basic syntax is crucial for writing Java programs.

Structure of a Java Program

                    
public class HelloWorld {
    // Entry point of the program
    public static void main(String[] args) {
        // Statements to be executed
        System.out.println("Hello, World!"); // Print statement
    }
}
                    
                

Class Declaration: Every Java program starts with a class declaration. The keyword public indicates that this class is accessible from outside. HelloWorld is the name of the class, which matches the filename (HelloWorld.java in this case).

Main Method: main method is the entry point of every Java application. It's where the program starts executing. public static void specifies that the main method can be called by any other class, without creating an instance of the class. String[] args is an array of strings that can be passed as arguments when the program is executed.

Statements: Java statements end with a semicolon (;). Each statement performs an action, such as declaring a variable, calling a method, or controlling the program flow. System.out.println() is used to print output to the console. Here, it prints "Hello, World!".

Data Types in Java

Primitive Data Types

Primitive data types are the most basic data types available in Java. They represent single values, and Java provides predefined keywords for each type.

                    
byte smallNumber = 10;
short mediumNumber = 500;
int regularNumber = 10000;
long largeNumber = 1000000000L; // Note the 'L' suffix for long literals

float smallerDecimal = 3.14f; // Note the 'f' suffix for float literals
double largerDecimal = 3.14159265359;

boolean isActive = true;
char initial = 'A';
                    
                

Integer Types: byte, short, int, long

Floating-Point Types: float, double

Other Primitive Types: boolean, char

Reference Data Types

Reference data types are used to store references to objects, which are instances of classes. They do not hold the actual data, but rather a reference (memory address) to the object.

Examples: String, Arrays

                    
String message = "Hello, World!";

int[] numbers = {1, 2, 3, 4, 5}; // Array of integers
String[] names = {"Alice", "Bob", "Charlie"}; // Array of strings
                    
                

Variable Declaration and Initialization

Variables in Java must be declared before they can be used. Declaration specifies the data type and optionally initializes the variable.

                    
int age; // Declaration
age = 30; // Initialization

double pi = 3.14; // Declaration and initialization in one line
                    
                

Comments

Comments in Java are used to explain code and make it more readable. They are ignored by the compiler.

                    
// This is a single-line comment

/*
 * This is a
 * multi-line comment
 */