Java and C# Comparison
原文:http://www.harding.edu/fmccown/java_csharp_comparison.html
|
|||||
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 |
Java and C# Comparison的更多相关文章
- android java.lang.IllegalArgumentException: Comparison method violates its general contract! 问题
android java.lang.IllegalArgumentException: Comparison method violates its general contract! 问题 jav ...
- Java 异常解决之java.lang.IllegalArgumentException: Comparison method violates its general contract!
Java 异常解决 在你的代码前加一句 System.setProperty("java.util.Arrays.useLegacyMergeSort", "true&q ...
- java.lang.IllegalArgumentException: Comparison method violates its general contract!
这个错误就是写比较器的时候少写了返回值的情况: 比如: Collections.sort(list, new Ordering<QtmSysUserListDto>() { @Overri ...
- 集合比较器报错java.lang.IllegalArgumentException: Comparison method violates its general contract!
Collections.sort(listMonthlyUsage, new Comparator<MonthlyUsageDto>() { //按照元素从小到大排序 @Override ...
- java-collections.sort异常Comparison method violates its general contract!
转载:http://www.tuicool.com/articles/MZreyuv 异常信息 java.lang.IllegalArgumentException: Comparison metho ...
- Writing a simple Lexer in PHP/C++/Java
catalog . Comparison of parser generators . Writing a simple lexer in PHP . phc . JLexPHP: A PHP Lex ...
- JDK7的Comparison method violates its general contract异常
1.摘要 前一阵遇到了一个使用Collections.sort()时报异常的问题,跟小伙伴@zhuidawugui 一起排查了一下,发现问题的原因是JDK7的排序实现改为了TimSort,之后我们又进 ...
- Java vs. C#
Java Program Structure C# package hello; public class HelloWorld { public static void main(String ...
- Comparison method violates its general contract
生产环境出现的错误排查,错误log如下 java.lang.IllegalArgumentException: Comparison method violates its general contr ...
随机推荐
- 【矩阵乘】【DP】【codevs 1305】Freda的道路
1305 Freda的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级: 大师 Master 题目描写叙述 Description Freda要到Rainbow的城堡去玩了. 我们能 ...
- How to Build a Search Page with Elasticsearch and .NET
Although SQL Server's Full-Text search is good for searching text that is within a database, there a ...
- 15款不容错过的前端开发Javascript和css类库 - 2017版本~
前端的JS和CSS类库日新月异, 在今天这篇介绍中,我们将精挑细选15款超棒的JS/CSS类库, 希望大家在开发过程中会觉得有帮助~ Core UI 基于Bootstrap4的一套UI类库, Core ...
- logistic回归具体解释(二):损失函数(cost function)具体解释
有监督学习 机器学习分为有监督学习,无监督学习,半监督学习.强化学习.对于逻辑回归来说,就是一种典型的有监督学习. 既然是有监督学习,训练集自然能够用例如以下方式表述: {(x1,y1),(x2,y2 ...
- Oracle导入excel数据快速方法
Oracle导入excel数据快速方法 使用PLSQL Developer工具,这个可是大名鼎鼎的Oracle DBA最常使用的工具. 在单个文件不大的情况下(少于100000行),并且目的 ...
- Android Context完全解析,你所不知道的Context的各种细节
Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但是这并不代表Context没有什么东西好讲的,实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学 ...
- 【树莓派】【转】树莓派3装Android 6.0,支持Wi-Fi和蓝牙
树莓派3装Android 6.0,支持Wi-Fi和蓝牙 相信对于许多树莓派初学者(包括我)来说,Android系统的确是一个不错的选择.但国内这方面资源稀缺,经本人FQ苦寻,找到了老外的树莓派Andr ...
- 连通性问题--Algorithms IN C读书笔记
近期在看<Algorithms IN C>这本书.刚開始看,读的是英文版的.感觉作者的叙述有点不太easy理解.就找了一本中文版的来看,发现还是看英文版的比較好.先看了第一章的大部分,后面 ...
- Python2.7.14安装和pip配置安装及虚拟环境搭建
目录 前言 1 Python2.7.14安装 2 pip配置安装 3 虚拟环境安装 前言 今天在搭建阿里云服务器,需要安装Python相关环境,之前在本机都已经安装过两遍,今天又来安装一遍,安装具 ...
- Atlas:ERROR 1105 (HY000): #07000Proxy Warning - IP Forbidden
1:遇到一个奇怪的问题 Atlas的管理接口正常 添加一个client之后save config mysql -uroot -p -P1234 -h127.0.0.1 报错了:ERROR 1105 ( ...