In Python, Sorting the string alphabetically is a common task helps for string manipulation or text analysis. It is used in creating the anagrams, checking for permutations, or standardizing the input for consistent processing, for example, turning 'banana' into 'aaabnn'. In this article, we will explore how to sort the letters in a string alphabetically, this can be done by using the Python built-in function. Using Python sorted() Function The Python sorted() function is used to return a new sorted list from the items in the iterable object. The order of sorting can be set to either ascending ... Read More
In Java, both String and StringBuffer classes are used to represent sequences of characters. However, the String class is immutable, which means once a String object is created, its value cannot be changed, while the StringBuffer class is mutable. It allows us to modify the contents of the string without creating a new object. In this article, we will discuss why we should use the StringBuffer class instead of String in Java. Why Use StringBuffer Instead of String? A few reasons why StringBuffer is often preferred over String is given below: A StringBuffer is thread-safe ... Read More
In Java, the string literals (or, string objects) are stored in a separate memory area called string constant pool to improve the performance of string operations and optimize the memory while using them. Let's understand how. Creating String Objects in Java There are two ways to create a String object in Java: Using the new operator Using String literal Example The example given below shows how to create a string object: public class Example { public static void main(String[] args) { ... Read More
The errors that occur during runtime after compilation are called exceptions. To handle and rectify these errors, it is necessary to understand the cause and the point where the error occurs. There are two ways to find the details of the exception: one is the printStackTrace() method, and another is the getMessage() method. Both are used for exception handling in Java, but they are different from each other. Let's discuss how. The printStackTrace() Method The printStackTrace() method is defined in Throwable class of java.lang package and it is inherited into both java.lang.Error class and java.lang.Exception class. When you use it ... Read More
We can use the datetime module to convert a datetime to a UTC timestamp in Python. If we already have the datetime object in UTC, then the timestamp() function can be directly used to get a UTC timestamp. This function returns the time since epoch for that datetime object. If we have the datetime object in the local timezone, first replace the timezone info and then fetch the time. The following are the various methods to convert a datetime object into a UTC timestamp in Python. Using datetime.timestamp() with UTC-aware datetime Local ... Read More
In Python, the mktime() function (from the time module) assumes that the passed tuple is in local time, while the calendar.timegm() (from the calendar module) assumes it's in GMT/UTC. Depending on the interpretation, the tuple represents a different time, so both functions return different values (seconds since the epoch are UTC-based). The difference between the values should be equal to the time zone offset of your local time zone. Understanding time.mktime() in Local Time Context The Python time.mktime() method converts the object form of local time into seconds since the epoch (January 1, 1970, 00:00:00 UTC). This method is the inverse function of localtime() and ... Read More
In Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization. We use these to denote or specify annotations before a particular field or method (that is declared in Java). Using an annotation before a field, we can denote whether it is a variable, is a JsonProperty, should be ignored, or what condition should be applied to it. So basically, Annotations make JSON output clearer as we required. In this Article, we will learn about ... Read More
Jackson is a Java library that is used to convert JSON to Java objects and vice versa. Conversion of JSON to a Java object is called deserialization, and Java object to JSON is known as serialization. Both of these tasks can be done by using the Jackson library. In this article, we are going to learn how to convert JSON to Java objects using the Jackson library. Jackson Library: Convert a JSON to a Java Object The ObjectMapper class belongs to the Jackson library. This class is responsible for the serialization and deserialization of Java objects. The ObjectMapper class is used ... Read More
To check if an ArrayList contains a specific item, we can either compare each element with the given item and print the result if they are equal, or use a predefined method that directly checks for the containing element. ArrayList in Java In Java, an ArrayList is a class similar to an Array, but unlike an array, it has a dynamic size that automatically increases or decreases, depending on the number of elements added or removed from it. Following is the syntax for creating an ArrayList in Java - ArrayList list_name = new ArrayList(); Here, ... Read More
A list stores a sequence of elements of a similar type. Like an array, the elements in a List are stored at specific indices, starting from index 0. The 0th index indicates the "first element", the 1st indicates the "second" element, and so on. In Java, a list is represented by the interface named List (with the same name) that extends the Collection interface. To create a List object, we can instantiate any class that implements the List interface, such as ArrayList, Stack, Vector, etc (Since we cannot instantiate an interface). We can get the first element of the List in ... Read More