Author: alegru

public class App { public static void main(String[] args) { // … } } public class App { public static void main(String[] args) { for(String s : args) { System.out.println(s); } } } public static void main(String[] args) public static void main(String []args)  public static void main(String args[]) public static void main(String… args) static public void main(String[] args) public static final void main(String[] args) final public static void main(String[] args)

Read More Java main method

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

public class Test { public static void main(String[] args) { String str = “Learn Java with alegrucoding.com”; String result = str.replaceAll(“\\s”, “”); System.out.println(result); } } Output: LearnJavawithalegrucoding.com import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String str = “Learn Java with alegrucoding.com”; String result = StringUtils.deleteWhitespace(str); System.out.println(result); } } Output: LearnJavawithalegrucoding.com

Read More Remove all whitespaces from a String 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

{ “firstName”:”John”, “lastName”:”Doe”, “username”:”johnDoe#1″, “membershipType”:”Platinum”, “address”: { “street”:”5th Avenue”, “city”:”New York”, “state”:”New York” } } import lombok.*; @Data @NoArgsConstructor @AllArgsConstructor public class Customer { private String firstName; private String lastName; private String membershipType; private Address address; } @Data @NoArgsConstructor @AllArgsConstructor public class Address { private String street; private String city; private String state; } import…

Read More @JsonIgnore Annotation in 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

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class ScheduledDemoApplication { public static void main(String[] args) { SpringApplication.run(ScheduledDemoApplication.class, args); } } import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTaskService { @Scheduled(fixedRate = 5000) public void execute() { // some logic that will be executed on a schedule System.out.println(“Code is being executed…”); } } Output:…

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

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