Skip to content

Instantly share code, notes, and snippets.

@fmaylinch
Last active September 15, 2025 00:43
Show Gist options
  • Save fmaylinch/4a708abcdb42bf2330e5 to your computer and use it in GitHub Desktop.
Save fmaylinch/4a708abcdb42bf2330e5 to your computer and use it in GitHub Desktop.
Common lines of code in different languages for quick reference and comparison

Code cheat sheet

Common lines of code in different languages for quick reference and comparison: Java, JavaScript, Objective-C, Python, Ruby, Scala, Swift.

Contents

Intro

This document shows common code constructions in some popular languages. One purpose is to serve as a quick reference when you need to switch languages and you don't know or remember basic things. Another use is to see languages side by side, to compare their differences and similarities.

If you see something that could be improved, added or fixed please let me know. I will gladly consider your suggestions.

Back to top

Install and minimum program

Instructions to install language interpreter and write a minimal runnable program.

To edit files you may use editors like Atom or Sublime Text.

Java

  • Install Java JDK (latest version: 8).
  • Create HelloWorld.java file with the content below.
  • From the command line, compile the file with javac HelloWorld.java (a file called HelloWorld.class will be created.
  • Execute the compiled file with java HelloWorld.
public class HelloWorld {
  public static void main(String... args) {
    System.out.println("Hello world!");
  }
}

JavaScript

You may execute JavaScript from the console of your browser. Find out how to open the console. In Chrome it's Alt+Cmd+J.

You can include JavaScript files from an HTML file. When the HTML is loaded by the browser, the JavaScript files will be executed.

You can also execute scripts from the command line. To do that:

  • Install node.js.
  • Create hello_world.js file with the content below.
  • Execute the script with node hello_world.js.
console.log("Hello world!");

Objective-C

Read about how to use Xcode to create iOS apps. Consider also using Swift instead of Objective-C, since it's a clearer and more modern language.

Python

  • Download and install Python (version 2 is recommended).
  • Create a file hello_world.py with content below.
  • Execute the script with python hello_world.py.
print "Hello world!"

Ruby

  • Download and install Ruby.
  • Create a file hello_world.rb with content below.
  • Execute the script with ruby hello_world.rb.
puts "Hello world!"

Scala

  • Download and install Scala.
  • Create a file HelloWorld.scala with content below.
  • Execute the script with scala HelloWorld.scala.
println("Hello world!")

Swift

Read about how to use Xcode to create iOS apps. If you installed Xcode, you may also run Swift code from the command line:

  • Create a file HelloWorld.swift with content below.
  • Execute the script with swift HelloWorld.swift.
// You usually need to import Foundation
// Or import UIKit if you want to use view classes
import Foundation
print("Hello world!")

Back to top

Variable declaration

Basic variable for some common types (numbers, strings, booleans). Here you can also see name conventions (camelCase, snake_case) and line comments.

Java

int age = 18;
final String name = "John Smith"; // Constant (final)
double height = 1.75;
boolean hasChildren = false;

JavaScript

var age = 18;
var name = "John Smith";  // Also with ''
var height = 1.75;
var hasChildren = false;

Objective-C

int age = 18;
NSString* name = @"John Smith";
double height = 1.75;
BOOL hasChildren = NO; // also `false`

Python

age = 18
name = "John Smith"    # Also with ''
height = 1.75
has_children = False

Ruby

age = 18
name = 'John Smith'  # With "" you can interpolate
height = 1.75
has_children = false

Scala

var age = 18
val name = "John Smith"     // Constant (val)
var height : Double = 1.75  // Explicit type is optional
var hasChildren = false

Swift

var age = 18
let name = "John Smith"     // Constant (let)
var height : Double = 1.75  // Explicit type is optional
var hasChildren = false

Back to top

Function calls, property access, print things

Basic usage of functions, properties, and how to print messages on screen/console. I added this section as an introduction for the following ones.

Java

User user = database.find(email, pwd);
System.out.println("Hello " + user.getName());

JavaScript

var user = database.find(email, pwd);
console.log("Hello " + user.name);

Objective-C

User* user = [database find:email pass:pwd]; // Call syntax is quite Weird
NSLog(@"Hello %@", user.name);  // Sometimes you use this C-style syntax

Python

user = database.find(email, pwd)
print "Hello " + user.name

Ruby

user = database.find(email, pwd)
puts "Hello #{user.name}"

Scala

val user = database.find(email, pwd)
println("Hello " + user.name)

Swift

// From 2nd parameter you write `parameter:argument`
let user = database.find(email, pass:pwd)
print("Hello \(user.name)")

Back to top

String manipulation

Java

int length = name.length();             // Length
String text = name + " is " + age;      // Concatenate
boolean b = name.contains("Smith");     // Find
b = name.indexOf("Smith") >= 0;         // Index
String first = name.substring(0, 4);    // Substring
String[] parts = text.split(" ");       // Split

JavaScript

var length = name.length();          // Length
var text = name + " is " + age;      // Concatenate
var b = name.indexOf("Smith") >= 0;  // Find/Index
var first = name.substring(0, 4);    // Substring
var parts = text.split(" ");         // Split

Objective-C

int lenght = [name length];                                     // Length
NSString* text = [NSString stringWithFormat:
  @"%@ is %@", name, @(age)];                                   // Interpolate
text = [text stringByAppendingString:@" years old"];            // Concatenate
BOOL b = [name rangeOfString:@"Smith"].location != NSNotFound;  // Find/Index
NSString* first = [name substringToIndex:4];                    // Substring
NSString* last = [name substringWithRange:NSMakeRange(5, 10)];  // Substring
NSArray<NSString*>* parts = [text componentsSeparatedByString:@" "]; // Split

Python

length = len(name)                # length
text = name + " is " + str(age)   # Concatenate
found = "Smith" in name           # Find
found = name.find(" ") >= 0       # Index
first_name = name[0:4]            # Substring
parts = text.split(" ")           # Split

Ruby

length = name.length                # length
text = "#{name} is #{age}"          # Interpolate
text += ", height: " + height.to_s  # Concatenate
found = name.include? "Smith"       # Find
index = name.index(" ")             # Index (truthy if found)
first_name = name[0...4]            # Substring
parts = text.split(" ")             # Split
text = parts.join(" ")              # Join

Scala

var text = s"$name is $age"            // Interpolate
text += ", height: " + height          // Concatenate
var found = name.contains("Smith")     // Find
found = name.indexOf(" ") >= 0         // Index
val firstName = name.substring(0, 4)   // Substring
val parts = text.split(" ")            // Split
text = parts.mkString(" ")             // Join

Swift

var length = name.characters.count                    // Length
var text = "\(name) is \(age)"                        // Interpolate
text = String(format: "%@ is %@", name, String(age))  // Interpolate
text += ", height: " + String(height)                 // Concatenate
let range = string.rangeOfString("Smith")             // Index
let found = range != nil                              // Find
// About substrings
//   http://stackoverflow.com/questions/24029163
//   http://stackoverflow.com/a/24046551/1121497
let parts = text.componentsSeparatedByString(" ")     // Split

Back to top

More...

To do:

  • Define functions
  • Define classes and create objects
  • Arrays/lists
  • Maps/dictionaries
  • Pass functions as arguments

Java

JavaScript

Objective-C

Python

Ruby

Scala

Swift

Back to top

@gitToIgna
Copy link

crack!! 👍

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