Java Enums is a new feature introduced in Java 5. Using Java Enum can help define a collection of constants in a more logical and meaningful way. Using Java Enum is a fluent way of defining constants that makes things more readable and maintainable. But sometimes we also need to convert a Java Enum to String, and that is what we will show in the examples below:
Java hashCode() method has a close relationship with the equals() method as they complement and are used together. On a high level, the equals method should inspect the relevant data of two instances to determine if they are logically the same object. What internal data the equals() method used should also be used to generate the hashCode() value of a specific object instance. Below are more detailed discussion with code examples about Java hashCode.
When we are dealing with a handful of data of the same type, we can use a different variable for each. But if we are working with arbitrarily more numbers of data of same type, array can be a good choice because it is a simple data structure to work with. List or Set is also a good choice, but arrays is much simpler to manipulate and work with. We explore below how do we work with Java Char Array. We will check different common scenario we encounter when working with char arrays.
Although a very old and dated data structure, array is still a very popular data structure to work with a collection of objects. This is because manipulation of arrays is very simple to learn and use. The only issue is it lacks advance features than can be found in the Collections API. For this tutorial, we will show how to Add elements to an Array in Java.
Byte Array is a convenient data structure that has many use cases. One scenario is when serializing some object to a file, and so we need to convert object to byte array when we write to file, and convert back from byte array to objects when we read from file. Another scenario is when sending data from one machine to another. Complex data structure is converted to a byte array before sending over the network, and on the other end the byte array is converted back to the original data structure. One of the simplest type of Object that we can convert to and from byte array is a String. This post will show how to convert Java Byte Array To String that can be applied to the use cases mentioned earlier.
Hash code in Java is very important specially with the introduction of the Java Collections API. Many classes in the Collections API uses hash code as a convention. So what exactly is a hash code? It is an integer representation of a specific Object instance. The assumption is that this integer value should be the same whenever the hashCode() method is invoked on the same Object more than once during an execution of a Java application. The hashCode method must consistently provide the same value provided that no internal data used in equals is modified on the Object. We should not worry about this on Immutable objects as internal data is not change throughout it's lifetime. In addition, if two objects are equal using the equals method of the class, then the hashCode of both should return the same value. This implies that the hashCode should be based on the data used in equals method. Lastly, it is not required that two unequal Objects should have different hashCode. Since hashCode is common to all classes as it is implemented in the Object class, we will discuss on how it is implemented in the String class of Java. This post will explain and give examples on Java String HashCode.
String is one of the most fundamental data structure. It is versatile and useful for many cases. This is why Strings are used in many cases instead of using more restrictive data structure. But sometimes we need to compare two or more Strings for specific programming needs. Below are som examples on how we can compare Strings in Java.
String is an all around data structure for saving information because it can handle arbitrary sequence of characters. Often times, it is also used to store numeric, date, or other type of information. Sometimes we need to convert a String object to another data structure. For example, if an HTML form submits a date information to the server, each data in the form are passed as String. In this case, we would need to convert the String to Date such that other computations can be performed. For example, determining the age based on birth date and current date. Below are some ways on how we could convert a String into Date for some specific condition or method.
String is just a staple of most programming languages. It represents a sequence of characters into one data structure. It is a very convenient way to store many types of things in memory or in the database. An array on the other hand is one of the most simple way to represent multiple values. It is simpler that Classes such as List or Set because it can also be found in older programming language such as C/C++ or Pascal. It is only natural that we sometimes want to convert a String into an array of Characters for easier manipulation later. Below are some ways on how we can Convert a String to Char Array in Java .
Object is the root class of all classes in Java. Every class that we use or declare in Java has Object as a super class when traced to the top. All objects, including primitive wrappers, implements the method of the Object class. Array on the other hand is a convenient data structure for holding multiple values with the same class or interface. This post will discuss some caveats about Java Array Of Objects.