Definition: Data types specify the type of data that a variable can hold. Java is a statically-typed language, which means you must declare a variable’s type before using it.
Explanation: Java provides two main types of data types: primitive and reference types.
Primitive Data Types:
byte
: 8-bit integershort
: 16-bit integerint
: 32-bit integerlong
: 64-bit integerfloat
: 32-bit floating-pointdouble
: 64-bit floating-pointchar
: 16-bit Unicode characterboolean
: true or falseReference Data Types: These include objects and arrays.
Example:
public class DataTypesExample {
public static void main(String[] args) {
int num = 10;
double price = 19.99;
char letter = 'A';
boolean isJavaFun = true;
System.out.println("Integer: " + num);
System.out.println("Double: " + price);
System.out.println("Character: " + letter);
System.out.println("Boolean: " + isJavaFun);
}
}
Definition: Variables are containers for storing data values. Each variable in Java must be declared with a data type.
Explanation: Variables are used to store data that can be manipulated later in the program. They can be of primitive types or reference types.
Example:
public class VariablesExample {
int instanceVariable = 5; // Instance variable
public static void main(String[] args) {
int localVariable = 10; // Local variable
VariablesExample example = new VariablesExample();
System.out.println("Local Variable: " + localVariable);
System.out.println("Instance Variable: " + example.instanceVariable);
}
}
Definition: Loops are used to execute a block of code repeatedly based on a condition.
Explanation: Java supports several types of loops: for loop, enhanced for loop (for-each), while loop, and do-while loop.
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println("Number: " + number);
}
int i = 0;
while (i < 5) {
System.out.println("Iteration: " + i);
i++;
}
int i = 0;
do {
System.out.println("Iteration: " + i);
i++;
} while (i < 5);
Definition: Conditions control the flow of the program based on certain criteria.
Explanation: Java supports conditional statements like if
, if-else
, and switch
to make decisions in the code.
int number = 10;
if (number > 0) {
System.out.println("Number is positive.");
} else {
System.out.println("Number is non-positive.");
}
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
break;
}
Definition: Operators perform operations on variables and values.
Explanation: Java supports several types of operators: arithmetic, relational, logical, and assignment operators.
int a = 10;
int b = 5;
System.out.println("Sum: " + (a + b)); // Output: 15
System.out.println("Difference: " + (a - b)); // Output: 5
System.out.println("Product: " + (a * b)); // Output: 50
System.out.println("Division: " + (a / b)); // Output: 2
System.out.println("Is a greater than b? " + (a > b)); // Output: true
System.out.println("Is a greater than 5 and b less than 10? " + (a > 5 && b < 10)); // Output: true
Definition: OOP is a programming paradigm based on the concept of objects, which can contain data and code.
Explanation: Java uses OOP principles to create organized and reusable code. Key concepts include:
public class Person {
private String name; // Private field
public String getName() { // Getter method
return name;
}
public void setName(String name) { // Setter method
this.name = name;
}
}
public class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("This dog eats bones.");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.eat(); // Calls Dog's eat method
}
}
interface Drawable {
void draw();
}
public class Circle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class MathOperation {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
public class Animal {
public void sound() {
System.out.println("Animal makes a sound.");
}
}
public class Cat extends Animal {
@Override
public void sound() {
System.out.println("Cat meows.");
}
}
Definition: Arrays are containers that hold a fixed number of values of a single type.
Explanation: Arrays are indexed from 0 and have a fixed size. Once created, their size cannot be changed.
Example:
public class ArraysExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Number at index " + i + ": " + numbers[i]);
}
}
}
Definition: A linked list is a data structure where each element (node) points to the next element.
Explanation: Linked lists allow efficient insertions and deletions but have a slower access time compared to arrays.
Example:
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add("Java");
list.add("Python");
list.add("C++");
for (String language : list) {
System.out.println("Language: " + language);
}
}
}
Definition: HashMap is a collection that stores key-value pairs with unique keys.
Explanation: HashMap provides efficient insertion, deletion, and access operations based on keys.
Example:
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap map = new HashMap<>();
map.put("1", "Java");
map.put("2", "Python");
map.put("3", "C++");
for (String key : map.keySet()) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
}
}
}
Definition: LinkedHashMap is a HashMap with predictable iteration order (insertion order).
Explanation: It combines the features of HashMap and LinkedList, providing predictable iteration order and efficient key-based access.
Example:
import java.util.LinkedHashMap;
public class LinkedHashMapExample {
public static void main(String[] args) {
LinkedHashMap map = new LinkedHashMap<>();
map.put("1", "Java");
map.put("2", "Python");
map.put("3", "C++");
for (String key : map.keySet()) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
}
}
}
Definition: Sets are collections that do not allow duplicate elements.
Explanation: Java provides several implementations of the Set interface, including HashSet, LinkedHashSet, and TreeSet, each with different properties regarding order and performance.
Example:
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("C++");
for (String language : set) {
System.out.println("Language: " + language);
}
}
}