출처 : http://www.harding.edu/USER/fmccown/WWW/java1_5_csharp_comparison.html
Java (J2SE 5.0) and C# Comparison
Java |
C# | ||||
Comments | |||||
// Single line /* Multiple line */ /** Javadoc documentation comments */ |
// Single line /* Multiple line */ /// XML comments on a single line /** XML comments on multiple lines */ | ||||
Data Types | |||||
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 | ||||
Constants | |||||
// May be initialized in a constructor final double PI = 3.14; |
const double PI = 3.14;
// Can be set to a const or a variable. May be initialized in a constructor. | ||||
Enumerations | |||||
enum Action {Start, Stop, Rewind, Forward}; // Special type of class Action a = Action.Stop; |
enum Action {Start, Stop, Rewind, Forward}; enum Status {Flunk = 50, Pass = 70, Excel = 90}; No equivalent. Status s = Status.Pass; | ||||
Operators | |||||
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, no & and | equivalents String Concatenation | ||||
Choices | |||||
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) { | ||||
Loops | |||||
while (i < 10) do for (int i : numArray) // foreach construct // for loop can be used to iterate through any Collection |
while (i < 10) do foreach (int i in numArray) // foreach can be used to iterate through any collection | ||||
Arrays | |||||
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][] { | ||||
Functions | |||||
// Primitive types and references are always passed by value class Point { Point p = new Point(); // Accept variable number of arguments int total = Sum(4, 3, 2, 1); // returns 10 |
// 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 | ||||
Strings | |||||
// String concatenation // String comparison System.out.println(mascot.substring(2, 5)); // Prints "son" // My birthday: Oct 12, 1973 // Mutable string |
// String concatenation // String comparison Console.WriteLine(mascot.Substring(2, 3)); // Prints "son" // My birthday: Oct 12, 1973 // Mutable string | ||||
Exception Handling | |||||
// Must be in a method that is declared to throw this exception try { |
Exception up = new Exception("Something is really wrong.");
| ||||
Namespaces | |||||
package harding.compsci.graphics;
import harding.compsci.graphics.*; // Import all classes |
namespace Harding.Compsci.Graphics { or namespace Harding { // Import all class. Can't import single class. | ||||
Classes / Interfaces | |||||
Accessibility keywords // Inheritance // Interface definition // Extending an interface // Interface implementation |
Accessibility keywords // Inheritance // Interface definition // Extending an interface // Interface implementation | ||||
Constructors / Destructors | |||||
class SuperHero { public SuperHero() { public SuperHero(int powerLevel) { // No destructors, just override the finalize method |
class SuperHero { | ||||
Objects | |||||
SuperHero hero = new SuperHero(); hero.setName("SpamMan"); SuperHero hero2 = hero; // Both refer to same object if (hero == null) Object obj = new SuperHero(); |
SuperHero hero = new SuperHero(); hero.Defend("Laura Jones"); SuperHero hero2 = hero; // Both refer to same object hero = null ; // Free the object if (hero == null) Object obj = new SuperHero(); | ||||
Properties | |||||
private int mSize; public int getSize() { return mSize; }
|
private int mSize; public int Size { shoe.Size++; | ||||
Structs | |||||
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" | ||||
Console I/O | |||||
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.");
// The studio costs $499.00 for 3 months. // Today is 06/25/04 |
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 // The studio costs $499.00 for 3 months. // Today is 06/25/2004 | ||||
File I/O | |||||
import java.io.*; // Character stream writing // Character stream reading // Binary stream writing // Binary stream reading |
using System.IO; // Character stream writing // Character stream reading
// Binary stream reading |
'bring | steal' 카테고리의 다른 글
아이폰용 드래곤 플라이트(Dragon Flight) 세이브 파일 에디트 (돈 치트) p 1 (0) | 2012.10.30 |
---|---|
소프트웨어 만들 때 쓰는 시간.. (4) | 2012.04.20 |
JOB INTERVIEW QUESTIONNAIRE (0) | 2012.04.17 |
[펌] C# I/O와 네트워킹 by Raffi Krikorian (0) | 2009.03.16 |
[펌] Free unix timestamp and epoch conversion tool (0) | 2009.03.16 |