c# 与 java 语法异同
Java and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between Java and C#.
This is not a complete overview of either language. Hope you find this useful!
Also see VB.NET and C# Comparison.
|
|||||
package hello;
public class HelloWorld { // See if an argument was passed from the command line System.out.println("Hello, " + name + "!"); |
using System;
namespace Hello { // See if an argument was passed from the command line 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 Reference 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; Status s = Status.Pass; |
enum Action {Start, Stop, Rewind, Forward}; enum Status {Flunk = 50, Pass = 70, Excel = 90}; No equivalent. Action a = Action.Stop; 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) { string color = "red"; |
||||
|
|||||
while (i < 10) for (i = 2; i <= 10; i += 2) do for (int i : numArray) // foreach construct // for loop can be used to iterate through any Collection for (Object o : list) |
while (i < 10) for (i = 2; i <= 10; i += 2) do foreach (int i in numArray) // foreach can be used to iterate through any collection foreach (Object o in list) |
||||
|
|||||
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]; float twoD[][] = new float[rows][cols]; 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]; float[,] twoD = new float[rows, cols]; 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 single class // Import all classes |
namespace Harding.Compsci.Graphics { or namespace Harding { // Import single class // Import all 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 { public SuperHero() { public SuperHero(int powerLevel) { ~SuperHero() { |
||||
|
|||||
SuperHero hero = new SuperHero(); hero.setName("SpamMan"); hero.Defend("Laura Jones"); SuperHero hero2 = hero; // Both refer to same object hero = null; // Free the object if (hero == null) Object obj = new SuperHero(); |
SuperHero hero = new SuperHero(); hero.Name = "SpamMan"; 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; } int s = shoe.getSize(); |
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) { StudentRecord stu = new StudentRecord("Bob", 3.5f); stu2.name = "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."); int c = System.in.read(); // Read single char // 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 writing // Binary stream reading |
Page last modified: 02/01/2011 06:45:24
This work is licensed under a Creative Commons License.
Please send any corrections or comments to fmccown@harding.edu.
c# 与 java 语法异同的更多相关文章
- Java语法
java语法: 一个java程序可以说是一系列对象的集合,而这些对象都要通过调用彼此的方法来协同工作. 对象: 对象是一个实例,例如:一只猫,它是一个对象,有状态和行为.它的状态状态有:颜色,名字,品 ...
- Java语法糖1:可变长度参数以及foreach循环原理
语法糖 接下来几篇文章要开启一个Java语法糖系列,所以首先讲讲什么是语法糖.语法糖是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的 ...
- 程序员带你学习安卓开发,十天快速入-对比C#学习java语法
关注今日头条-做全栈攻城狮,学代码也要读书,爱全栈,更爱生活.提供程序员技术及生活指导干货. 如果你真想学习,请评论学过的每篇文章,记录学习的痕迹. 请把所有教程文章中所提及的代码,最少敲写三遍,达到 ...
- Java语法基础(1)
Java语法基础(1) 1. Java是一门跨平台(也就是跨操作系统)语言,其跨平台的本质是借助java虚拟机 (也就是JVM(java virtual mechinal))进行跨平台使用. ...
- C++、Java语法差异对照表
C++.Java语法差异对照表 C++ and Java Syntax Differences Cheat Sheet First, two big things--the main function ...
- jsp页面执行java语法,获取的值在页面调用
首先在页面头引用用到类的包路径 写需要执行的java语法 页面转换引用 <!-- 引用包路径 --> <%@ page language="java" impor ...
- Java语法之反射
一.反射机制 在前面Java语法之注解自定义注解时我们也有提到反射,要获取类方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation对象.那什么是反射呢?JAVA反射机制是在运行状 ...
- Java语法知识总结
一:java概述: 1991 年Sun公司的James Gosling等人开始开发名称为 Oak 的语言,希望用于控制嵌入在有线电视交换盒.PDA等的微处理器: 1994年将Oak语言更名为Java: ...
- Java语法糖设计
语法糖 Java语法糖系列,所以首先讲讲什么是语法糖.语法糖是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的字节码或者特定的方式对这 ...
随机推荐
- c#重写和重载的区别?重写和重载的意义?
重写: 要求方法名.参数合返回值相同: 意义:重写是为了增强类的重用性和复用性,扩展性:重写是对类中方法的扩充,因为继承用的是父类的东西,重写则不仅得到父类的东西,同时也加入了自己的东西. 方法重写的 ...
- 关于 lerp();
value lerp(value s, value a, value b ); 该函数返回的值为:a + s * (b - a) ,是一个处于 [a, b] 之间的值. 当s=0, 该函数返回a :当 ...
- DevExpress v17.2新版亮点—ASP.NET篇(二)
用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET v17.2 的GridView Control. ...
- Java数字签名算法--RSA
签名具有的特性: 安全性 抗否认性 数字签名:带有密钥(公钥.私钥)的消息摘要算法(使用私钥进行签名,使用公钥进行验证) 数字签名算法:RSA.DSA.ECDSA 数字签名特性: 验证数据完整性 认证 ...
- MyEclipse2017CI破解教程
因为工作中需要有多个MyEclipse去管理不同的项目组的工作,恰逢MyEclipse2017CI发布,下载破解尝鲜,因为之前安装了MyEclipse2016CI7和MyEclipse2014GA,两 ...
- git中的分支管理
/*游戏或者运动才能让我短暂的忘记心痛,现如今感觉学习比游戏和运动还重要——曾少锋*/ 如果对git基础不太熟悉的可以参考:http://www.cnblogs.com/zengsf/p/750621 ...
- HDU 2072 单词数 详细解答
题目 单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HTTP、TCP、UDP以及SOCKET
HTTP.TCP.UDP以及SOCKET 一.TCP/IP代表传输控制协议/网际协议,指的是一系列协组. 可分为四个层次:数据链路层.网络层.传输层和应用层. 在网络层:有IP协议.ICMP协议.AR ...
- hdu1233 还是畅通工程 最小生成树
给出修建边的边权,求连通所有点的最小花费 最小生成树裸题 #include<stdio.h> #include<string.h> #include<algorithm& ...
- hdu1355
题意:有一片矩形花生田在路的一侧,田上的整数坐标位置有0个或多个花生,现规定从路上走到田地最边上的某个格点位置.从田边上走回路上.从一个格点移动到另一个格点.采摘格点上的花生,这四种动作都要花费一单位 ...