0% found this document useful (0 votes)
4 views

Java - Features

The document provides a comprehensive overview of Java features, categorized into basic, medium, advanced, and cutting-edge features. It covers data types, control statements, methods, classes, inheritance, polymorphism, exception handling, collections, and newer features like records, pattern matching, and sealed classes. Each section includes explanations, use cases, and code snippets to illustrate the concepts.

Uploaded by

camilo.ms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java - Features

The document provides a comprehensive overview of Java features, categorized into basic, medium, advanced, and cutting-edge features. It covers data types, control statements, methods, classes, inheritance, polymorphism, exception handling, collections, and newer features like records, pattern matching, and sealed classes. Each section includes explanations, use cases, and code snippets to illustrate the concepts.

Uploaded by

camilo.ms
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

JAVA

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");
}

for (int i = 0; i < 5; i++) {


System.out.println(i);
}

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
}

public static int multiply(int a, int b) {


return a * b; // Static Method
}
}
abstract class Shape {
abstract void draw(); // Abstract Method
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing a circle");
}
}

d. Classes and Objects


 Version: Java 1.0 (1996)
 Part of: JDK
 Explanation: Classes are blueprints for creating objects,
encapsulating data and behavior.
 Use Case: Modeling real-world entities in a program.
 Sub-Concepts:
o Constructors: Special methods for initializing objects.
o Destructors: Java uses garbage collection, so explicit
destructors are not present, but you can override finalize().
 Code Snippet:
java
Copy code
public class Dog {
private String name;

public Dog(String name) {


this.name = name; // Constructor
}

public String getName() {


return name;
}
}
2. Medium Features

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"); }
}

class Dog extends Animal {


void sound() { System.out.println("Bark"); } // Method overriding
}

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
}

double add(double a, double b) {


return a + b;
}
}

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
}

class Dog implements IAnimal {


public void speak() {
System.out.println("Bark");
}
}

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;

ArrayList<String> list = new ArrayList<>();


list.add("Hello");

HashMap<String, Integer> map = new HashMap<>();


map.put("One", 1);
3. Advanced Features

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

 Version: Java 14 (2020)


 Part of: JDK
 Explanation: Records are a special kind of class in Java designed to
act as transparent carriers for immutable data. They provide a
compact syntax for declaring data-carrying classes without boilerplate
code.
 Use Case: Reducing boilerplate code for data classes, enhancing code
readability, and providing a clear intent of data storage.
 Sub-Concepts:
o Implicitly Final: Records cannot be subclassed.
o Canonical Constructor: Automatically generated constructor
based on declared fields.
o equals() and hashCode(): Automatically generated based on
fields.
 Code Snippet:
java
Copy code
public record Person(String name, int age) {}

public class Main {


public static void main(String[] args) {
Person alice = new Person("Alice", 30);
System.out.println(alice.name()); // Outputs: Alice
System.out.println(alice.age()); // Outputs: 30
}
}
b. Pattern Matching for instanceof

 Version: Java 16 (2021)


 Part of: JDK
 Explanation: Pattern matching simplifies the use of the instanceof
operator by allowing you to test an object’s type and cast it in a single
operation. This reduces boilerplate code and enhances readability.
 Use Case: Making type checks and casts cleaner and more expressive.
 Sub-Concepts:
o Type Guarding: The variable is automatically cast to the
specified type within the scope of the pattern.
 Code Snippet:
java
Copy code
public static void printLength(Object obj) {
if (obj instanceof String s) {
System.out.println("String length: " + s.length());
} else {
System.out.println("Not a string");
}
}

public class Main {


public static void main(String[] args) {
printLength("Hello, World!"); // Outputs: String length: 13
printLength(100); // Outputs: Not a string
}
}

c. Sealed Classes

 Version: Java 17 (2021)


 Part of: JDK
 Explanation: Sealed classes allow you to control which classes can
extend or implement them. This feature helps in defining a more
controlled class hierarchy.
 Use Case: Enforcing a specific structure in class hierarchies,
enhancing maintainability and security.
 Sub-Concepts:
o Permits Clause: Specifies which classes are allowed to extend
the sealed class.
o Non-Sealed Classes: Classes that can be extended without
restriction.
 Code Snippet:
java
Copy code
public sealed class Shape permits Circle, Square {}

public final class Circle extends Shape {


// Circle implementation
}

public final class Square extends Shape {


// Square implementation
}

// Attempting to extend Shape in a non-permitted way will result in a


compile-time error.

d. Switch Expressions

 Version: Java 12 (2019)


 Part of: JDK
 Explanation: Switch expressions extend the traditional switch
statement to allow it to return a value, providing a more expressive
way to handle multiple conditions.
 Use Case: Simplifying the syntax for handling multiple cases and
returning values from a switch statement.
 Sub-Concepts:
o Arrow Syntax: Using -> to define case branches.
o Yield Statement: Returning a value from a switch expression.
 Code Snippet:
java
Copy code
public class Main {
public static void main(String[] args) {
int day = 2;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Invalid day";
};
System.out.println(dayName); // Outputs: Tuesday
}
}

e. Foreign Function & Memory API (Incubator)

 Version: Java 17 (2021)


 Part of: JDK (Incubator)
 Explanation: This API allows Java programs to interoperate with code
and data outside of the Java runtime, enabling access to native
libraries and memory.
 Use Case: Accessing native libraries directly from Java code,
enhancing performance for certain operations.
 Sub-Concepts:
o Foreign Functions: Calling external C functions.
o Memory Access: Reading and writing to native memory.
 Code Snippet:
java
Copy code
import jdk.incubator.foreign.*;

public class Main {


public static void main(String[] args) {
// Example to access foreign functions will be available when the API is
stable.
}
}

f. JEP 411: Deprecate the Security Manager for Removal

 Version: Java 17 (2021)


 Part of: JDK
 Explanation: This JEP proposes to deprecate the Security Manager
and its associated API for removal in a future release.
 Use Case: Reflects a shift in Java's security model, encouraging
modern alternatives.

g. JEP 382: New macOS Rendering Pipeline

 Version: Java 17 (2021)


 Part of: JDK
 Explanation: A new rendering pipeline for macOS is introduced,
utilizing Apple’s Metal framework for better performance.
 Use Case: Improving graphics rendering performance on macOS.

h. Improved Pattern Matching for switch (Preview)

 Version: Java 17 (2021)


 Part of: JDK (Preview)
 Explanation: This feature enhances the switch statement to support
pattern matching, allowing for more flexible code.
 Use Case: Simplifying complex switch-case logic.
 Code Snippet:
java
Copy code
public void handleInput(Object input) {
switch (input) {
case String s -> System.out.println("String: " + s);
case Integer i -> System.out.println("Integer: " + i);
default -> System.out.println("Unknown type");
}
}

You might also like