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 ...
随机推荐
- Spark:java api实现word count统计
方案一:使用reduceByKey 数据word.txt 张三 李四 王五 李四 王五 李四 王五 李四 王五 王五 李四 李四 李四 李四 李四 代码: import org.apache.spar ...
- 面试题04_替换空格_剑指Offer系列
题目描写叙述 请实现一个函数,将一个字符串中的空格替换成"%20". 比如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路 ...
- Direct2D教程VII——变换几何(TransformedGeometry)对象
目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...
- 微信、支付宝App支付-JPay0.0.2发布
JPay 对微信App支付.支付宝App支付的二次封装,对外提供一个相对简单的接口以及支付结果的回调 GitHub:https://github.com/Javen205/JPay OsChina:h ...
- java enum 用法
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or th ...
- C#.NET常见问题(FAQ)-如何让TabControl可以动态增加或删除
动态插入可以使用TabPages.Insert方法 动态删除可以用Remove方法 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/aceta ...
- centos7.2安装社区版docker-ce-17.06.1
先yum install安装如下包: container-selinux-2.21-1.el7.noarch libcgroup-0.41-13.el7.x86_64 libtool-ltdl-2.4 ...
- 微信小程序 - 3d轮播图组件(基础)
<!-- 目前仅支持data数据源来自banner,请看测试案例 ################ 以上三种形式轮播: 1. basic 2. 3d 3. book basic即普通轮播 3d即 ...
- Python+OpenCV实现FasterRcnn样本查看器
一.上代码 import cv2 import os def get_samples(dir): datasets = [] files = os.listdir(dir) for file in f ...
- 在centos7中离线方式安装mysql5.7
第一步:下载mysql 在Linux终端使用wget命令下载网络资源:(可以先下好) wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17 ...