Print the first, last, and in-between integers of a range
As you ask for optimization:


You re-evaluate Math.abs() in each iteration of the loop, which is a constant value. Consider for(int n = 1, upperBound = Math.abs(b - a); n &lt; upperBound; n++) instead.
The same goes for if (b &gt; a) which is also constant. Maybe calculate an offset beforehand int offset = (b &gt; a) ? 1 : -1; and then just add the offset in the loop.
... and if you already calculate such an offset, you may also use it to replace getInitial() with initial = a + offset;