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:
In mathematics, factorial of a number means taking the product of all positive integers less than or equal to the given number. Or the product of multiplying all numbers from one to the given number. For example, the factorial of 6 is 1x2x3x4x5x6=720. Factorial has many uses, specifically in combinatorics or counting problems, as this is a building block for coming up with more complex counting formula. However, this also have uses in other branch of mathematics, hence writing program that is math heavy may need to have a way of computing factorial. If you are working with the Java programming language, this post might help. This post will show example on how to compute factorial in Java. We will help write a factorial program in Java.
The Fibonacci sequence is a very popular sequence in Math. The first two numbers in the sequence is 0 and 1. And all the succeeding numbers in the series is derived by adding the previous two numbers before it. For example, the third number in the sequence is derived by adding the first two which is 0 and 1, that equals to 1. The fourth is derived by adding the second and third 1+1 = 2. The fifth number is derived by adding the third and fourth 1+2 =3. And so on. And it is interesting to know that this sequence, which by first impression doesn't make sense, has many application in multiple discipline. For example, some traders who study technical analysis in stocks and other securities has observed that certain patterns in price movements follow the Fibonacci sequence. In software development, particularly in Agile practitioner, they follow task estimation using Fibonacci numbers. There are many more application of this. But this post will try to explore how to generate the Fibonacci series in Java.
In programming, tokenizing a String is a simple way of achieving lexical analysis. It is a simple way of producing parts of a String that we can process one by one. We achieve this in Java using the StringTokenizer class. Groovy has made it more convenient by providing the tokenize() method in the String class. Below are some examples on how to use tokenization of String in Groovy.
Enum is a very convenient way of representing a group of information that is fairly static. For example, Sex, Marital Status, and such can be represented by Enum. Enum is great way to do things instead of constant because it is more intuitive and easy to work with. However, when we store Enums to database or transfer it as a message to another process, we convert it into String. The question now is, how do we turn back a String to Enum? Below are some examples that converts a String to Enum in Groovy. And we know that Groovy has beautiful magic to do this.