If we want to divide a variable with some value, and then assign the modulus or remainder back to the original variable, then the Modulus Assign Operator is a better way of doing it. This operator when written is more concise and intuitive. Below are some examples on how to do Modulus Assign Operator in Java.
The divide assign operator is a short hand notation provided by Java so we could shorten our code where we want to divide a variable by some value and assign back the result to the variable. It is two operations in one statement to make our code much shorter, concise, and readable. Below are some examples on how we can use Divide Assign Operator in Java.
Increment Assign Operator in the previous example is a very common compound assign operator in Java. The next most common involves subtraction. What we want to do is to decrement the value of a variable, and assign it back to the variable - hence the name decrement assign. Below are some examples that we will share on how to perform decrement assign operator in Java.
A very common operation that we do with integer variables, is to add value to it. For example, if we want to add the value 5 to an existing variable a, we do the following statement: a = a+ 5. But since this is very common, Java made a short hand notation to make this even shorter. In this post, we will show examples on how to perform Increment Assign Operator to an integer variable.
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: