Search results for: Java

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

class PrintArrayElementsExample { public static void main(String[] args) { int[] arr1 = {1, 7, 9, 5, 2, 8, 3}; String[] arr2 = {“Megan”, “Tom”, “Melissa”, “John”, “Steve”}; // print elements of the arr1 System.out.println(Arrays.toString(arr1)); // print elements of the arr2 System.out.println(Arrays.toString(arr2)); } } Output: [1, 7, 9, 5, 2, 8, 3] [Megan, Tom, Melissa, John,…

Read More Print Array Elements in Java

public class SplitString { public static void main(String[] args) { String names = “Tom,Steve,John,Megan,Melissa”; // split string String[] arr = names.split(“,”); // print the size of the array System.out.println(arr.length); // print the elements Stream.of(arr).forEach(System.out::println); } } Output: 5 Tom Steve John Megan Melissa   If you need a list, instead of an array, use the…

Read More Split a comma-separated String in Java

In this tutorial, you will learn how to implement the API Gateway Lambda Authorizer in Java. You will also learn how to make Lambda Authorizer validate Authorization JWT. First, we will create an API using API Gateway then we will make lambda function as an integration point for the GET method of our resource. After…

Read More API Gateway Lambda Authorizer Example in Java

How to declare an array in Java? Print array elements in Java Split a comma-separated String in Java How to break out of the loop in Java? Check if Array contains a value in Java Convert InputStream to a String in Java Find duplicate elements in an Array in Java Escape double quotes in Java…

Read More Java Example Programs

In this tutorial, you will learn to work with Java Spring Boot and build microservices simply and effectively. Let’s start with the introduction. Spring Framework in Java It’s an open-source, enterprise-level framework for creating standalone, production-grade applications that run on the Java Virtual Machine (JVM). One of the biggest advantages of Spring is that it…

Read More Java Spring Boot – Getting Started

In this tutorial, we are going to see how we can throw custom exceptions with a custom payload in a Spring Boot application and deploy it on AWS as a serverless Lambda function. Let us start by creating a simple Spring boot project. In this project, we are going to create a Request Handler that…

Read More Handle Custom JAVA Exceptions in AWS Lambda with Spring Boot

class CheckIfArrayContainsValue { public static void main(String[] args) { String[] programmingLanguages = {“Python”, “Kotlin”, “Ruby”, “JavaScript”, “C#”, “Java”, “Flutter”}; for (String lang : programmingLanguages) { if (lang.equals(“Java”)) { System.out.println(“It does contain!”); break; // value found, exit the loop } } } } Output: It does contain!   Here, we are iterating over the array, and…

Read More Check if Array contains a value in Java

import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.*; class ConvertInputStreamToString { public static void main(String[] args) { String resultString; String str = “Some String”; // create InputStream using the bytes from the above String InputStream inputStream = new ByteArrayInputStream(str.getBytes()); // Convert data from InputStream into a new String try (Scanner scanner = new Scanner(inputStream).useDelimiter(“\\A”)) { resultString =…

Read More Convert InputStream to a String in Java

<dependency> <groupid>org.apache.poi</groupid> <artifactid>poi-ooxml</artifactid> <version>5.0.0</version> </dependency> import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Product { private int id; private String color; private String size; private int cost; private String description; } import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import…

Read More Read and write Excel files in Java

This is a Java JSON tutorial. Here, you will learn what JSON is and how to work with JSON format in Java. You will see where to find the required JSON libraries and parse Java Objects to JSON and vice versa. Let’s get started. What is JSON? JSON (JavaScript Object Notation) is a lightweight text…

Read More Java JSON tutorial

In this tutorial, we are going to see how to handle exceptions from a Lambda function in the API Gateway. For Lambda exceptions, we have to map the error status returned by the Lamba functions to the HTTP Status of the methods in API Gateway. If not, the API Gateway returns a 200 OK status…

Read More Handling Exceptions from AWS Lambda written in JAVA