The first programming language I learned was Ruby. After that I worked on Clojure, finding the experience of writing code in a completely different language quite interesting. Perhaps it wasn't as different as it seemed on the surface. The more I learned about Clojure, the more comfortable I became using the language. To put it simply, some things seemed easier to do and some things seemed more difficult. As I mentioned in my [last post](http://www.ryanverner.com/post/38956624755/defining-classes-methods-and-variables-in-java), my latest challenge is the Java programming language. When approaching something new, we try to form a bridge between our past experiences and the knowledge we've gained from them and this new thing we're encountering for the first time. We latch on to previous experiences and look for commonalities. So as I begin learning Java, I'm relating it to Ruby (and to a lesser extent, Clojure). This post is about the transition from Ruby to Java. ***Similarities*** * Objects are [strongly typed](http://www.artima.com/weblogs/viewpost.jsp?thread=7590). * There are restrictions on how objects of different types can be used together. * Java requires that each variable has a defined type. * Ruby is strongly typed in the sense that type errors are prevented at runtime and most type conversion is explicit, but it does not use static type checking (we'll go into this later) so the compiler does not check or enforce type constraint rules. Ruby: string = "hello" number = 5 string + number #=> TypeError: can't convert Fixnum into String Java: String myString = 6 //=> error: incompatible types // String declares the type // myString is the name of the variable // 6, however, is not a string, so we get an error * Memory is managed for you via a garbage collector. * Garbage collection is a form of automated memory management. Essentially, the garbage collector is looking for objects in memory that are no longer in use. The resources (memory) these objects use can be reclaimed. ***Differences*** * In Java, you compile your code. * In Java, you do not use the `end` keyword after class and method declarations. Everything is wrapped in `{ }` instead. * In Java, you use `import` instead of `require` for file inclusion. This is a compile-time element. * In Java, member variables are not necessarily private. Member variables are those that belong to objects. A local variable is declared inside of a function. Java: public class MyClass { int number1; // a is a member variable int number2; } MyClass newInstanceOfMyClass = new MyClass(); newInstanceOfMyClass.number1 = 7; newInstanceOfMyClass.number2 = 3; public void myFunction() { String coolString = "this is a string" // coolString is a local variable } * In Java, parentheses are required in method declarations, even if no arguments are passed to the method when it's invoked. * In Java, there is static type checking. * In Java, variables have explicitly declared types, whereas in Ruby they do not. * In Java, constructors are a bit different. In Ruby we have the `initialize` method. Ruby: def initialize(player) @player = player end Java: class AnotherClass { String someNewString = "Pat Shurmur is the worst coach in NFL history"; public AnotherClass() { someNewString += " and soon he'll be fired"; } // this is the constructor, much like initialize in Ruby } Another example in Java using member variables: class Elephants { int count; boolean big; Elephants() { count = 13; big = true; } * In Java, there is casting. Java: String anotherString = "94"; int stringToInteger = Integer.parseInt(anotherString); // and another example String beautifulString = "hello"; // creates an object of type string Object brandNewObject = beautifulString; // creates a variable of type object and points it to the string we just created above String myNewString = (String) brandNewObject; // casting converts the object to a variable of type string * In Java, it's `null`, instead of `nil` like in Ruby. This list is not all-inclusive, but we hit the major similarities and differences between Ruby and Java. After spending several days intently studying Java code, it begins to sink in and your mind makes those connections that I discussed in the second paragraph. I've slowly been relating the unfamiliar Java syntax to my prior knowledge of Ruby and using that to gain a better understanding of the language as a whole. It's a very useful tool. *Some Helpful Stuff* [To Ruby From Java](http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-java/) [Java Tutorial Hub](http://www.javatutorialhub.com/java-tutorial.html) [Difference between member variables and local variables](http://www.javacoffeebreak.com/faq/faq0040.html) [Static Typing](http://c2.com/cgi/wiki?StaticTyping) [Java vs Ruby: a quick and fair comparison](http://www.slideshare.net/Belighted/ruby-vs-java)