User:Sae1962/sandbox

From Wikipedia, the free encyclopedia
C# Java
using System; 

namespace Hello {
   public class HelloWorld {
      public static void Main(string[] args) {
         string name = "C#";

      // Use argument as name, if it exists
         if (args.Length == 1) name = args[0];

         Console.WriteLine("Hello, " + name + '!');
      }
   }
}
package hello;

public class HelloWorld {
   public static void main(String[] args) {
      final String NAME = "Java";

      // Use argument as name, if it exists
      if (args.length == 1) NAME = args[0];

      System.out.println("Hello, " + NAME + '!');
    }
}

In C#, const makes a value constant, but differs somehow from final in Java.

Als ein etwas größeres Beispiel möchte ich noch die Rundungsfunktionalität in beiden Sprachen implementieren. Man wird sich denken, dass dies nur ein Spiel ist, weil ja alle Sprachen Bibliotheken für diesen Zweck haben - in C# ist es die Math.Round-Methode, in Java die round-Methode in java.lang.Math.

Hier das Beispiel programmiert in C# und hier dasselbe Beispiel in Java.

/**
 * Test interface
 */
public interface Interfaceable<E> {
    /**
     * Determine, if there is a next element
     * @return true, if there is an element, false otherwise
     */
    boolean hasNext();

    /**
     * Return the next element
     * @return
     */
    E        next();

    /**
     * Removes current element
     */
    void    remove();
}