Java Cheatsheet for begineers, Cheat Sheet of Java Programming

It contain all important java code snippets .

Typology: Cheat Sheet

2023/2024

Uploaded on 01/12/2024

swayam-patil-1
swayam-patil-1 🇮🇳

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Hello World:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Variables and Data Types:
// Variable declaration
int num1 = 10;
double num2 = 3.14;
String name = "John";
// Primitive data types
int, double, char, boolean
// Reference data types
String, Arrays, Classes, etc.
3. Control Flow:
// If-else statement
if (condition) {
// code block
} else if (anotherCondition) {
// code block
} else {
// code block
}
// Switch statement
int day = 2;
switch (day) {
case 1:
// code block
break;
case 2:
// code block
break;
default:
// code block
}
// While loop
while (condition) {
// code block
}
// For loop
for (int i = 0; i < 5; i++) {
// code block
}
// For-each loop (enhanced for loop)
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
// code block
}
4. Functions (Methods):
// Method declaration
public returnType
methodName(parameters) {
// code block
return returnValue; // returnType should
match the declared type
}
// Example
public int add(int a, int b) {
return a + b;
}
5. Object-Oriented Programming (OOP):
// Class declaration
class MyClass {
// Fields (variables)
int field1;
String field2;
// Constructor
public MyClass(int value, String name) {
pf2

Partial preview of the text

Download Java Cheatsheet for begineers and more Cheat Sheet Java Programming in PDF only on Docsity!

1. Hello World:

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

2. Variables and Data Types:

// Variable declaration int num1 = 10; double num2 = 3.14; String name = "John"; // Primitive data types int, double, char, boolean // Reference data types String, Arrays, Classes, etc.

3. Control Flow:

// If-else statement if (condition) { // code block } else if (anotherCondition) { // code block } else { // code block } // Switch statement int day = 2; switch (day) { case 1: // code block break; case 2: // code block break; default: // code block } // While loop while (condition) { // code block } // For loop for (int i = 0; i < 5; i++) { // code block } // For-each loop (enhanced for loop) int[] numbers = {1, 2, 3, 4, 5}; for (int number : numbers) { // code block }

4. Functions (Methods):

// Method declaration public returnType methodName(parameters) { // code block return returnValue; // returnType should match the declared type } // Example public int add(int a, int b) { return a + b; }

5. Object-Oriented Programming (OOP): // Class declaration class MyClass { // Fields (variables) int field1; String field2; // Constructor public MyClass(int value, String name) {

field1 = value; field2 = name; } // Methods public void method1() { // code block } } // Object creation MyClass obj = new MyClass(42, "John"); // Accessing fields and methods int value = obj.field1; String name = obj.field2; obj.method1();

6. Arrays:

// Array declaration int[] numbers = new int[5]; // creates an array of size 5 int[] numbers = {1, 2, 3, 4, 5}; // initializes array with values // Accessing array elements int firstElement = numbers[0]; // Array length int length = numbers.length;

 ## 7. Input/Output: import java.util.Scanner; **_// Import Scanner class for user input // Reading user input_** Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); String input = scanner.nextLine(); ## 8. Exception Handling: try { _// code that might cause an exception_ } catch (ExceptionType1 ex1) { _// exception handling for ExceptionType_ } catch (ExceptionType2 ex2) { _// exception handling for ExceptionType_ } finally { _// optional code block that runs regardless of exceptions_ } ## 9. Inheritance: **_// Parent class_** class Animal { void sound() { System.out.println("Animal makes a sound."); } } **_// Child class_** class Dog extends Animal { void sound() { System.out.println("Dog barks."); } } ## 10. Interfaces: _// Interface declaration_ interface MyInterface { void doSomething(); } _// Implementing the interface_ class MyClass implements MyInterface { public void doSomething() { // code block } }