A Java character is a primitive data type where it can only hold just 1 character value, whereas a String can contain multiple characters. A String represents a sequence of characters, while a char only contains a single character. For example, we can only assign the value 'A' to a char, and if we want to add more characters, we then need a String to do that. For example "AB" can't be stored using a char, but a String can. But sometimes, coming from a character, we wish to convert it into a String with only one char item inside. Below we explore some methods and examples on how to convert a Char To String in Java programming language.
File manipulation is a common programming theme that we need to do. As we store some data into a file, it is natural for us to be able to read and write into a file. The most common file format that we encounter is just a plain text file. Which means it would be natural also to read it's contents and put the value into a String variable. In this post, we present several solution on how to Read File To String in Java. We give several solutions, specially with the recent advancement in Java.
We always love working with Strings in our own programs. Strings is one of the most common data structure in most programming language. One common thing we wish to do with String is to clean it up. One common way of cleaning up Strings is by removing spaces from the beginning and the end of the value. For example, when we want to save first name or last name, having spaces at the beginning or the end of a name doesn't make sense. So we want to remove them. This is where the Java String's Trim() method comes in handy. This is what it exactly do. Below are more discussion about how to use Trim method of Java String.
The switch statement is a beautiful alternative to multiple if else, when all we want to do is match a variable's value to one of many values, to determine what to do. The switch statement is both concise, readable, and have better performance compared to multiple if else version. One issue though, before Java 7 was introduced, we can't use String value with switch statements. That is not fun as String is a well used data type. Sometimes we don't want to define enum and just want to work with Strings. Below are some way on how to use String in Java Switch statements.
Most String manipulation problems requires us to come up with combination of information and summarize into one String. For example, imagine we wish to convert an integer value into words as String, this will require us to combine multiple Strings to come up with an answer String. What is common to this is we need to append one String to another, to form a closer form to the answer. Below we explore how to do String Append in Java.
When we are programming with String, or doing String manipulation in short, a typical problem we encounter is we wish to concatenate together some Strings using a specific delimiter. For example, if we have an array of 4 String representing fruits [Apple, Banana, Orange, Grapes], and wish to concatenate them using delimiter dash "-", the resulting String we want is "Apple-Banana-Orange-Grapes". Good thing we don't need to implement our own, nor we don't need an external library from Apache or Google. There is already a method we can use inside Java - String Join. Below are some examples on how we could concatenate a list of String using a specific delimiter using Java String Join. Note that this feature only exists from Java 8 and above.
List in Java is a powerful data structure because it can hold arbitrary number of Objects. Unlike Arrays, List in Java can be resized at any point in time. For example, if we have a Java List of String, depending on the implementation, we can add as many String object as we want into the List. Contrast it with Arrays, if we declared an array of size 10, then we can only have around 10 Objects in an Array. If we want to have more, then we need to recreate the array. But for example, in Java List of String, we can add as many String instance as we want into the List as memory limitation would allow, without needing to recreate the data structure. Below are some examples on how we work with Java List of String.
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:
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.