分类:
2009-12-17 10:27:49
Java and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between Java and C#. Hope you find this useful!
Also see .
| |||||
// Single line /* Multiple line */ /** Javadoc documentation comments */ |
// Single line /* Multiple line */ /// XML comments on a single line /** XML comments on multiple lines */ | ||||
| |||||
Primitive Types
Conversions // int to String // String to int // double to int |
Value Types Reference Types Convertions // int to string // string to int // double to int | ||||
| |||||
final double PI = 3.14; | const double PI = 3.14; | ||||
| |||||
/* Enumerations coming in 1.5. For now use a class to do almost the same thing. */ public class Action { public static final int START = 0; public static final int STOP = 1; public static final int REWIND = 2; public static final int FORWARD = 3; } int action = Action.STOP; System.out.println(action); // Prints "1" |
enum Action {Start, Stop, Rewind, Forward}; enum Status {Flunk = 50, Pass = 70, Excel = 90}; Action a = Action.Stop; if (a != Action.Start) Console.WriteLine((int) a); // Prints "1" Console.WriteLine(Status.Pass); // Prints "Pass" | ||||
| |||||
Comparison Arithmetic Assignment Bitwise Logical Note: && and || perform short-circuit logical evaluations String Concatenation |
Comparison Arithmetic Assignment Bitwise Logical Note: && and || perform short-circuit logical evaluations String Concatenation | ||||
| |||||
java.io.DataInput in = new java.io.DataInputStream(System.in); System.out.print("What is your name? "); String name = in.readLine(); System.out.print("How old are you? "); int age = Integer.parseInt(in.readLine()); System.out.println(name + " is " + age + " years old.");
|
Console.Write("What's your name? "); string name = Console.ReadLine(); Console.Write("How old are you? "); int age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("{0} is {1} years old.", name, age); // or Console.WriteLine(name + " is " + age + " years old."); int c = Console.Read(); // Read single char | ||||
| |||||
// Primitive types and references are always passed by value class Point { Point p = new Point();
|
// Pass by value (default), in/out-reference (ref), and out-reference (out) class Point { Point p1 = new Point(); // Accept variable number of arguments int total = Sum(4, 3, 2, 1); // returns 10 | ||||
| |||||
int nums[] = {1, 2, 3}; or int[] nums = {1, 2, 3}; for (int i = 0; i < nums.length; i++) System.out.println(nums[i]); String names[] = new String[5]; names[0] = "David"; float twoD[][] = new float[rows][cols]; twoD[2][0] = 4.5; int[][] jagged = new int[5][]; |
int[] nums = {1, 2, 3}; for (int i = 0; i < nums.Length; i++) Console.WriteLine(nums[i]); string[] names = new string[5]; names[0] = "David"; float[,] twoD = new float[rows, cols]; twoD[2,0] = 4.5f; int[][] jagged = new int[3][] { | ||||
| |||||
while (i < 10) do // foreach coming in Java 1.5; for now simulate using a Vector |
while (i < 10) do // foreach can be used to iterate through any collection | ||||
| |||||
greeting = age < 20 ? "What's up?" : "Hello"; if (x < y) if (x != 100) { int selection = 2; |
greeting = age < 20 ? "What's up?" : "Hello"; if (x < y) if (x != 100) { | ||||
| |||||
// String concatenation // String comparison System.out.println(mascot.substring(2, 5)); // Prints "son" // Mutable string |
// String concatenation // String comparison Console.WriteLine(mascot.Substring(2, 3)); // Prints "son" // Mutable string | ||||
| |||||
// Must be in a method that is declared to throw this exception try { |
Exception up = new Exception("Something is really wrong.");
| ||||
| |||||
// Inheritance // Interface definition // Extending an interface // Interface implementation |
// Inheritance // Interface definition // Extending an interface // Interface implementation | ||||
| |||||
package harding.compsci.graphics;
import harding.compsci.graphics.*; // Import all classes |
namespace Harding.Compsci.Graphics { or namespace Harding { using Harding.Compsci.Graphics; // Import all classes | ||||
| |||||
class SuperHero { public SuperHero() { public SuperHero(int powerLevel) { // No destructors, just override the finalize method |
class SuperHero { | ||||
| |||||
No structs in Java. |
struct StudentRecord { public string name; public float gpa; public StudentRecord(string name, float gpa) { this.name = name; this.gpa = gpa; } } StudentRecord stu = new StudentRecord("Bob", 3.5f); StudentRecord stu2 = stu; stu2.name = "Sue"; Console.WriteLine(stu.name); // Prints "Bob" Console.WriteLine(stu2.name); // Prints "Sue" | ||||
| |||||
private int mSize; public int getSize () { return mSize; }
|
private int mSize; public int Size { shoe.Size++; |
Page last modified:
02/03/2006 07:18:29
Copyright © 2004-2006 by Frank McCown
Permission to make photocopies or reproductions of this web page in its entirety is granted for personal or non-commercial uses as long as no money is charged.
Please send any comments or corrections to .