Java syntax refers to the rules and guidelines for writing valid Java code. Here are some key syntax rules in Java:
- Java is a case-sensitive language. This means that “Hello” and “hello” are considered two different identifiers.
- All statements in Java must end with a semicolon (;). This includes variable declarations, assignments, method calls, and control structures.
- Blocks of code are enclosed within curly braces ({}) and can be nested within each other.
- Java variables must be declared with a specific data type, such as int, double, or String. The data type determines what kind of values the variable can hold.
- Java methods are declared with a return type, which indicates the type of value that the method will return.
- Java keywords, such as “if,” “while,” “for,” and “switch,” have a specific syntax and cannot be used as variable or method names.
- Java comments can be used to add notes and explanations to the code. Single-line comments start with “//” and extend to the end of the line. Multi-line comments start with “/” and end with “/”.
Here is a simple Java program that prints “Hello, World!” to the console:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Let’s break it down:
- The first line
public class HelloWorld
declares a new class named HelloWorld. By convention, the name of the class should match the name of the file (i.e.,HelloWorld.java
). - The second line
public static void main(String[] args)
declares the main method, which is the entry point for any Java program. It takes an array of strings as input and returns nothing (void
). - The third line
{
starts the body of the main method. - The fourth line
System.out.println("Hello, World!");
prints the string “Hello, World!” to the console. TheSystem.out
part refers to the standard output stream, and theprintln
method prints a string followed by a newline character. - The fifth line
}
ends the body of the main method. - The sixth line
}
ends the class declaration.
To run this program, you can compile the code using the Java compiler (i.e., javac HelloWorld.java
) and then run the resulting bytecode using the Java Virtual Machine (i.e., java HelloWorld
). The output should be:
Hello, World!
Let’s learn more Java syntax:
Case Sensitivity
Java is a case-sensitive language, which means that the names of variables, methods, classes, and other identifiers should be written in the same case throughout the program.
int count = 5; // valid int Count = 10; // valid int CoUnt = 15; // valid int couNT = 20; // valid int CouNT = 25; // valid int COUNT = 30; // valid, but different from the previous examples
Comments
Comments are used to add notes and explanations to the code. In Java, single-line comments start with //
and multiline comments start with /*
and end with */
.
// This is a single-line comment /* This is a multiline comment that spans multiple lines */
Data Types
Java has several data types such as int
, float
, double
, boolean
, char
, etc. that are used to define variables.
int age = 30; float price = 2.99f; double salary = 50000.50; boolean isTrue = true; char grade = 'A';
Variables
A variable is a name given to a memory location to store a value. In Java, variables must be declared with their data type before they can be used.
int num1 = 10; int num2 = 20; int sum = num1 + num2; System.out.println("Sum = " + sum);
Operators
Java has various operators such as arithmetic operators (+, -, *, /), relational operators (==, !=, >, <, >=, <=), logical operators (&&, ||, !), etc.
int a = 10; int b = 20; int c = a + b; // addition operator int d = a * b; // multiplication operator boolean isGreater = (a > b); // greater than operator boolean isNotEqual = (a != b); // not equal operator boolean isTrue = (a < b) && (b < c); // logical AND operator
Control Statements
Java supports various control statements such as if-else, switch, for loop, while loop, do-while loop, etc. that are used to control the flow of the program.
int num = 10; if (num % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } switch (num) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; default: System.out.println("Other"); break; } for (int i = 0; i < 5; i++) { System.out.println(i); } while (num < 20) { System.out.println(num); num++; } do { System.out.println(num); num++; } while (num < 30);
Classes and Objects
In Java, a class is a blueprint for creating objects. Objects are instances of classes that have their own attributes and behaviors.
class Person { String name; int age; void sayHello() { System.out.println("Hello, my name is " + name); } } Person john = new Person(); john.name = "John"; john.age = 30; john.sayHello();
These are some basic examples of Java syntax.