

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
It contain all important java code snippets .
Typology: Cheat Sheet
1 / 2
This page cannot be seen from the preview
Don't miss anything!
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
// 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.
// 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 }
// 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();
// 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 } }