Common lines of code in different languages for quick reference and comparison: Java, Javascript, Objective-C, Python, Ruby, Scala, Swift.
- Intro
- Basic things to consider
- Variable declaration
- Function calls, property access, print things
- String manipulation
- More to do...
This document shows common code constructions in different languages (for now, those that I use more frequently). 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.
// Swift
// You usually need Foundation or UIKit
import FoundationBasic variable declaration for some common types (numbers, strings, booleans). Here you can also see name conventions (camelCase, snake_case) and line comments.
// Java
int minAge = 18;
final String name = "John Smith"; // Constant (final)
double height = 1.75;
boolean male = true;// Javascript
var minAge = 18;
var name = "John Smith"; // Also with ''
var height = 1.75;
var male = true;// Objective C
int minAge = 18;
NSString* name = @"John Smith";
double height = 1.75;
BOOL male = YES;# Python
min_age = 18
name = 'John Smith'
height = 1.75
male = True# Ruby
min_age = 18
name = 'John Smith' # With "" you can interpolate
height = 1.75
male = true// Scala
var minAge = 18
val name = "John Smith" // Constant (val)
var height : Double = 1.75 // Explicit type is optional
var male = true// Swift
var minAge = 18
let name = "John Smith" // Constant (let)
var height : Double = 1.75 // Explicit type is optional
var male = trueBasic 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); // Call function
System.out.println("Hello " + user.getName()); // Print property// Javascript
var user = database.find(email, pwd); // Call function
console.log("Hello " + user.name); // Print property// Objective C
User* user = [database find:email pass:pwd]; // Call function
NSLog(@"Hello %@ at %@", user.name, user.city); // Print property# Python
user = database.find(email, pwd) # Call function
print "Hello " + user.name # Print property# Ruby
user = database.find(email, pwd) # Call function
puts "Hello #{user.name}" # Print property// Scala
val user = database.find(email, pwd) // Call function
println("Hello " + user.name) // Print property// Swift
let result = object.someMethod(arg1, param2:arg2) // Call function
print("Hello " + name) // Print property// Java
int length = name.length(); // Length
String text = name + " likes " + food; // 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 + " likes " + food; // 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:
@"%@ likes %@", name, food]; // Interpolate
text = [text stringByAppendingString:@" and drinks"]; // 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 + " likes " + food # Concatenate
found = "Smith" in name # Find
found = name.find(" ") >= 0 # Index
first_name = name[0:index] # Substring
parts = text.split(" ") # Split# Ruby
length = name.length # length
text = "#{name} likes #{food}" # Interpolate
text += " and " + drink # Concatenate
found = name.include? "Smith" # Find
index = name.index(" ") # Index (truthy if found)
first_name = name[0...4] # Substring
parts = text.split(" ") # Split// Scala
var text = s"$name likes $food" // Interpolate
text += " and " + drink // Concatenate
var found = name.contains("Smith") // Find
found = name.indexOf(" ") >= 0 // Index
val firstName = name.substring(0, 4) // Substring
val parts = text.split(" ") // Split// Swift
int length = name.length // Length
var text = "\(name) likes \(food)" // Interpolate
text += " and " + drink // Concatenate
let range = string.rangeOfString("Smith") // Index
let found = range != nil // Find
// Try: http://stackoverflow.com/a/24046551/1121497
let parts = text.componentsSeparatedByString(" ") // Split// Java// Javascript// Objective C# Python# Ruby// Scala// Swift
crack!! 👍