Zach Cochran
by Zach Cochran
2 min read

Categories

Tags

Rather than sitting down to watch a bunch of videos that were going to end up putting me to sleep this evening, I decided to hop right back into things and start trying to solve some practice problems with Java.

As I mentioned in my blog yesterday (I think?), it’s been a loooong time since I’ve touched Java. Javascript feels very safe to me. Java feels like something that wants to eat my face.

So I decided I’d say screw it and start tackling some prblems, using google as needed to work my way through thing. Tonight I did just a handful of problems off of exercism.io to get my toes wet again.

Here are a few of the things I learned/observed/experienced tonight:

Arrays are weird (to me)

I think arrays were some what like this in golang, but it’s been so long I honestly don’t remember.

Arrays in java must be of the same data type. There is no mixing and matching. Looks like you have to use an object for that.

With java you end up defining whether or not something is an array by attaching it straight to the type.

For example, if I want to make a new array of integers I could do the following:

int[] nums1 = new int[5];
int[] nums = {1,2,3,4}

Both of those would be valid ways to initialize new integer arrays.

JUnit is pretty swell

I’ve always used things that were kind of off shoots of JUnit, but never the tool itself. It’s actually pretty nice.

Granted I’ve only used the test scripts that have come with the sample problems, but they’re incredibly easy to read with a nice way to include or exclude the tests from the test suite.

Reversing a String

So evidently strings to not have a built in method for reversing in Java. BUT, there is something called a StringBuilder that can be reversed.

It looks like the old way for handling strings used to be to create a StringBuilder object, and then append your strings using that method.

However, in more recent version, evidently concatinating with a simple + takes care of that for you. Evidently it compiles to the same thing behind the scene, and that’s what people are recommending to use now.

Anyways, to reverse the string, you just need to stick it into a StringBuilder and reverse it:

return new StringBuilder(inputString).reverse().toString();

Or you can do it the long way:

int i;
int len = inputString.length();
String backwards = "";
for( i = len - 1; i >=0; i-- ){
    backwards += inputString.charAt(i);
}
return backwards;

💚