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 ...
随机推荐
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
- Setting a maximum attachment size
By default IBM® Lotus® iNotes™ allows a maximum attachment size of 50,000K (50MB). You can increas ...
- Syntax error missing ; before *
[问题] I have a header file like so: #pragma once #include "gamestate.h" #include "Ex ...
- Sublime Es6教程2-基本语法
2.基本语法 let, const, forEach,for of class, extends, super arrow functions, template string, destructur ...
- PHP的异常以及异常存在的意义
php的try catch与其它语言的try catch相比有许多不同,而且用起来相对比较不爽. php中,如果你制作的站点相对较大,同时模块化,并且在错误处理机制上有一套自己的处理机制,可以尝试使用 ...
- 谷哥的小弟学前端(10)——JavaScript基础知识(1)
探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 具体解释Android主流框架不可或缺的基石 站在源代码的肩膀上全解Scroller工作机制 Android多分辨率适 ...
- 数据库中truncate与delete的区别与联系
昨天被问到truncate与delete的区别,truncate没用过,回去百度了一下,才知道还有这个一种语句. truncate table命令将快速删除数据表中的所有记录(保留数据表结构).这种快 ...
- QT之设计部件背景色
一.使用QT样式表设计部件外观 样式表使用文本描写叙述,能够使用QApplication::setStyleSheet()函数将其设置到整个应用程序上.也能够使用QWidget::setStyleSh ...
- Atlas系列一:【已解决】error while loading shared libraries: libcrypto.so.6: cannot open shared object file: No such file or directory
1:Atlas的安装 https://github.com/Qihoo360/Atlas/wiki/Atlas的安装 2: [root@localhost bin]# ./mysql-proxyd t ...
- Webwork【01】Webwork与 Struct 的前世今生
Struts 1是全世界第一个发布的MVC框架,它由Craig McClanahan在2001年发布,该框架一经推出,就得到了世界上Java Web开发者的拥护,经过长达6年时间的锤炼,Struts ...