Java Articles

Page 123 of 450

How do I get a platform-independent new line character?

guru
guru
Updated on 01-Sep-2025 721 Views

When working with text in Java we often need to include new line characters to the format output properly. Different operating systems have different conventions for new line characters: Windows: This uses \r (Carriage Return + Line Feed). Unix/Linux: This uses (Line Feed). Mac (pre-OS X): This uses \r (Carriage Return). To write code that works seamlessly across all platforms and we need to use a platform-independent way of handling new line characters. This article will guide us through the different methods available in the Java to achieve this. Using Platform-Independent New Line Characters The recommended way ...

Read More

How can we format a date using the Jackson library in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 3K+ Views

Jackson is a Java-based library, and it can be useful to convert Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can format a date using the setDateFormat() of ObjectMapper class. This method can be used for configuring the default DateFormat when serializing time values as Strings and deserializing from JSON Strings. Syntax of setDateFormat() public ObjectMapper setDateFormat(DateFormat df) Here, df is the DateFormat instance to be used for formatting dates. Steps to format a date using Jackson in Java Following ...

Read More

Convert POJO to XML using the Jackson library in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 7K+ Views

Jackson is a library that allows you to convert Java objects into XML and vice versa. In this example, we will demonstrate how to convert a POJO (Plain Old Java Object) into XML using the Jackson library. Well, if you are not familiar with POJO, it is a simple Java object that does not follow any specific framework or design pattern. It is just a regular Java class with fields and methods. We will use the method writeValueAsString() of the XmlMapper class to convert a POJO into XML. The XmlMapper class is part of the Jackson library and it ...

Read More

How can we ignore the fields during JSON serialization in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 14K+ Views

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization. In this article, we will learn how to ignore the fields during JSON serialization in Java using the Jackson library. Steps to ignore the fields during JSON serialization in Java: In order to use Jackson, you will need to add it to your project. If you use Maven, add the following dependency to your ...

Read More

Differences between Method Reference and Constructor Reference in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 3K+ Views

The Method Reference and Constructor Reference are part of Java 8's functional programming features, they used for refering to methods and constructors without executing them. They are often used in conjunction with functional interfaces, such as those defined in the java.util.function package. Method Reference A method reference is a shorthand representation of a lambda expression for calling a method. A method reference refers to a method without executing it. Method references can refer to static methods, instance methods, and constructors. Constructor Reference A constructor reference is a unique kind of method reference that is a reference to a constructor. ...

Read More

Constructor Chaining In Java programming

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 2K+ Views

The constructor chaining is a specific sequence of calling constructors when a user initializes an object in a certain way. This technique is used when multiple constructors are invoked one after another, based on the instance class. It is also closely related to inheritance, where the role of a subclass constructor is to call the constructor of its superclass. You can perform constructor chaining in two ways: Within the same class - where one constructor calls another constructor of the same class. Across different classes - where a subclass constructor calls ...

Read More

Why an interface doesn\'t have a constructor whereas an abstract class have a constructor in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 01-Sep-2025 9K+ Views

Interfaces in Java are used for defining a contract that classes can implement. They can contain method signatures, default methods, static methods, and constants. we must implement the methods defined in the interface in the implementing class. Constructor in an Interface A Constructor is to initialize the non-static members of a particular class with respect to an object. An Interface in Java doesn't have a constructor because all data members in interfaces are public static final by default, they are constants (assign the values at the time of declaration). There are ...

Read More

How do we check if a String contains a substring (ignoring case) in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 5K+ Views

Problem Statement The task is, given a string and a substring, you have to check if a substring is present in a string or not. Do not mind if the case of the string and substring is different, it should not matter. For example, if the string is "Hello World" and the substring is "hello", then the output should be true. Even if the substring is "HELLO" or "HeLLo", the output should be true. Solution To solve this problem, we will first convert the string and the substring to lowercase using the toLowerCase() method. The toLowerCase () method of the ...

Read More

Sorting contents of a string that holds integer values in Java

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 4K+ Views

Problem Statement Here we have a string that contains integer values, our task is to sort those integer values in ascending order. For example, if the string is "31134", the sorted string should be "11334". Solution We can solve the above problem using multiple ways: Using char array Using Stream API Using Bubble Sort Using char array We will use the toCharArray() method of the String class to convert the string into a character array. Then we will sort the character array using the Arrays.sort() method. ...

Read More

How to get first and last elements from ArrayList in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 16K+ Views

ArrayList is a part of the Java Collections Framework. Which is a dynamic type of array that can grow and shrink as needed. It is a resizable array implementation of the List interface. The ArrayList class is used when we want to store a list of elements in a dynamic array. In this article, let's learn how to get the first and last elements from an ArrayList in Java. The get() method of the ArrayList class accepts an integer representing the index value and returns the element of the current ArrayList object at the specified index. Therefore, if you pass ...

Read More
Showing 1221–1230 of 4,498 articles
« Prev 1 121 122 123 124 125 450 Next »
Advertisements