Java & C# Comparison


Program Structure
Java

package hello;

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

      // See if an argument was passed from the command line
      if (args.length == 1)
         name = args[0];

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


C#

using System;

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

         // See if an argument was passed from the command line
         if (args.Length == 1)
            name = args[0];

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




Comments
Java

// Single line
/* Multiple
    line  */
/** Javadoc documentation comments */



C#

// Single line
/* Multiple
    line  */
/// XML comments on a single line
/** XML comments on multiple lines */





Data Types
Primitive Types
boolean
byte
char
short, int, long
float, double

Reference Types
Object   (superclass of all other classes)
String
arrays, classes, interfaces

Conversions

// int to String 
int x = 123;
String y = Integer.toString(x);  // y is "123"

// String to int
y = "456";
x = Integer.parseInt(y);   // x is 456

// double to int
double z = 3.5;
x = (int) z;   // x is 3  (truncates decimal)

Value Types
bool
byte, sbyte
char
short, ushort, int, uint, long, ulong
float, double, decimal
structures, enumerations

Reference Types
object    (superclass of all other classes)
string
arrays, classes, interfaces, delegates

Conversions

// int to string 
int x = 123;
String y = x.ToString();  // y is "123"

// string to int
y = "456";
x = int.Parse(y);   // or x = Convert.ToInt32(y);

// double to int
double z = 3.5;
x = (int) z;   // x is 3  (truncates decimal)