How do you move from Java to Ruby?
In this guide, you’ll learn what are the major differences between the two languages to help you make the jump.
It’s great for the many people looking to move from Java/C# to Ruby.
But…
If you’re just curious about the differences, then this is also for you.
Let’s do this!
Static Typing vs Dynamic Typing
Typing style is the biggest & most notable difference when looking at code for either programming language.
Here’s the situation:
- Java uses static typing
- Ruby uses dynamic typing
What does that mean, exactly?
Typing refers to how variables & method arguments work.
A strongly (or statically) typed language has to declare what types (classes) every variable can accept.
It looks like this:
int n = 1;
Where int
is the type, in this case, Integer
.
Why is that useful?
Because if you try to assign any other type of object to this variable, the compiler will raise an error & your program won’t even get to run.
This means that you’ll always know what kind of object you’re working with.
With the disadvantage of being less flexible.
In contrast…
Dynamic typing is all about flexibility, variables aren’t tied to a particular type, so the type can change.
Here’s an example:
n = 1 n = "abc"
The downside is that you may run into more errors if your code is sloppy & you don’t get as much when reading code.
That’s one of the major differences!
More in the next section.
Syntax: Simple Things & Boilerplate
Java is the king of boilerplate.
Boilerplate is all that “setup” code that you have to add just to make your code syntactically valid.
As a result…
Java makes SIMPLE things complicated!
Hello World in Java:
class Hello { static public void main() { System.out.println("Hello World"); } }
Then you have to compile this into a class file & run it.
Two steps!
(IDEs help with this.)
Another difference is that expressions in Java end with a semicolon, which is not required in Ruby. You can also leave out parenthesis in Ruby, most of the time.
Hello World in Ruby:
puts "Hello World"
Yep.
That’s it.
It seems like Ruby results in cleaner code. Agree?
On top of that…
You can run this code with a single command from a terminal!
No compilation step needed.
Playing With Code: Built-In REPL
Ruby comes with irb, which makes testing out a bit of code really quick.
You don’t even need to save a file.
Or open an editor!
Java doesn’t have this built-in, at least it doesn’t until Java 9.
File Names & File Organization
Java forces a specific file naming structure & organization.
For example:
If you have a (public) class named Hello
, the file name MUST be Hello.java
.
This isn’t the case in Ruby.
Also, we don’t have the concept of a private class in Ruby.
Exception Handling
Java has two types of exceptions:
- Checked exception
- Unchecked exception
The difference?
You HAVE to handle checked exceptions!
Or your program won’t even run.
Now:
Ruby has only one type of exception.
You can handle it if you want, the interpreter won’t complain about it, but it may end up crashing your program.
Compiler & Language Licensing
Lastly, let’s talk about licensing.
This isn’t usually a problem with programming languages.
Why?
Because they’re open source, with one person as the head maintainer & designer.
Java is different here.
It to a big corporation (Oracle) & that has implications on it’s licensing.
In fact:
There are two versions of the Java runtime, “Oracle JDK”, which (if I understand correctly) starting with version 9 is a commercial product.
Then you have “Open JDK”, which also belongs to Oracle.
But it has an open-source license.
Library & Code Distribution
Another MAJOR difference, one of my favorites (also Matz’s favorite) about Ruby is RubyGems.
It makes distributing libraries (like HTTP clients) much easier.
Because:
- There is a central repository
- It’s integrated into the language
- Many open-source gems are available & easy to find
As far as I know, Java doesn’t have anything close to this, so this is a major win for Ruby.
Summary
You’ve learned about the major difference between Java & Ruby. Including things like static typing, boilerplate, and file naming.
Now you’re better prepared to understand Ruby so you can make the correct decision for you.
Thanks for reading! 🙂
I enjoyed this a lot. It was very informative.