Author: alegru

If you’re new to Java programming or just starting out, it can be a bit overwhelming to know where to begin. That’s why I’ve put together this guide to help you get set up with everything you need to start developing Java applications. In this tutorial, I’ll walk you through the steps of downloading and…

Read More Setting up Java Development Environment: A Complete Guide

Operator Description Example + Indicates positive value +a – Negates an expression value (Converts positive number to negative) -a ++ Increments a value by 1 ++a — Decrements a value by 1 –a ! Inverts the value of a boolean type !true gives false %= Assigns the remainder to the left operand when dividing the…

Read More Java operators

Java is a programming language that is widely known for its versatility, power, and many features. It is popular among developers due to its object-oriented design, portability, and strong security. Java’s object-oriented design allows developers to organize their code into modular units, making it easier to build large-scale applications. Java’s portability means that code can…

Read More Java’s Powerful Features: A Comprehensive Guide

Java main method is the entry point of any Java program. It’s the method that the Java Virtual Machine (JVM) calls to execute the program. In this tutorial, we’ll explore what the main method is, how it’s executed, and what the valid Java main method signatures are. We’ll also discuss some best practices for writing…

Read More Java Main Method: A Comprehensive Guide

class Vehicle { int maxSpeed = 250; public void printMaxSpeed() { System.out.println(“Max speed: ” + maxSpeed); } } class Car extends Vehicle { int maxSpeed = 300; @Override public void printMaxSpeed() { System.out.println(“Max speed: ” + maxSpeed); } } class Test { public static void main(String[] args) { Vehicle vehicle = new Car(); // upcasting…

Read More Upcasting Vs Downcasting in Java

{ “firstName” : “John”, “lastName”: “Doe”, “gender”: “Male”, “state”: “Texas” } public class Person { private String firstName; private String lastName; private String state; // constructors, getters and setters } public class Person { private String firstName; private String lastName; private String gender; <<<< private String state; } import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class…

Read More Unrecognized field not marked as ignorable – Java Jackson

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

To schedule a task in Spring Boot we use the @Scheduled annotation.  We place the @Scheduled annotation above the declaration of the method that should not expect any parameters, and the return type should be void.  Spring Boot internally uses the TaskScheduler interface to schedule the annotated execution methods. Schedule a task in Spring Boot – steps…

Read More How to Schedule a Task in Spring Boot

some data in a file import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.FileCopyUtils; import java.io.*; import java.nio.charset.StandardCharsets; @SpringBootApplication public class DemoApplication { public static void main(String[] args) throws IOException { SpringApplication.run(DemoApplication.class, args); readFile(); } public static void readFile() throws IOException { // read a file Resource resource = new ClassPathResource(“classpath:data.txt”); // get inputStream…

Read More Read a File From the Resources Folder in Spring Boot