Java
Type Casting in Java
class Test { public static void main(String[] args) { short a = 10; int b = a; // automatically converts the short type into int type int c = 10; long d = c; // automatically converts the integer type into long type float f = d; // automatically converts the long type into float…
Read More Type Casting in JavaJava operators
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 operatorsJava Features
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 JavaUnrecognized 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