http://www.harding.edu/fmccown/java1_5_csharp_comparison.html
아래 복사는 Creative Commons License.에 따라 적법하게 긁어온것임.
Java (J2SE 5.0) and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between Java and C#.
This is by no means a complete overview of either language. Hope you find this useful!
Also see VB.NET and C# Comparison.
| |||||
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 + "!"); } } |
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 + "!"); } } } | ||||
| |||||
// 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 | ||||
| |||||
// 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. | ||||
| |||||
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; | ||||
| |||||
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 | ||||
| |||||
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) { | ||||
| |||||
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 | ||||
| |||||
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][] { | ||||
|
|||||
// 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 | ||||
| |||||
// 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 | ||||
| |||||
// Must be in a method that is declared to throw this exception try { |
Exception up = new Exception("Something is really wrong.");
| ||||
| |||||
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. | ||||
| |||||
Accessibility keywords // Inheritance // Interface definition // Extending an interface // Interface implementation |
Accessibility keywords // Inheritance // Interface definition // Extending an interface // Interface implementation | ||||
| |||||
class SuperHero { public SuperHero() { public SuperHero(int powerLevel) { // No destructors, just override the finalize method |
class SuperHero { | ||||
| |||||
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(); | ||||
|
|||||
private int mSize; public int getSize() { return mSize; }
|
private int mSize; public int Size { shoe.Size++; | ||||
| |||||
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" | ||||
| |||||
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 | ||||
| |||||
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 |
Page last modified:
11/10/2007 07:31:19
Copyright © 2004 by Frank McCown
Please send any corrections or comments to fmccown@harding.edu.
Home
This work is licensed under a Creative Commons License.
'Projects > CoRapport' 카테고리의 다른 글
ASP.NET App-Domain 못만든다고 할때... (0) | 2009.06.11 |
---|---|
무료 XAML 편집기 (0) | 2009.05.28 |
http request 및 response 헤더 보여주는 툴 (0) | 2009.04.12 |
닷넷 euc-kr 형식처리 (0) | 2009.04.11 |
[HttpWebRequest] 닷넷 응용프로그램에서 쿠키로 인증된 세션을 사용 (0) | 2009.04.11 |