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.
String manipulation is an important part of any general purpose programming language. For example, we may want to extract some data from a given String or text. In this case, performing Substring is very important. This post will give some ways on how we can extract Subtring in the Groovy language. And since this is Groovy, some are just very cool.
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.