Java Misc

Java Regex, also known as Regular Expression, is a powerful tool used for manipulating and managing strings. It consists of a sequence of characters that form a pattern, which can be used to match and locate specific strings within a larger text. In Java, regular expression functionality is provided through the java.util.regex package, which includes…

Read More Java Regex – Regular Expression Examples

synchronized void method() {} public class SynchronizedInstanceMethodExample implements Runnable { public static void main(String[] args) { SynchronizedInstanceMethodExample runnable = new SynchronizedInstanceMethodExample(); new Thread(runnable).start(); // creates one thread new Thread(runnable).start(); // creates second thread } // Inherited run method from the Runnable interface @Override public void run() { try { // Calling the non-synchronized method print()…

Read More Java Synchronized Blocks and Methods

Sometimes we need to consume HTTPS endpoints that do not have valid certificates.  We can do that by configuring the Java HttpClient to accept all SSL certificates, even from an untrusted Certificate Authority. Here you will see how to communicate with HTTPS endpoint that may not have a valid SSL certificate. In the following example,…

Read More Java HttpClient – Accept all SSL Certificates

import org.apache.http.HttpResponse; import org.apache.http.HttpHeaders; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpUriRequest request = RequestBuilder.get() .setUri(“https://jsonplaceholder.typicode.com/todos/1”) // adding headers .setHeader(HttpHeaders.CONTENT_TYPE, “application/json”) .setHeader(“Authorization”, “Bearer 123token”) .build(); System.out.println(“Executing GET request…”); HttpResponse response = httpclient.execute(request);…

Read More Add a Custom HTTP Header with the HttpClient

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpget = new HttpDelete(“https://jsonplaceholder.typicode.com/todos/1”); System.out.println(“Executing DELETE request…”); HttpResponse response = httpclient.execute(httpget); System.out.println(“Status code: ” + response.getStatusLine().getStatusCode()); String…

Read More How to Execute an HTTP DELETE Request in Java (Solved!)

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPut; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPut httpPut = new HttpPut(“https://jsonplaceholder.typicode.com/posts/1”); // specify the PUT body to send to the server as part of…

Read More How to Execute an HTTP PUT Request in Java

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpResponse; import org.apache.http.entity.StringEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(“https://jsonplaceholder.typicode.com/posts”); // specify the POST body to send to the server as part of…

Read More Execute an HTTP POST Request in Java

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpget = new HttpGet(“https://jsonplaceholder.typicode.com/todos/1”); System.out.println(“Executing GET request…”); HttpResponse response = httpclient.execute(httpget); String responseBody = new BasicResponseHandler().handleResponse(response); System.out.println(“Response: ” +…

Read More Execute HTTP GET Request in Java

public class GenericClass <T> GenericClass<Type> obj = new GenericClass<Type>(); List<String> list = new ArrayList<>(); Map<Integer, String> map = new HashMap<>(); Set<String> set = new HashSet<>(); public class Test <T> { private T data; public Test(T data) { this.data = data; } public static void main(String[] args) { Test<Integer> test1 = new Test<>(10); Test<String> test2 =…

Read More Generics in Java with Examples

public class TestMultithreading extends Thread { public void run() { System.out.println(“The thread is running…”); } public static void main(String args[]) { TestMultithreading test = new TestMultithreading(); test.start(); } } Output: The thread is running…   Get the id and name of a thread public class TestMultithreading extends Thread { public void run() { System.out.println(“ID: “…

Read More Multithreading in Java

import java.io.Serializable; public class User implements Serializable { transient int userId; String name; public User(int userId, String name) { this.userId = userId; this.name = name; } } import java.io.* public class Test { public static void main(String args[]) { try { User user1 = new User(15, “Ryan”); //Write the object in a stream FileOutputStream outputStream…

Read More Transient Keyword in Java (Explained!)

In this tutorial, we will cover the basics of serialization and deserialization, including the importance of the ObjectOutputStream and ObjectInputStream classes, the use of the transient keyword, and the implementation of custom serialization methods. We’ll also provide code examples and address some common questions about these concepts. Let’s get started! What are Serialization and Deserialization…

Read More Serialization and Deserialization in Java: A Comprehensive Guide

public void printData(int data) { System.out.println(data); printData(data); } public static void printData(int data) { if (data > 0) { System.out.println(data); printData(–data); } } Output: 5 4 3 2 1 public class Test { public static void main(String[] args) { System.out.println(factorial(5)); } static int factorial(int num) { if (num == 1) { return 1; } else…

Read More What is Recursion in Java?