UTIL PACKAGE / NOTES</>↗Util Package07 Apr 2025
In Java, the Optional class provides a way to handle possible null values in a more functional style. If you’re using an Optional and want to provide a default value, you can do so using the orElse() or orElseGet() methods. Here’s an explanation and code examples for both: 1. Using Optional.orElse() The orElse() method provides […]
UTIL PACKAGE / NOTES</>↗Util Package07 Apr 2025
To check if a Java Optional has a value, you can use the isPresent() or isEmpty() methods: Using isPresent() This method returns true if the Optional contains a value, and false if it is empty. Optional<String> optional = Optional.of("Hello"); if (optional.isPresent()) { System.out.println("Value is present: " + optional.get()); } else { System.out.println("Value is not present."); […]
UTIL PACKAGE / NOTES</>↗Util Package07 Apr 2025
To create an Optional in Java, you can use the Optional class, which was introduced in Java 8 as part of the java.util package. It is used to represent a value that can either exist (non-null) or be absent (null), making your code more robust and reducing the risk of NullPointerExceptions. Here are some common […]
CORE API / NOTES</>↗Core API07 Apr 2025
To set and read custom HTTP headers using HttpURLConnection in Java, you can make use of its methods setRequestProperty to set headers and getHeaderField to read them. Here’s how you can do it: Setting Custom HTTP Headers You can set custom HTTP headers on a request using the setRequestProperty method. For example: package org.kodejava.net; import […]
HTTPCLIENT / NOTES</>↗HttpClient06 Apr 2025
In Java 11, the HttpClient API was introduced as part of JEP 321, providing a standardized way to perform HTTP operations. To send a POST request with JSON data, you can use the HttpClient along with its HttpRequest object. Here’s a step-by-step guide and example: Steps to Send POST Request with JSON Data in Java […]
HTTPCLIENT / NOTES</>↗HttpClient06 Apr 2025
In Java 11, you can use the HttpClient to send HTTP requests easily. Here’s how you can send a simple GET request using HttpClient: Full Example: package org.kodejava.net.http; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; public class SimpleGetRequestExample { public static void main(String[] args) { try { // Create an HttpClient instance HttpClient httpClient […]