Java cheatsheet

 

Great question! Java is strict with rules. Here's a quick checklist of the most important things you MUST remember while writing Java code—like semicolon, braces, and more.


JAVA CODE RULES TO ALWAYS REMEMBER

1. Semicolon (;)

๐Ÿ”น Use it to end every statement.
๐Ÿ“Œ Remember: If you forget it, compiler will give an error.

int x = 10;   // ✅ correct
System.out.println(x);  // ✅ correct

2. Curly Braces { }

๐Ÿ”น Use for class, method, loops, if-else blocks.
๐Ÿ“Œ Open { and always close with }.

if (x > 0) {
   System.out.println("Positive");
} // ✅ braces are matched

3. Main Method Syntax (fixed format)

๐Ÿ”น This is where your program starts running.
๐Ÿ“Œ Don’t change the spelling or structure.

public static void main(String[] args) {
    // code
}

4. Class Name = File Name (Case Sensitive)

๐Ÿ”น File name must match the class name (if public).

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hi");
    }
}
// Must be saved as Hello.java ✅

5. Case Sensitive

๐Ÿ”น HellohelloHELLO
๐Ÿ“Œ Variable and method names are case-sensitive.

int num = 5;
System.out.println(num);  // ✅
System.out.println(Num);  // ❌ Error

6. Use new to Create Object

๐Ÿ”น You can't use a class directly—use new.

Student s = new Student();  // ✅

7. Use System.out.println() for Output

๐Ÿ”น Java’s standard way to print something.

System.out.println("Hello");

8. Always Close Quotes and Brackets

๐Ÿ”น Forgetting a " or ) causes errors.

System.out.println("Hi");  // ✅
System.out.println("Hi);   // ❌ missing "

๐Ÿง  BONUS MEMORY HACK (CLUE WORDS)

๐Ÿ’ก What ๐Ÿง  Clue Tip
End statement Semicolon ; Like a full stop .
Group code Curly braces {} Open door → do stuff → close door
Output System.out.println() Say something out loud
Create object new "new object born"
Start program main method Like main() in C/C++
File name = class Match exactly If class Cat, file = Cat.java

✅ PRACTICE CHECKLIST FOR EVERY JAVA PROGRAM

✔ Starts with class
✔ Has main method
✔ Ends each line with ;
✔ Uses {} correctly
✔ Uses System.out.println() for output
✔ Object created using new
✔ File name = public class name


If you want, I can give you a template to copy-paste every time. Want that?


Absolutely! Here's more detailed guidance on writing correct and clean Java code—beyond semicolons and syntax. This will help you avoid errors and write code like a pro.


๐Ÿง  PART 1: STRUCTURE TO REMEMBER

✅ Basic Java Program Template

// Class name (must match file name if public)
public class MyProgram {

    // Main method (entry point)
    public static void main(String[] args) {
        // Your code here
        System.out.println("Hello, Java!");
    }
}

๐Ÿ’ก Tip: Save file as MyProgram.java


๐Ÿ”‘ PART 2: COMMON MISTAKES & REMINDERS

1. Every statement ends with ;

int a = 5;        // ✅
a = a + 10;       // ✅

❌ Forgetting ; causes "expected ;" error


2. Curly braces {} must always be paired

if (a > 0) {
   System.out.println("Positive");
} // ✅ closed

❌ Forgetting } gives "reached end of file while parsing" error


3. Case-sensitive

int Age = 20;
System.out.println(Age); // ✅
System.out.println(age); // ❌ age not defined

4. Class name = File name (important!)

public class HelloWorld {} 
// Must save as HelloWorld.java ✅

5. Use correct method names (spelling matters!)

System.out.println("Hello"); // ✅
System.out.print("No newline"); // ✅

system.out.Println() will cause errors (capitalization matters)


6. Create objects with new

Car myCar = new Car(); // ✅

7. Avoid using public class more than once in same file

public class A {}   // ✅
class B {}          // ✅

❌ Only one public class per file


๐Ÿ”ง PART 3: TIPS TO WRITE CLEAN CODE

✅ DO ❌ AVOID
Use indentation (tabs/spaces) Writing all code in one line
Use meaningful variable names Names like x1, var2
Use comments // to explain logic No comments makes code confusing
Group related code with {} Writing code without structure

๐Ÿ“š PART 4: Java Keyword Categories (Mini Reference)

๐Ÿ”น Data Types

int, float, double, char, boolean, String

๐Ÿ”น Control Flow

if, else, switch, while, for, break, continue

๐Ÿ”น OOP Basics

class, object, new, this, public, private, protected

๐Ÿ”น Error Handling

try, catch, throw, throws, finally

๐Ÿ”น Other

static, void, return, import, package


๐Ÿงช PART 5: MINI PRACTICE CHALLENGE

Try writing this yourself:

✅ Create a simple class Student with:

  • Attributes: name, roll
  • Method: displayDetails() which prints both

Expected Output:

Name: Ravi
Roll: 101

Need help with the answer? I can give you the full code after you try!


Would you also like a Java cheat sheet PDF or a beginner Java roadmap next?




References

https://chatgpt.com/share/6882f1b3-e66c-8001-8cca-a4783ad31d32