Java Syntax Cheat Sheet

In Java

Java is a popular programming language that is widely used for developing various applications, including web, mobile, and desktop applications. It is an object-oriented language that is easy to learn and use. Java syntax is the set of rules that govern the structure and format of Java code. In this article, we will introduce you to the basic syntax of Java.

Java code is written in plain text files with the .java extension. The code is then compiled into bytecode, which can be executed on any platform that has a Java Virtual Machine (JVM) installed. The syntax of Java is similar to that of other programming languages, such as C++ and C#. However, Java has some unique features that make it stand out.

Java code is organized into classes, which are the building blocks of Java programs. Each class contains methods, which are blocks of code that perform specific tasks. Methods can be called from other methods or from outside the class. Java also has variables, which are used to store data. Variables can be of different types, such as integers, strings, and booleans.

Java syntax also includes control structures, which are used to control the flow of the program. These include if-else statements, loops, and switch statements. If-else statements are used to execute code based on a condition. Loops are used to repeat a block of code multiple times. Switch statements are used to execute different blocks of code based on the value of a variable.

Java also has object-oriented features, such as inheritance, polymorphism, and encapsulation. Inheritance allows one class to inherit properties and methods from another class. Polymorphism allows objects of different classes to be treated as if they were of the same class. Encapsulation allows data to be hidden from other classes, ensuring that it is only accessed through methods.

This cheat sheet provides an extensive list of Java syntax with a brief description of each element. The syntax is divided into different categories for easy reference.

Basic Syntax

SyntaxDescription
public static void main(String[] args)The main method that is executed when the program is run
System.out.println(""Hello, World!"");Prints “”Hello, World!”” to the console
// This is a single-line commentComments out a single line of code
/* This is a multi-line comment */Comments out multiple lines of code
;Ends a statement
{ }Defines a block of code

Variables

SyntaxDescription
int x = 5;Declares an integer variable named x and assigns it the value 5
double y = 3.14;Declares a double variable named y and assigns it the value 3.14
String name = ""John"";Declares a String variable named name and assigns it the value ""John""
boolean isTrue = true;Declares a boolean variable named isTrue and assigns it the value true
final int MAX_VALUE = 100;Declares a final integer variable named MAX_VALUE and assigns it the value 100 (cannot be changed)

Operators

SyntaxDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)
++Increment
--Decrement
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
&&Logical AND
||Logical OR
!Logical NOT

Control Flow

SyntaxDescription
if (condition) { }Executes the code inside the block if the condition is true
if (condition) { } else { }Executes the code inside the first block if the condition is true, otherwise executes the code inside the second block
switch (variable) { case value: }Executes the code inside the case block that matches the value of the variable
for (initialization; condition; update) { }Executes the code inside the block repeatedly while the condition is true
while (condition) { }Executes the code inside the block repeatedly while the condition is true
do { } while (condition);Executes the code inside the block at least once, then repeatedly while the condition is true
break;Exits the current loop or switch statement
continue;Skips the current iteration of the loop

Classes and Objects

SyntaxDescription
public class MyClass { }Defines a public class named MyClass
private int x;Declares a private integer variable named x
public void setX(int value) { }Defines a public method named setX that takes an integer parameter named value
MyClass obj = new MyClass();Creates a new instance of the MyClass class named obj
obj.setX(5);Calls the setX method on the obj instance with the value 5

Inheritance and Polymorphism

SyntaxDescription
public class ChildClass extends ParentClass { }Defines a public class named ChildClass that extends the ParentClass class
@OverrideIndicates that a method in a subclass is overriding a method in the superclass
public void myMethod() { super.myMethod(); }Calls the myMethod method in the superclass from the subclass
ParentClass obj = new ChildClass();Creates a new instance of the ChildClass class named obj and assigns it to a variable of type ParentClass
obj.myMethod();Calls the myMethod method on the obj instance, which will execute the method in the ChildClass class if it has been overridden

Exception Handling

SyntaxDescription
try { } catch (Exception e) { }Executes the code inside the try block and catches any exceptions that occur in the catch block
throw new Exception(""Error message"");Throws a new exception with the specified error message
finally { }Executes the code inside the finally block after the try and catch blocks, regardless of whether an exception was thrown

Input and Output

SyntaxDescription
Scanner scanner = new Scanner(System.in);Creates a new Scanner object to read input from the console
String input = scanner.nextLine();Reads a line of input from the console and assigns it to the input variable
File file = new File(""filename.txt"");Creates a new File object with the specified filename
Scanner scanner = new Scanner(file);Creates a new Scanner object to read input from the specified file
PrintWriter writer = new PrintWriter(""filename.txt"");Creates a new PrintWriter object to write output to the specified file
writer.println(""Hello, World!"");Writes “”Hello, World!”” to the file
writer.close();Closes the PrintWriter object and flushes any remaining output to the file

Conclusion

This cheat sheet provides a comprehensive list of Java syntax with a brief description of each element. It covers basic syntax, variables, operators, control flow, classes and objects, inheritance and polymorphism, exception handling, and input and output. Use this cheat sheet as a quick reference guide when coding in Java.

Reference:

https://docs.oracle.com/en/java/

#