The readers of our blog have already had a chance to get to know the duel of Ruby vs. PHP, as well as the combat Python vs. Ruby vs. Node.js. Today, we have Java vs. Ruby in turn.
Traditionally, we’re going to look at both technologies from a product owner’s perspective, which means learning not only technical differences between the languages, but also their actual use across different projects. Here we go.Warm-up
Before any competition, the contestants need to warm up. It also happens that this term is used by one of the wrestlers, Java, for the lazy class loading of Java Virtual Machine. So, let’s not step out of line and have the competitors spiffed up to the battle.Ruby
It’s not a secret that our company places a bet on this interpreted scripting language created by Yukihiro Matsumoto in the far 1995. A bunch of deep-rooted technologies like Smalltalk, Python, Perl, Ada, C++, and others had an impact on shaping Ruby’s syntax and features. As a result, we got a technology known for elegant and expressive coding, as well as increased flexibility and productivity of development. It means that engineers take pleasure in writing code with Ruby and are not restricted in their vision on how to execute one thing or another.At the same time, this technology cannot be called popular. According to the TIOBE index as of December 2018, it is ranked 17th. Nevertheless, the language is not dying and keep afloat with the 3.0 version expected to be released in 2020. To sum up, Ruby allows programmers to enjoy what they do and be productive at once by following the principle of least astonishment.Ruby key features
Apart from comparing Ruby to Java, let’s take a look at some key things one should know about this gem of a language.- Open-source
- Interpreted language
- Multi-platform
- Dynamic + duck typing
- Has a smart garbage collector
- Everything including methods, numbers and variable is an object
- You can embed Ruby into HTML
- High scalability
- Applicable for writing CGIs, building web and intranet apps
- Support of different GUI tools including OpenGL, Tcl/Tk, and GTK
- Support of Sybase, Oracle, MySQL, and DB2 connection
Ruby pros and cons
What you will like | What you won’t like |
---|---|
- Code length - Significantly reduced dev time - Rapid app development - Flexible and easy-to-understand syntax - Huge ecosystem of tools & libraries - Automatic memory management plus garbage collector - Big newbie-friendly community - TDD - Ruby on Rails - Supported language for AWS Lambda | - Runtime speed is not the Ruby’s best bower - Inapplicable for projects requiring memory-intensive tasks - Those who prefer following strict rules, won’t like the language’s unopinionated nature, which allows you to take a variety of approaches for a particular task (for example, you have four different ways to define methods) |
Java
Despite Railsware’s unhidden preference towards Matsumoto’s brainchild, Java is also a part of our team’s arsenal. For example, we leveraged it together with other technologies within the Philips Directlife project. That’s to say, we know both of them and are happy to use whichever language best suits the task at hand.In a sign of numerous Java and Ruby differences to be described a bit later, there is one undisputed similarity – they were born in the same year. Nevertheless, since then, Java has come through 11 versions with Java SE 11 as the latest one (compared to the Ruby’s latest release of 2.6.0). The technologies that influenced the creation of Java include C++, Ada 83, Object Pascal and others.The TIOBE index shows that this compiled programming language overcame C in ranking and holds the first position as of the end of 2018. Moreover, it is a primary technology for Android native app development, which by the way was recently enhanced with Kotlin. Another facet of Java is that it is also an ecosystem of tools including Java Development Kit (JDK) for writing/running/compiling Java code, Java Virtual Machine (JVM) for running software built with Java and JVM languages within the same infrastructure to some degree of interoperability (for example, Kotlin for expressiveness and conciseness, Scala for functional programming, etc.), and Java Runtime Environment (JRE) for running Java apps. The basic points why developers opt for this technology are its reliability, platform independence, and ease of use.Java key features
- Open-source
- Object-oriented language (there are objects, as well as primitives, which are limited to a few types)
- Both compiled and interpreted language
- Architecture neutrality
- Static typing
- Built-in security
- Dynamic compilation and dynamic loading of classes
- Automatic garbage collection
- Simple memory management model
- Multithreading capabilities
- Platform independence
Java pros and cons
What you will like | What you won’t like |
---|---|
- Static typing to build large codebase - Syntax derived from C - Code design thinking - Standard for enterprise computing (for example, bank related software is mostly written in Java) - Huge ecosystem - Built-in security manager - TDD - Backward compatibility - Write Once Run Anywhere concept, which means you can run your program be it built in Java or another programming language on any JVM-equipped device regardless of the operating system - Remote Method Invocation (RMI) - a methodology for distributed computing - Automatic memory management plus garbage collector - Multithreading capability - Java is a basic study for many colleges meaning there is no shortage in talent - Huge amount of educational resources, as well as tools - Plethora of job offers to Java specialists - Huge ecosystem with the support of multiple languages that compile to bytecode for JVM (as a result, you get more tailored languages for specific tasks like building a web app based on microservices architecture where each microservice can be created using a different language and then run together using an app container like Apache Tomcat) | - Performance may suffer due to compilation, as well as versatile CPU-intense features like garbage collector - JVM has a significant overhead on memory - Verbosity makes the code explicit and more difficult to read and scan - Garbage collector might cause noticeable delays in code execution - Memory leaks - NullPointerException (aka the billion dollar mistake) - Long compilation time |
Difference between Ruby and Java [Infographic]
Java vs. Ruby similarities and differences from a tech viewpoint
Before the face-off where you can learn more about Java vs. Ruby’s speed, productivity, performance, complexity and other aspects, let’s take a look at fundamental differences and similarities between them.What do they differ in?
Interpreted or compiled
As you know, for interpreted languages there is no need to compile the code into machine-language instructions before executing most of its implementation. For compiled technologies, we need compilers to generate machine code from the source one. Everything is clear with Ruby – it’s an interpreted language. As for Java, you can find a lot of sources claiming that it’s strictly compiled. At the same time, along with JIT (just-in-time) compilation of the bytecode from the Java compiler, some JVM implementations may interpret the bytecode as well. It is not an interpreter that you can observe in other programming languages like Ruby, but it’s also possible to interpret the Java code directly. So, Java can be called both an interpreted and compiled technology.Dynamic or static typing
A polar opposite difference between Java and Ruby lies in the way they apply a “type” to a variable. Static typing means that a programmer has to define what type each variable is before using them – types are checked before run-time. At the same time, in some JVM languages, types can be derived upon initialisation from other code. For example:int number = 1 vs. number = 1 It’s super easy for the compiler to derive the type. Another example would be:int methodReturningInteger() { return 42; } number = methodReturningInteger();compiler can figure out whether it is int.Such hybrid of static and dynamic typing can be found at many JVM languages including Kotlin and Apache Groovy, but not at Java, which is a genuinely statically typed technology.In dynamically typed languages, a programmer does not have to define types every time. Ruby doesn’t rely on types at all and cares about methods it needs to call on a given object (the famous duck-typing).
class Animal attr_accessor :name end class Desk attr_accessor :name endand later…
animal = Animal.new desk = Desk.new stuff = [animal, desk, animal, desk]It’s absolutely fine in Ruby. In statically typed languages the both classes would have to have a common interface (another type) or common base class they can both inherit from. What’s more:
whatever = stuff.first
print whatever.nameis also fine as long as the object responds to the method called name.Statically typed languages show better performance at run-time because they check types before running. At the same time, dynamic typing is associated with better project bootstrapping and changes smoothing, which is especially important in the early to middle stages.
Pure object-oriented or hybrid object-oriented
Object-oriented programming (OOP) allows engineers to define the type of data and its structure, as well as specify the set of functions applied to it. In the class-based OOP paradigm, an object is a chunk of memory containing different data (variables, functions, etc.). We know that Ruby defines everything as an object including all predefined and user-defined types. It is a pure object-oriented language. Java does not fall under this category because of the eight primitive types (byte, short, int, long, float, double, char, and boolean) that are not classified as objects. Hence, it is not a pure OOP technology and is usually referred to as a hybrid one.Method overloading
This term is an important OOP feature and denotes having methods with the same name but taking a different set of arguments. For Java, method overloading is a form of implementing generics. Since variables in Ruby are not typed, the arguments cannot differ in type, and this feature is not associated with this language.Operator overloading
As for operator overloading, Ruby supports the feature that allows developers to define an operator (+ or *) for user-defined types, while Java does not.What are the similarities?
Beside the difference between Ruby and Java, they share some similar features as follows:Garbage collection
It is a great technique, which allows engineers to build a memory safe system without the responsibility of memory management. Garbage collection is inherent in many high-level languages including Ruby and Java. At the same time, these technologies have a little variation as for the collectors they leverage. In addition to the mark-sweep algorithm associated with Ruby, JVM has such types of collectors as generational, incremental, and mark-compact as well.Multithreading
Both languages use standard libraries for concurrent and parallel execution of tasks. In Java, it can be achieved to run code in parallel – utilising all the CPU cores. In Ruby, it cannot be achieved and all you have is mere concurrency, which means that one thread starts, then it’s paused, another one is resumed, then it goes back to the first one, etc.Built-in security
In terms of Ruby vs. Java security, the web is full of claims that both are secure languages. Well, a disclaimer is needed here. Any language itself cannot be secure or protect a software created with it from vulnerabilities. At the same time, the technologies we’re comparing do have special features that facilitate avoiding common security flaws. As for Java, it is free of pointers, which can sometimes allow unauthorized access to memory, and has a Security Manager that allows you to run apps in a sort of sandbox to detect possible defects. Rubyists can enjoy a plethora of security libraries (they call them gems) including Brakeman, bundler-audit, Codesake:: Dawn, Tarantula, and others.Ruby language vs. Java – comparison table
Technology name | Ruby | Java |
---|---|---|
Type | General-purpose programming language | |
TIOBE index* (as of December 2018) | 17 | 1 |
Year of the first release | 1995 | 1995 |
Creator | Yukihiro "Matz" Matsumoto | James Gosling et al. |
Underlying technologies | C++, Ada, Lisp, Eiffel, CLU, Python, Dylan, Lua, Perl, Smalltalk | Object Pascal, Ada 83, UCSD Pascal, C++, Generic Java, C#, Modula-3, Eiffel, Mesa, Oberon, Objective-C |
Compilation | Interpreted language | Both compiled and interpreted language |
Typing | Dynamic | Static |
Object-orientation | Pure | Hybrid |
Language integration | C, C++, Java | C, C++ |
Built-in security | Yes | Yes |
Multithreading | Concurrent | Parallel |
Garbage collection | mark-sweep | generational, incremental, mark-compact, mark-sweep |
Access control | public, protected, private | public, protected, "package", private |
Inheritance | Single class, multiple "mixins" | Single class, multiple interfaces |
Web app frameworks | Ruby on Rails, Sinatra, Hanami | Spring, JSF, GWT |
Ruby or Java – the shootout from a product management viewpoint
In the following shootout, we will look at the languages from a variety of angles and try to answer such common questions as “Which one is easier to learn?”, “What language provides better performance?”, “What about Java vs. Ruby for web development?” and so on and so forth.Trends
Java Trend
In terms of search interest, Java experienced a gradual decline in the last decade. Now, people are looking for it for almost half as much than ten years ago.Ruby Trend
Ruby, in general, has a similar chart, but the year of 2019 ushered in a growth.Trend comparison
Meanwhile, the comparison of popularity between the two languages shows an undisputed winner. And it is JavaThe main interest in it comes from the enterprise and corporate sector of software development. Java circulates among large products being created by large teams of engineers. Companies having lots of Java developers may become hostages of circumstances and choose Java as a technology for the next product based on the available talent base. That, in turn, generates a big demand for Java-savvy guys and contributes to a higher search interest. Kind of a vicious circle. A huge ecosystem of libraries and frameworks is another interest growth factor.Ruby’s habitat includes dynamic and elegant products. It is a completely different scale, which does not correlate with high popularity. However, you should not treat the lack of enterprise-level support as a drawback. Besides, a recent trend is Rubyzation of corporations like Cisco, Autodesk, and others.Learning curve
I bet many knowledge seekers have asked Quora or searched through other web services regarding the learning complexity of both languages. Nevertheless, it’s unwise to give a strict answer because a lot of factors should be considered including the student’s mental capabilities, programming experience, and so on. Since Java as an educational subject is more popular in colleges than Ruby, it probably has a milder learning curve. However, the latter is more suitable for newcomers since it doesn’t force programmers into specific paradigms and releases beginner engineers from the most nitty-gritty of programming like reading a file (in Java you have no option but to use BufferedReader and InputStream, which is not needed most of the time; in Ruby, it is easier to start with as it’s just: File.read(path_to_file)), big number algebra, etc.. So, to answer the question “Java or Ruby which is better to study”, you need to have a clear understanding of what you want to do. If you want to start your path in programming – go with Ruby. If you are not a newbie and want to continue advancing your knowledge in computer science – Java is a great choice.Books to obtain knowledge
If you made up your mind to master either Ruby or Java, you’ll want to start with a recommended set of books is the place to start. In one of our earlier blog posts, we expressed our opinion on the top Ruby books. These include “The Well-Grounded Rubyist” by David A. Black, “Eloquent Ruby” by Russ Olsen, “Practical Object-Oriented Design in Ruby” by Sandi Metz, and others. Some of them have already come through recent updates and are the up-to-date knowledge sources.This is the first time Java has come to the Railsware blog, so we haven’t made any recommendations on books or courses to master this language. However, the dedicated developer community highlights Joshua Bloch’s “The Effective Java”, Brian Goetz’s “Java Concurrency in Practice”, and Herbert Schildt’s “Java: A Beginner’s Guide”. Actually, if you ask Amazon about books related to learning this programming language, be prepared to find over 6k results, while Ruby can only boast just over 1K.For our part, we advise you to consider two worth-reading books from The Robert C. Martin Series, namely The Clean Coder and Clean Architecture. They do not specify Ruby or Java explicitly but use the latter for almost all examples in the context.Scope of use
Both technologies are designed to be general-purpose, which means you can use them for a number of different projects including web apps and servers, low-level system utilities, picture recognition engines, versatile software tools, etc. Experience has shown that the scope of use of Ruby programming vs. Java are not opposites, just very much different. Today, the language associated with elegant syntax is mostly known for its web app framework – Ruby on Rails. Moreover, you may encounter a lot of disparaging articles where some claim RoR is more a language than Ruby. Though the popularity of Rails is huge, this should not detract from its underlying language, which has been leveraged in such cool projects as Sass (CSS extension language), Metasploit (penetration testing framework), Vagrant (a tool for building and distributing development environments), Homebrew (the missing package manager for macOS), and many others.Java’s domain of usage is wider and includes Android native apps (Kickstarter, FastHub), server apps at banking and financial services industry (Barclays, Standard Chartered and other industry giants opt for Java for their front and back office electronic trading systems and tools), web apps, software and development tools (Eclipse, Netbeans IDE), trading apps (Murex), Big Data technologies (Apache Accumulo, ElasticSearch), and a long list of further use cases. In short, Java is the King of programming. In addition to these, Java’s wide usage transcends just UI-based applications – such as back-end operations like web scraping in Java, which is employed in data mining and data harvesting from websites.Web development
We cannot neglect the both languages’ contribution to building web applications. Ruby on Rails is a standing customer of our blog and has a number of topics dedicated to apps built with RoR, top online courses to learn Rails, hosting providers, and even fintech project applicability. Along with this framework, Ruby underlies Sinatra, Hanami, Hobbit, etc. Web engineers have a bunch of Java-based tools in possession as well. These include JSF, Spring, Hibernate and many others. Nevertheless, if we compare any of them, for example Java Spring vs. Ruby on Rails, the latter will be more popular. And it keeps strengthening its position in the market.Ecosystem
“It has an extremely large ecosystem of libraries and tools” – this statement is applicable for both Java and Ruby. Meanwhile, each technology has its own peculiarities to be understood for the project you’re going to work on. The most important element of the Java ecosystem is JVM, which sets the background for languages other than Java, e.g., Kotlin, to flourish. Rubyists do not have a virtual machine in possession and take advantage of gems – libraries containing a specific piece of functionality like handling money, credit card integration, and versatile auxiliary tools to facilitate coding. JARs are a kind of analog of Ruby gems in Java. However, they have some qualities that make them very different from gems. JAR files are the deployment format for Java containing classes, icons, property files, and other resources. It would be cool to compare Ruby gems vs. Java JARs in detail someday.Performance
We’ve mentioned that poor performance is a drawback inherent to both technologies. This value can be defined only if compared with a better or worse value. When checking the Ruby vs. Java performance, the former technology is leading. Ruby is simpler hence faster than Java. The code written in Ruby changes on the fly, while its competitor needs to generate the byte code before it can run. The Ruby performance supremacy is true for small tasks only, while Java recoups huge computational needs. It performs better when the same tasks are performed repeatedly. I guess, the draw as for performance will be fair enough here.Productivity
Did you read about Ruby as a productivity booster, which allows you to cut your pipeline by one-third? Some authors claim even 10x faster development time. However, none of them managed to give some proof or at least specify the technology they compared Ruby with. All these terrific figures appeared for a reason – this programming language is a perfect fit for rapid development. A task, which will force you to write a bunch of C-syntax code lines in Java, takes fewer lines in Ruby. Moreover, the latter offers a high level of flexibility and readability. In practice, you spend less time on bug fixing and accelerate your pipeline. As for the Java vs. Ruby code comparison, the latter is an undisputed winner.Talent demand
Being the most popular language on the planet, Java is definitely a leader in the market of talents. Many experts predict it to be one of the most in-demand technologies in the coming years along with Python and JavaScript. And where is Ruby? It’s not even in the top ten, unfortunately. Nevertheless, it does not mean that the Ruby vs. Java job market comparison is pointless. The evident lower popularity of Ruby causes a lack of talent proficient in this language. For that reason, the average Rubyist’s salary in the US is over $137K per year, while Java experts earn a bit more than $120K. Long live Ruby on Rails!Which technology to choose for a project?
Let’s suppose, you have not made up your mind on which technology to choose for your project and are still at the crossroads. On the one hand, you have Ruby with its undisputed efficiency in development and elegance in work. On the other hand is Java, which virtually reigns the world of programming and can be used literally for any task you need. We’ll try to help you in decision-making by comparing Ruby vs. Java productivity.When to use Ruby
When it comes to web app development, Ruby with its powerful framework should be the first option to consider. Here you can discover why use Ruby on Rails. At the same time, RoR is not a silver bullet, which is fit for all tasks. However, it is best for startups with the following requirements:- Your idea is driven purely by the value to be provided to customers
- No fixed product concept is available yet
- Concise timeframe
- Frequent functionality revisions/project changes are expected
- Fast prototyping
- Any industry or business sector
- Small to middle app size
When to use Java
It goes without saying that Ruby isn’t Java, but it’s not a down-the-line argument to opt for the “King of programming”. This language is associated with being an enterprise-grade technology aimed at big data products and apps demanding high traffic and user base. Web app development is not Java’s focus area. Despite its huge popularity, a relatively small percentage of web apps are built with the technology. Java will fit if your startup is associated with the following:- Memory-hungry and complex app architecture
- High-volume and high-security product
- Interaction with a number of adjacent systems like mainframe back-ends, databases, peer web-services, background batch-processing systems, etc.
- Unlimited timeframes and resources
- You’re running or going to run your own server (otherwise, be prepared to drain your purse for hosting services)
Wrapping Up
Traditionally, our comparison blog posts end with key takeaways outlined above.Ruby | Java |
---|---|
- Interpreted programming language - Pure object-oriented programming - Elegant, mellifluous and concise syntax - Contains a bunch of features aimed at increasing developer’s productivity - Great performance for small tasks - Focus area - web app development | - Compiled and interpreted programming language - Number one in terms of global popularity - Hybrid object-oriented programming - Verbose code - Performant for huge tasks - Focus area - enterprise-grade software |