Comparativa de llenguatges

Vist a http://rosettacode.org/wiki/Category:Programming_Tasks


main
    Java:   public MyClass {  public static void main(String[] args) {  System.out.println("hola"); } }
    C#:     using System;  class MyClass { public static void Main() {  Console.WriteLine("hola"); } }
    JS:     function f() { print("hola"); }    f()
    Python: def f():   print("hola")    if __name__ == "__main__":   f()
    Kotlin: fun main(args: Array<String>) { println("hola"); } 
    Scala:  object MyObj extends App { def foo { println("hola"); }  foo }

Biblioteques
   Java:   package a.b.c;   import a.b.c;
   C#:     using a.b.c;     namespace a.b.c { ... }
   JS:     const pali = require('../lib/palindrome');
   Kotlin: 
   Python: import doctest
   Scala:  import org.scalacheck._


Strings
    Java:    String doubleQuotes = "\"\"";  
    C#:      string doubleQuotes = "\"\"";
    JS:      var doubleQuotes = "\"\"";  var doubleQuotes = '""';
    Python:  doubleQuotes = "\"\"";   doubleQuotes = '""';
    Kotlin:  val doubleQuotes = "\"\"";
    Scala:   val doubleQuotes = "\"\"";  

Strings multiline
    Java:     String multiline = " ... \n ...";  
    C#:       string doubleQuotes = @" ... ";
    JS:       var multiline = """ ... """;
    Python:   multiline = """ ... """;  multiline =''' ... '''
    Kotlin:   val multiline = """ ... """;
    Scala:    val multiline = """ ... """;

Boolean
    Java:     (a && b) || (!c ^ d)
    C#:       (a && b) || (!c ^ d)
    JS:       (a && b) || (!c)
    Python:   (a and b) or (not c)
    Kotlin:   (a and b) or (!c xor d)   però també  (a && b) || (!c ^d)
    Scala:    (a && b) || (!c)

Foreach
    Java:     for (String thing : things) { ... }
    C#:       foreach (string thing in things) { ... }
    JS:       for (var thing in things) { ... }
    Python:   for thing in things: 
    Kotlin:   for (thing : things) { ... }
    Scala:    for (thing <- things) { ... }

For
    Java:     for (int i=0; i< 4; i++) { ... }
    C#:       for (int i = 0; i < 5; i++) { ... }
    JS:       for (var i=0; i< 4; i++) { ... }    o  range(5).forEach( func )
    Python:   for i in range(1,6): 
    Kotlin:    for (i in 1..4) { ... }   o  (1..4).forEach { ... }
    Scala:    for (i <- 1 to 4) { ... }


While
    Tots:    while ( condicio ) { ... }
    Python:  while n > 0 :


Try/catch
    Java:  try { throw new Exception(); } catch (Exception e) { ... } finally { ... }
    C#:    try { throw new Exception(); } catch (Exception e) { ... } finally { ... }
    JS:    try { throw(new Exception()); } catch (e if e instanceof Exception) { ... } finally { ... }
    Python: try: ... raise Exception ... except Exception: ...
    Kotlin: try { throw Exception(); } catch (e : Exception) { ... } finally { ... }
    Scala:  try { new Exception } catch { case e: Exception => ... }


String compare
   Java:  str1.equals(str2)
   C#:    string.Compare( str1, str2 )
   JS:    str1 === str2
   Python: str1 == str2
   Kotlin: str1 == str2
   Scala:  str1 == str2
   

Object ref compare
   Java:  str1 == str2
   C#:    
   JS:
   Python:
   Kotlin: str1 === str2
   Scala:  str1 eq str2

Arrays
    Java:   String[][] funny_matrix = new string[][]{ {"clowns", "are"} , {"not", "funny"} };
    C#:     string[,] funny_matrix = new string[2,2]{ {"clowns", "are"} , {"not", "funny"} };
    JS      var  matrix =  [["clowns", "are"] , ["not", "funny"]]
    Kotlin: var  matrix = arrayOf( 1,2,3,4 );
    Python: matrix =  [["clowns", "are"] , ["not", "funny"]]
    Scala:  val b = Array("foo", "bar", "baz")    

Lists
   Java:
   C#:     List<int> list = new List<int>();  list.Add(10);   print(list[0]) ;
   JS:       
   Kotlin:
   Python: list = []   list.append(10)     list[0]



Function
   Java:  static double mult( double x, double y ) { return x*y; }
   C#:    static double mult( double x, double y ) { return x*y; }
   JS:    function mult(a, b) { return a*b; }
   Kotlin: fun mult(a: Int, b: Int): Int { return a * b }
   Python: def multiply(a, b): return a * b 
   Scala:  def multiply(a: Int, b: Int) = a * b

Lambda Function
   Java:  
   C#:    Func<double, double, double> multiply = ((a,b) => a*b);
   JS:    var multiply = (a, b) => { return a * b };
   Kotlin:
   Python: multiply = lambda a, b: a * b
   Scala:
 
Paràmetres opcionals
   Java:   addWidget(AddWidgetParams.Builder("root").x(100).y(35).build());
   C#:     void AddWidget(string parent, float x = 0, float y = 0, string text = "Default"); 
             AddWidget("root", 320, 240, "First"); 
             AddWidget("root", text: "Footer", y: 400);
   JS:     function addWidget(parent, options) { opts = {}; opts.x = options.x || 0;  opts.y = options.y || 1; opts.text = options.text || 'pork chops'}
             addWidget("root", {text: "lamb kebab", x: 3.14});
   Kotlin: fun someFunction(first: String, second: Int = 2, third: Double);
             someFunction("positional", 1, 2.0);  
             someFunction(first = "named", second = 1, third = 2.0)
   Python: def show_args(parent, text = 'default value', *posparam, **keyparam): ...
             show_args('POSITIONAL', 'OPTIONAL')
             show_args(text='OPTIONAL', parent='KEYWORD')
             show_args('POSITIONAL', 'OPTIOMAL', 'EXTRA1', 'EXTRA2', 'EXTRA3',  kwa1='EXTRA', kwa2='KEYWORD', kwa3='ARGUMENTS')
   Scala:  def add(x: Int, y: Int = 1) = x + y
             add(5)
             add(y=10, x=4)


Function in/out parameters
    C#:    void Foo(ref int Value) { Value += 1 }  ;  int p=1;  Foo(ref p);




Classes

Java:
------------

  public class Being implements Serializable {
    public boolean isAlive() { return alive; } 
    public void setAlive(boolean alive) { alive= alive; } 
    private boolean alive;
  }
 
  public class Animal extends Being implements Serializable {
    public Animal() { }
 
    public Animal(long id, String name, boolean alive ) {
      Id = id;
      Name = name;
      Alive = alive;
    }
 
    public long getId() { retrun id; }
    public void setId(long id) { id= id; }

    public String getName() { return name; }
    public void setName(String name) { name=name; }

    long id;
    String name;
 
    public void print() { System.out.println( String.format("{0}, id={1} is {2}",
      Name, Id, Alive ? "alive" : "dead")); }
  }

  Animal animal= new Anima( 1, "starky", true );







C#:
--------------


namespace Object_serialization
{
  [Serializable] public class Being
  {
    public bool Alive { get; set; }
  }
 
  [Serializable] public class Animal: Being
  {
    public Animal() { }
 
    public Animal(long id, string name, bool alive = true)
    {
      Id = id;
      Name = name;
      Alive = alive;
    }
 
    public long Id { get; set; }
    public string Name { get; set; }
 
    public void Print() { Console.WriteLine("{0}, id={1} is {2}",
      Name, Id, Alive ? "alive" : "dead"); }
  }
} 
 

Animal animal= new Animal( 1, "starky" )




Python:
------------------

class Entity:
	def __init__(self):
		self.name = "Entity"
	def printName(self):
		print self.name
 
class Person(Entity): #OldMan inherits from Entity
	def __init__(self): #override constructor
		self.name = "Cletus" 


instance1 = Person()
 
instance2 = Entity()



Leave a comment

Your comment