DEV Community

Cover image for Java vs Ruby - The Bare Bones
El Marshall (she/they)
El Marshall (she/they)

Posted on

Java vs Ruby - The Bare Bones

This week I started a Udemy course on Java, and have been learning the beginnings, so I wanted to type up my impressions of some basic differences between Java and the language I learned to code on, Ruby.

Overall, Ruby is written to be as easy and smooth for the developer as possible - it is intentionally flexible and readable, where other languages focus on other things. That sounds on the face like only a positive thing, and honestly it's one of the reasons I do really love Ruby, but there are pros and cons to each style.

To get into a few specific differences, firstly Java is Statically Typed while Ruby is Dynamically Typed. What Statically Typed means, essentially, is that the programmer must define variables' types before they are used, and the type of the variables cannot be changed. For example, in order to create a variable that will hold an integer in Java, you would need to type the following: int myVariable = 4; Then, if you tried later on to assign myVariable to a different data type, you would get an error.

Ruby, on the other hand, is Dynamically Typed, meaning that you can be much more loose about things. I could type myVariable = 5; and then three lines later say myVariable = "Hi!" and the variable would be reassigned with no issues.

The benefit of Static Typing is that it can prevent errors - the program will let you know right away if you have tried to assign something in a way that is not going to work out. There is more work up front for more assurance that your program will actually run when the time comes. Which brings me to the next point.

Java is a Compiled language where Ruby is Interpreted. With a Compiled language, the entire set of code is translated before attempting to run anything. In an Interpreted language, the code is compiled real-time while running the program. Again, there are pros and cons to each. The compiled language may help you catch errors early on that you wouldn't otherwise. On the other hand, an interpreted language is going to allow you to work with more flexibility, and demands less up front work before you can have something functioning to toy with.

Here is a post describing the differences between static and dynamic typing with more eloquence than I have, perhaps.

Overall Java requires much more explicit coding. A great deal of the beginning of this course has been dedicated just to learning about the 8 different primitive data types Java has, and how much capacity each one has, while Ruby has far fewer, more dynamic types.

Finally, Java doesn't recognize white space. That is, I could split my code onto separate lines with no effect on how it runs, unlike with Ruby or Javascript, where a line break in the middle of a line of code can break it. I'm not sure how I feel about this - on the one hand, it seems like it might allow you to play with the visual layout and readability of your code. On the other hand, it may be confusing to someone trying to interpret your code (maybe even you at a later date).

I've only just dipped my toes in the waters of Java at this point, so I can't say for sure how I feel about all these differences. I have a real soft spot for Ruby, but I also really love piecing together new systems - I get a wonderful little thrill when I figure out how to do something under a new set of rules, or how one piece of the puzzle fits into this new picture.

During my time at Flatiron, I initially rankled at the switch to Javascript and the extra bits and bobs of code needed as compared to Ruby (curly braces on if statements or writing 'let' ahead of my variables, for instance), but after spending some time with JS I not only got used to it, but often appreciated the way it clearly delimited some things.

Java is even more verbose and specific. I can't wait to spend more time with Java and see how I feel about its style and way of doing things!

Top comments (0)