Syntax in Java

Java syntax is the set of rules that define how a Java program is written and interpreted. The syntax is mostly derived from C and C++. Unlike C++, in Java there are no global functions or variables, but there are data members which are regarded as global variables.

Basic Keywords and Operators

The following are some of the most basic keywords and operators in Java:

Keywords

class
public
static
void
main
String
System
out
println

Control Flow Statements

Java provides various control flow statements, allowing you to control the order in which your code executes. Some common control flow statements include:

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}
While Loop
while (condition) {
    // code to execute while condition is true
}
For Loop
for (int i = 0; i < 10; i++) {
    // code to execute for each value of i
}
Classes and Objects

Java is an object-oriented programming language. It uses classes and objects to represent data and behavior. A class is a blueprint for an object, and an object is an instance of a class.
To create a class, use the `class` keyword. For example:

class MyClass {

}

To create an object, use the `new` keyword. For example:

MyClass myObject = new MyClass();
Methods

Methods are functions defined inside a class. They can perform actions or return values. To define a method:

public void myMethod() {

}

To call a method, use the object's name, followed by a dot and the method name:

myObject.myMethod();
Variables

Variables store data. To declare a variable, use a type keyword like `int`, `double`, `String`, or `char`. For example:

int myInt;
double myDouble;
String myString;
char myChar;

Assign a value to a variable using the `=` operator:

myInt = 10;
myDouble = 3.14;
myString = "Hello, world!";
myChar = 'A';
Arrays

Arrays store multiple values of the same type. To declare an array:

int[] myIntArray;
double[] myDoubleArray;
String[] myStringArray;
char[] myCharArray;

Assign values to an array:

myIntArray = new int[] { 10, 20, 30 };
myDoubleArray = new double[] { 3.14, 2.718, 1.618 };
myStringArray = new String[] { "Hello", "world!" };
myCharArray = new char[] { 'A', 'B', 'C' };