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 ...
随机推荐
- StackPanel
StackPanel 的 HorizontalAlignment 属性和 VerticalAlignment 属性 默认情况下,这两个属性都被设置为 Stretch.
- Creating fields using CSOM
When creating a field, whether you are using CAML, server-side object mode, or one of the client-s ...
- XE6入门(二)项目中的文件
XE6中项目文件为DPR,查看方法请参考一下以前写过的博文: "Delphi项目构成之项目文件DPR" 项目文件DPR 通过主菜单[Project | View Source],就 ...
- (算法)从0到n整数中数字2出现的次数
题目: 数出0到n(含)中数字2出现了几次. 思路: 1.暴力方法,数出每个数字包含几个2,然后累加起来. 2.分析:分别考虑数字n每一位出现2的次数,如123123: 从左往右考虑4123123: ...
- centos7默认安装没有连接到网络
1.显示所有连接 # nmcli con show 2.连接到网络 # nmcli con up enp0s3 这个ens33是通过显示所有连接查到的
- LintCode: Unique Characters
C++, time: O(n^2) space: O(0) class Solution { public: /** * @param str: a string * @return: a boole ...
- 微信小程序 - 上传图片组件
2019-01-08 更新至1.1:修复了一些问题 2019-03-14 全面更新,推荐:https://www.cnblogs.com/cisum/p/10533559.html 使用了es8的as ...
- tomcat7配置
<?xml version='1.0' encoding='utf-8'?><!-- Licensed to the Apache Software Foundation (ASF) ...
- ios网络学习------9 播放网络视频
IOS提供了叫做MPMoviePlayerController MPMoviePlayerViewController两个类.能够轻松用来实现视频播放. MPMoviePlayerViewContr ...
- win10 教育版本变专业版本
输入win10升级产品密匙:VK7JG-NPHTM-C97JM-9MPGT-3V66T.系统自动验证后依次点击下一步.始按钮kms激活