## Java Basics: Primitives and Wrapper Classes
```mermaid
graph TD;
A[Java Basics] --> B[Primitives and Wrapper Classes]
B --> B1[Primitive Types]
B1 --> B1_1[byte] --> B2[Wrapper Classes]
B1 --> B1_2[short] --> B2
B1 --> B1_3[int] --> B2
B1 --> B1_4[long] --> B2
B1 --> B1_5[float] --> B2
B1 --> B1_6[double] --> B2
B1 --> B1_7[char] --> B2
B1 --> B1_8[boolean] --> B2
B2 --> B2_1[Byte]
B2 --> B2_2[Short]
B2 --> B2_3[Integer]
B2 --> B2_4[Long]
B2 --> B2_5[Float]
B2 --> B2_6[Double]
B2 --> B2_7[Character]
B2 --> B2_8[Boolean]
```
## Java Array:
```mermaid
graph TD;
A[Java Basics] --> C[Array Basics]
C --> C1[Array Declaration]
C1 --> C1_1["int[] arrayName;"]
C1 --> C1_2["int[] arrayName = new int[10];"]
C1 --> C1_3["int[] arrayName = {1, 2, 3, 4, 5};"]
```
## Java Basics: Useful Classes and Class
``` mermaid
graph TD;
A[Java Basics] --> C[Useful Classes]
C --> C1[String]
C --> C2[StringBuilder]
C --> C3[BigDecimal]
A --> D[Class]
D --> D1[Attributes]
D --> D2[Getter and Setter]
D --> D3[Constructor]
```
## Java Basics: OOP Concepts
``` mermaid
graph TD;
A[Java Basics] --> E[OOP Concepts]
E --> E1[Attributes]
E --> E2[Getter and Setter]
E --> E3[Constructor]
E --> E4[Inheritance]
E4 --> E4_1[extends]
E4 --> E4_2[abstract class]
E4 --> E4_3[super]
```
## Java Basics: Conditionals and Loops
``` mermaid
graph TD;
A[Java Basics] --> F[Conditionals]
F --> F1[if-else]
F --> F2[switch]
A --> G[Loops]
G --> G1[for-loop]
G1 --> G1_1[initialization; condition; update]
G --> G2[while-loop]
G2 --> G2_1[condition]
G --> G3[do-while loop]
G3 --> G3_1[execute once then check condition]
```
## Java Basics: Methods and Keywords
``` mermaid
graph TD;
A[Java Basics] --> H[Methods]
H --> H1[Instance Method]
H --> H2[Static Method]
A --> I[Static and Final Keywords]
I --> I1[static final]
I --> I2[final]
A --> J[Other Keyword Combinations]
J --> J1[final method]
J --> J2[final class]
J --> J3[static class]
J --> J4[static variable]
```
---
### 1. What is the difference between static method and instance method?
Static Method
• Belongs to the class, not any particular instance.
• Can be called without creating an object of the class.
• Can access static data members and change their values.
```javascript=
class StaticExample {
static int count = 0;
static void showCount() {
System.out.println("Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
StaticExample.showCount(); // Count: 0
StaticExample.count = 5;
StaticExample.showCount(); // Count: 5
}
}
```
Instance Method
• Belongs to an instance of the class.
• Requires an object of the class to be called.
• Can access instance variables and methods directly.
```javascript=
class InstanceExample {
int count = 0;
void showCount() {
System.out.println("Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
InstanceExample obj = new InstanceExample();
obj.showCount(); // Count: 0
obj.count = 5;
obj.showCount(); // Count: 5
}
}
```
### 2. What is Abstraction?
Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object.
```javascript=
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car();
myCar.start(); // Car started
}
}
```
### 3. What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. It allows methods to do different things based on the object it is acting upon.
```javascript=
abstract class Animal{
}
interface Swimable {
void swim() ;
}
interface Runnable {
void run() ;
}
class Cat extends Animal implements Swimable,Runnable {
@Override
void swim() {
System.out.println("Cat are swimming.");
}
@Override
void run() {
System.out.println("Cat are running.");
}
}
class Dog extends Animal implements Swimable,Runnable {
@Override
void swim() {
System.out.println("Dog are swimming.");
}
@Override
void run() {
System.out.println("Dog are running.");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.swim(); // Dog are swimming.
myDog.run(); // Dog are running.
Animal myCat = new Cat();
myCat.swim(); // Cat are swimming.
myCat.run(); // Cat are running.
}
}
```
### 4. What is the difference between abstract class and interface?
Abstract Class
• Can have both abstract and non-abstract methods.
• Can have instance variables.
• Can provide method implementations.
```javascript=
abstract class Animal {
abstract void sound();
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Dog barks
dog.eat(); // Animal eats
}
}
```
Interface
• Can only have abstract methods (Java 8 onwards, can have default and static methods).
• Cannot have instance variables.
• Provides a way to achieve full abstraction.
```javascript=
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Dog barks
}
}
```
### 5. What is Inheritance?
Inheritance is a mechanism where one class acquires the properties and behaviors of a parent class.
```javascript=
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Animal eats
dog.bark(); // Dog barks
}
}
```
### 6. What is the difference between class Object and class Objects?
Class Object
• The root class of the Java class hierarchy.
• Every class in Java is a subclass of Object.
• Provides basic methods like equals(), hashCode(), toString(), etc.
```javascript=
public class Main {
public static void main(String[] args) {
Object obj = new Object();
System.out.println(obj.toString()); // java.lang.Object@<hashcode>
}
}
```
Class Objects
• A utility class in java.util package.
• Provides static methods to operate on or return objects.
```javascript=
import java.util.Objects;
public class Main {
public static void main(String[] args) {
String str = null;
System.out.println(Objects.toString(str, "default")); // default
}
}
```
### 7. What is Encapsulation?
Encapsulation is the technique of wrapping the data (variables) and code acting on the data (methods) together as a single unit. It restricts direct access to some of an object’s components.
```javascript=
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("John");
System.out.println(person.getName()); // John
}
}
```
### 8. Based on APIE in Java, what is OOP?
OOP (Object-Oriented Programming)
Object-Oriented Programming is a paradigm centered around objects and classes. The four main principles of OOP are:
• Abstraction: Hiding the complex implementation details and showing only the essential features.
• Polymorphism: The ability of an object to take on many forms, allowing methods to do different things based on the object it is acting upon.
• Inheritance: A mechanism where one class acquires the properties and behaviors of a parent class.
• Encapsulation: Wrapping the data and code together as a single unit, restricting direct access to some of the object’s components.
### 9. What is the usage of @Override hashcode() and equals()?
hashCode()
The hashCode() method returns an integer hash code value for an object. It is used in hashing-based collections like HashMap, HashSet, and Hashtable to store and retrieve objects efficiently.
When you override the hashCode() method, you ensure that objects which are equal (according to the equals() method) produce the same hash code. This is crucial for maintaining the contract between hashCode() and equals().
equals()
The equals() method checks if two objects are equal. By default, the equals() method in the Object class checks for reference equality (i.e., whether the two references point to the same object).
When you override the equals() method, you can define what it means for two objects of your class to be considered equal. Typically, this involves comparing the values of relevant fields.
Example
Consider a class Person with fields name and age. To properly use objects of this class in a HashSet, you need to override both equals() and hashCode().
```javascript=
import java.util.Objects;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName(){
reutrn this.name;
}
public int getAge(){
return this.age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if(!(obj instanceOf Person))
return false;
Person person = (Person)obj;
return Objects.equals(obj.name,person.getName())
&& Obejcts.equals(obj.age,person.getAge());
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("John", 25);
Person p2 = new Person("John", 25);
System.out.println(p1.equals(p2)); // true
System.out.println(p1.hashCode() == p2.hashCode()); // true
HashSet<Person> people = new HashSet<>();
people.add(p1);
people.add(p2);
System.out.println(people.size()); // 1
System.out.println(people); // [Person{name='John', age=25}]
}
}
```
Summary
• Override equals() to define when two objects should be considered equal based on their state.
• Override hashCode() to ensure objects that are equal according to equals() return the same hash code, facilitating correct behavior in hash-based collections.
• Proper implementation of both methods is crucial for objects used in collections like HashMap, HashSet, etc.