java
Java main method
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 methodFormatting Output with printf() in Java
Java.io package contains a PrintStream class with two formatting methods: format() and printf(). This tutorial will teach formatting output with printf() in Java. Here, we will first deconstruct the printf() syntax. After that, we will practice a few of the examples through which you will learn to format string, number, boolean, date, and time in java.…
Read More Formatting Output with printf() in JavaUpcasting Vs Downcasting in Java
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 JavaRemove all whitespaces from a String 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 JavaCheck if Java String is Empty, Null or Whitespace
In this tutorial, you will learn how to check if a Java string is empty, null or consist of a whitespace character. To learn more about Java, please check the Java category page. Let’s start with a simple example that checks if String is null. Check If Java String is Null If a String variable…
Read More Check if Java String is Empty, Null or WhitespaceHow to Split String in Java
This tutorial will teach you how to split a string in Java. There is more than one way to split a string, so we will look at several ways. Let’s start with a very basic one. Split a String To split a string in Java, you can use a String class’s split() method. The split()…
Read More How to Split String in JavaConvert Image byte[] Array to Base64 encoded String in Java
In this tutorial, you will learn how to convert an image byte[] array into a Base64 encoded string of characters. Additionally, you might be interested in checking the “Converting byte[] Array to String in Java” tutorial. Convert Image to a byte[] Array Let’s assume that we have an image on our computer. Our task is to…
Read More Convert Image byte[] Array to Base64 encoded String in JavaConverting byte[] Array to String in Java
This tutorial will teach you how to convert an array of bytes to a string of characters in Java. Additionally, you might be interested in learning how to convert an image/file byte[] array to a Base64 encoded String. If you are, then check the following tutorial. How to Convert Image byte[] Array to Base64 encoded…
Read More Converting byte[] Array to String in JavaHow to Round Float or Double in Java
In this tutorial, you will learn how to round a Float or a Double value to 2 decimal places in Java. 1. Using BigDecimal One of the ways to round a float or a double value is to use BigDecimal class. BigDecimal bigDecimalDouble = new BigDecimal(doubleValue); BigDecimal class allows us to set a scale and…
Read More How to Round Float or Double in JavaHow to Convert String to int in Java
This tutorial will teach you how to convert a String value to an int value in Java. Convert String to int To convert String value to an int value, you can use Integer.parseInt() function. The parseInt() method accepts a String value as a parameter and returns an int value as a result. Successful conversion public…
Read More How to Convert String to int in JavaTest for Exception in JUnit 5 and JUnit 4
In this tutorial, you will learn how to write a JUnit Test that validates if the method under the test throws the correct exception. This tutorial will have examples for JUnit 5 and JUnit 4 as well. Let’s begin with JUnit 5 version first. JUnit 5. “assertThrows()” Assertion To test for exceptions in JUnit 5…
Read More Test for Exception in JUnit 5 and JUnit 4Unrecognized field not marked as ignorable – Java Jackson
{ “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@JsonIgnore Annotation in Java
{ “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 JavaHow to Declare an Array in Java?
dataType[] arr; Where: int[] intArray = new int[5]; String[] arr = new String[3]; arr[0] = “value 1”; arr[1] = “value 2”; arr[2] = “value 3”;
Read More How to Declare an Array in Java?