Java - Features
Java - Features
1. Basic Features
a. Data Types
Version: Java 1.0 (1996)
Part of: JDK
Explanation: Java has two categories of data types: primitive (like
int, char) and reference types (like arrays, objects).
Use Case: Storing different types of data.
Sub-Concepts:
o Primitive Data Types: int, char, double, boolean.
o Reference Data Types: Strings, Arrays, Objects.
Code Snippet:
java
Copy code
int number = 10;
char letter = 'A';
String greeting = "Hello, World!";
b. Control Statements
Version: Java 1.0 (1996)
Part of: JDK
Explanation: Control statements dictate the flow of the program
based on conditions.
Use Case: Making decisions in the code.
Sub-Concepts:
o Conditional Statements: if, switch.
o Looping Statements: for, while, do-while.
Code Snippet:
java
Copy code
if (number > 0) {
System.out.println("Positive");
} else {
System.out.println("Not positive");
}
c. Methods
Version: Java 1.0 (1996)
Part of: JDK
Explanation: Methods define reusable blocks of code that can take
parameters and return values.
Use Case: Encapsulating functionality for reuse.
Sub-Concepts:
o Static Methods: Methods that belong to the class rather than
instances.
o Instance Methods: Methods that belong to an instance of a
class.
o Abstract Methods: Declared in abstract classes and must be
implemented in derived classes.
o Virtual Methods: Methods that can be overridden in
subclasses.
Code Snippet:
java
Copy code
public class MathOperations {
public int add(int a, int b) {
return a + b; // Instance Method
}
a. Inheritance
Version: Java 1.0 (1996)
Part of: JDK
Explanation: Inheritance allows a class to inherit properties and
methods from another class, promoting code reusability.
Use Case: Creating a class hierarchy (e.g., Animal and Dog classes).
Sub-Concepts:
o Superclass: The class being inherited from.
o Subclass: The class that inherits from another class.
Code Snippet:
java
Copy code
class Animal {
void sound() { System.out.println("Animal sound"); }
}
b. Polymorphism
Version: Java 1.0 (1996)
Part of: JDK
Explanation: Polymorphism allows methods to do different things
based on the object that it is acting upon.
Use Case: Handling multiple types of objects through a common
interface.
Sub-Concepts:
o Method Overloading: Same method name with different
parameters.
o Method Overriding: Redefining a base class method in a
derived class.
Code Snippet:
java
Copy code
class MathOperations {
int add(int a, int b) {
return a + b; // Method Overloading
}
c. Interfaces
Version: Java 1.0 (1996)
Part of: JDK
Explanation: Interfaces define a contract that classes must
implement, allowing for multiple inheritance.
Use Case: Defining common behavior for different classes (e.g.,
IAnimal interface).
Code Snippet:
java
Copy code
interface IAnimal {
void speak(); // Abstract method
}
d. Exception Handling
Version: Java 1.0 (1996)
Part of: JDK
Explanation: Exception handling is a mechanism to respond to the
occurrence of exceptions during program execution.
Use Case: Preventing application crashes and handling errors
gracefully.
Sub-Concepts:
o try-catch: Catching exceptions.
o finally: Code that runs regardless of whether an exception
occurred.
Code Snippet:
java
Copy code
try {
int result = 10 / 0; // This will throw an exception
} catch (ArithmeticException ex) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Cleanup code here.");
}
e. Collections Framework
Version: Java 1.2 (1998)
Part of: JDK
Explanation: The Collections Framework provides data structures for
storing and manipulating groups of objects.
Use Case: Managing collections of objects efficiently.
Sub-Concepts:
o List: ArrayList, LinkedList.
o Set: HashSet, TreeSet.
o Map: HashMap, TreeMap.
Code Snippet:
java
Copy code
import java.util.ArrayList;
import java.util.HashMap;
a. Java Streams
Version: Java 8 (2014)
Part of: JDK
Explanation: The Streams API allows functional-style operations on
streams of elements.
Use Case: Processing collections of data in a declarative way.
Sub-Concepts:
o Stream Operations: Filter, map, reduce.
Code Snippet:
java
Copy code
List<String> names = Arrays.asList("John", "Jane", "Jack");
names.stream()
.filter(name -> name.startsWith("J"))
.forEach(System.out::println);
b. Lambda Expressions
Version: Java 8 (2014)
Part of: JDK
Explanation: Lambda expressions provide a clear and concise way to
represent a single method interface using an expression.
Use Case: Simplifying code for functional interfaces.
Sub-Concepts:
o Functional Interfaces: An interface with a single abstract
method.
Code Snippet:
java
Copy code
List<String> names = Arrays.asList("John", "Jane", "Jack");
names.forEach(name -> System.out.println(name)); // Lambda expression
c. Optional Class
Version: Java 8 (2014)
Part of: JDK
Explanation: The Optional class is a container object which may or
may not contain a value, helping to avoid NullPointerExceptions.
Use Case: Handling optional values more gracefully.
Code Snippet:
java
Copy code
Optional<String> optionalName = Optional.ofNullable(getName());
optionalName.ifPresent(name -> System.out.println(name));
d. Java Modules
Version: Java 9 (2017)
Part of: JDK
Explanation: The module system allows you to group related
packages and manage dependencies.
Use Case: Enhancing modularity and encapsulation in large
applications.
Code Snippet:
java
Copy code
module my.module {
exports com.example;
}
e. Annotations
Version: Java 5 (2004)
Part of: JDK
Explanation: Annotations provide metadata about the program, which
can be used by the compiler or at runtime.
Use Case: Improving code readability and enabling frameworks to
process annotations.
Code Snippet:
java
Copy code
@Override
public void method() {
// Implementation
}
4. Extra (Cutting Edge) Features
a. Records
c. Sealed Classes
d. Switch Expressions