-
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.
-
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.
-
Like Javascript,
new Class()
instead ofClass.new
-
Constructors are named the name of the class instead of initialize:
public class User { public User(String name) { // Create a user with name } }
-
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); } }
-
static
generally makes something a class class method/variablepublic class User { public static User find(int userId) { // Find and return a user with the matching userId, or null. } }
-
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]; ... }
-
Command methods that do not return a value return a
void
typepublic void sayHello(String name) { // There's no string interpolation System.out.println("Hello, " + name); }
-
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; } }
-
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 { ... }
-
No
require
; everything is required (loaded). Insteadimport
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() } }
-
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(); } } }
-
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
Last active
January 25, 2023 18:49
-
-
Save nilbus/8668708 to your computer and use it in GitHub Desktop.
Some Java basics for Rubyists
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have a knack for explaining things. Thanks for the guide.