1730021642852
1730021642852
1. Introduction of Java
2. JDK setup
3. First code in Java
4. How Java Works
5. Variables
6. Data Types
7. Literal
8. Type Conversion
9. Arithmetic Operators
10. Relational Operators
11. Logical Operators
12. If Else
13. If Else If
14. Ternary
15. Switch Statement
16. Need For Loop
17. While Loop
18. Do While Loop
19. For Loop
20. Which Loop To Use
21. Class And Object Theory
22. Class and Object Practical
23. JDK JRE JVM
24. Methods
25. Method Overloading
26. Stack And Heap
27. Need of Array
28. Creation of Array
29. Multi-Dimensional Array
30. jagged and 3D Array
31. Drawbacks of Array
32. Array of Objects
33. Enhanced for loop
34. What is String
35. Mutable vs Immutable string
36. String Buffer and StringBuilder
37. Static Variable
38. Static method
39. Static block
40. Encapsulation
41. Getters and setters
42. This keyword
43. Constructor
44. Default vs Parameterized Constructor
45. Naming Convention
46. Anonymous Object
47. What is Inheritance
48. Need of Inheritance
49. Single and Multilevel inheritance
50. Multiple Inheritance
51. This and super method
52. Method Overriding
53. Packages
54. Access Modifiers
55. Polymorphism
56. Dynamic Method Dispatch
57. Final keyword
58. Object Class equals toString hashcode
59. Upcasting and Downcasting
60. Wrapper Class
Java Cheat Sheet
1. Introduction to Java
2. JDK Setup
JDK (Java Development Kit) includes tools like the compiler (`javac`) and runtime
environment (JRE).
Installation:
1. Download the JDK from [Oracle]
(https://www.oracle.com/java/technologies/javase-downloads.html).
2. Set `JAVA_HOME` environment variable to the JDK path.
3. Update `PATH` variable to include `JAVA_HOME/bin`.
5. Variables
Variables store data for processing. Java supports local, instance, and static variables.
Syntax: `dataType variableName = value;
int age = 25;
6. Data Types
Primitive Types**: `byte`, `short`, `int`, `long`, `float`, `double`, `char`, `boolean`
Non-primitive Types**: Arrays, Classes, Interfaces, etc.
Example:
int num = 10; char letter = 'A';
7. Literals
8. Type Conversion
int a = 10;
double b = a; // Implicit conversion from int to double
double x = 10.5;
int y = (int) x; // Casting double to int
9. Arithmetic Operators
`+`, `-`, `*`, `/`, `%` (Addition, Subtraction, Multiplication, Division, Modulo)
Example:
int sum = 10 + 5; // sum = 15
Example:
`&&` (AND), `||` (OR), `!` (NOT)
boolean result = (5 > 3) && (8 > 6); // result = true
12. If Else
13. If Else If
Loops help in executing a block of code repeatedly as long as the condition is true.
Create classes and objects, set fields, and call methods practically.
Example:
class Student {
int id;
String name;
void display() {
System.out.println(id + " " + name);
}}
Multiple methods with the same name but different parameters in the same class.
Example:
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}}
Arrays store multiple elements of the same data type, reducing code complexity.
Can access only static data members and call other static methods.
Example:
class Test {
static void display() {
System.out.println("Static Method");
}}
40. Encapsulation
Bundling data (variables) and methods in a single unit (class) and restricting access using
private access modifiers.
Example:
class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String name) {
this.name = name;
}}
42. `this` Keyword
43. Constructor
Definition: A special method invoked when an object is created. Used to initialize objects.
Example:
public class Car {
private String model;
// Constructor
public Car(String model) {
this.model = model;
}}
// Default Constructor
public Vehicle() {
this.type = "Unknown";
}
// Parameterized Constructor
public Vehicle(String type) {
this.type = type;
}
}
45. Naming Conventions
// Single Inheritance
class A { }
class B extends A { }
// Multilevel Inheritance
class C extends B { }
50. Multiple Inheritance
Definition: A class cannot extend more than one class in Java due to ambiguity
(solved by interfaces).
Example:
interface A { }
interface B { }
class C implements A, B { }
Definition: Subclass has a method with the same signature as a method in its superclass.
Example:
class Parent {
void show() { System.out.println("Parent show"); }
}
53. Packages
55. Polymorphism
Definition: Ability to present the same interface for different data types.
Types: Compile-time (method overloading) and Run-time (method overriding).
Example:
class Animal {
void sound() { System.out.println("Generic Animal Sound"); }
}
Definition: Method call resolved at runtime based on the object’s actual type.
Example:
Animal a = new Cat(); // Upcasting
a.sound(); // Calls Cat's sound method
class Test {
final void display() { } // Cannot be overridden
}
Definition: Provides a way to use primitive data types as objects (e.g., `Integer`, `Character`).
Example:
int num = 10;
Integer obj = Integer.valueOf(num); // Boxing
int n = obj.intValue(); // Unboxing
This cheat sheet covers the basic concepts and examples to help you quickly grasp the main OOP
principles in Java. Let me know if you need more details or specific examples on any of these
topics!