Skip to content

Instantly share code, notes, and snippets.

@nilbus
Last active January 25, 2023 18:49
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nilbus/8668708 to your computer and use it in GitHub Desktop.
Save nilbus/8668708 to your computer and use it in GitHub Desktop.
Some Java basics for Rubyists
  1. Java uses static, declared typing:

    String hello = "Hello, World!";
    List<String> phrases = new ArrayList<String>;
    phrases.add(hello);
    phrases.add(null);
    phrases.add(123); // Compile error! Not a string.
  2. Java is compiled down to bytecode, which the JVM runs. Java can be compiled on the fly for code reloading; in practice this happens only in JSPs (views) and in your IDE editor. Compiling gives the benefits of execution speed and error (mostly syntax and type) checking.

  3. Like Javascript, new Class() instead of Class.new

  4. Constructors are named the name of the class instead of initialize:

    public class User {
        public User(String name) {
            // Create a user with name
        }
    }
  5. Instead of optional arguments, overloaded methods with the same name and different arguments

    public class User {
        public User(HashMap<String, String> attributes) {
            // Create a user with the given attributes
        }
    
        public User(String name) {
            // Delegate to the other constructor
            HashMap<String, String> attributes = new HashMap<String, String>;
            attributes.put("name", name);
            User(attributes);
        }
    }
  6. static generally makes something a class class method/variable

    public class User {
        public static User find(int userId) {
            // Find and return a user with the matching userId, or null.
        }
    }
  7. Loops instead of blocks to iterate

    // user.each
    for (user in users) {
       ...
    }
    
    // user.each_with_index
    for (int i = 0; i < users.length; i++) {
       User user = users[i];
       ...
    }
  8. Command methods that do not return a value return a void type

    public void sayHello(String name) {
        // There's no string interpolation
        System.out.println("Hello, " + name);
    }
  9. Instance variables - Declared at the class level, usually private w/ getters and setters, and no special @ character in the name.

    public class Foo {
        private String instanceVar;
    
        public void setInstanceVar(String instanceVar) {
            this.instanceVar = instanceVar;
        }
    
        public String getInstanceVar() {
            // `this.` is optional when it's not ambiguous, like it was above.
            return instanceVar;
        }
    }
  10. Like namespacing, classes are organized into packages

    // src/com/nilbus/example/Greeting.java
    // Declare that the following class will be in this package;
    package com.nilbus.example;
    public class Greeting {
        ...
    }
  11. No require; everything is required (loaded). Instead import classes you want to reference that are not in the current package.

    // src/com/nilbus/example/Robot.java
    package com.nilbus.example;
    import gov.nasa.robotics.Movements;
    public class Robot {
        public Robot() {
            // Can reference Greeting in the same package.
            new Greeting("beep");
            // Can reference Movements (as opposed to the full gov.nasa.robotics.Movements) because of the import above.
            new Movements(this).wave()
        }
    }
  12. Instead of duck typing, Java uses interfaces to define what common messages a set of classes should respond to.

    // An interface can have no code.
    // Only declare what methods and arguments must be implemented.
    public interface Duck {
        public void quack();
    }
    
    public class RealDuck implements Duck {
        public void quack() {
            // Quack like a duck
        }
    }
    
    public class RoboDuck implements Duck {
        public void quack() {
            // Do the robo-boogie and quack
        }
    }
    
    public class Pond {
        // Don't care what kind of Duck, as long as it implements the Duck interface
        public Pond(List<Duck> ducks) {
            for (duck in ducks) {
                duck.quack();
            }
        }
    }
  13. Method visibility basics:

    • public: Same as Ruby
    • protected: Can be called by any class in the same package and by subclasses
    • private: Can be called by other instances of the same class
@jasmo2
Copy link

jasmo2 commented Sep 26, 2015

Thanks for this awesome documentation. Is much easier than goes through books or tutorials.

@dbwest
Copy link

dbwest commented Dec 20, 2015

This is awesome. How about equivalents of #map/#collect?

@sananab
Copy link

sananab commented Feb 16, 2018

You have a knack for explaining things. Thanks for the guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment