java与c#语法对比及转换

上传人:第*** 文档编号:34212444 上传时间:2018-02-22 格式:DOC 页数:15 大小:87.50KB
返回 下载 相关 举报
java与c#语法对比及转换_第1页
第1页 / 共15页
java与c#语法对比及转换_第2页
第2页 / 共15页
java与c#语法对比及转换_第3页
第3页 / 共15页
java与c#语法对比及转换_第4页
第4页 / 共15页
java与c#语法对比及转换_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《java与c#语法对比及转换》由会员分享,可在线阅读,更多相关《java与c#语法对比及转换(15页珍藏版)》请在金锄头文库上搜索。

1、Java Comments C#/ Single line/* Multipleline */* Javadoc documentation comments */ Single line/* Multipleline */ XML comments on a single line/* XML comments on multiple lines */Java Data Types C#Primitive Typesbooleanbytecharshort, int, longfloat, doubleReference TypesObject (superclass of all othe

2、r classes)Stringarrays, classes, interfacesConversions / int to String int x = 123; String y = Integer.toString(x); / y is 123/ String to inty = 456; x = Integer.parseInt(y); / x is 456/ double to intValue Typesboolbyte, sbytecharshort, ushort, int, uint, long, ulongfloat, double, decimalstructures,

3、 enumerationsReference Typesobject (superclass of all other classes)stringarrays, classes, interfaces, delegatesConvertions/ int to string int x = 123; String y = x.ToString(); / y is 123/ string to inty = 456; x = int.Parse(y); / or x = Convert.ToInt32(y);double z = 3.5; x = (int) z; / x is 3 (trun

4、cates decimal)/ double to intdouble z = 3.5; x = (int) z; / x is 3 (truncates decimal)Java Constants C#/ 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. readonly int MAX_HEIGHT = 9; Java E

5、numerations C#enum Action Start, Stop, Rewind, Forward;/ Special type of class enum Status Flunk(50), Pass(70), Excel(90);private final int value;Status(int value) this.value = value; public int value() return value; ; Action a = Action.Stop;if (a != Action.Start)System.out.println(a); / Prints Stop

6、Status s = Status.Pass;System.out.println(s.value(); / Prints 70 enum Action Start, Stop, Rewind, Forward;enum Status Flunk = 50, Pass = 70, Excel = 90;No equivalent.Action a = Action.Stop;if (a != Action.Start)Console.WriteLine(a); / Prints StopStatus s = Status.Pass;Console.WriteLine(int) s); / Pr

7、ints 70Java Operators C#Comparison= = !=Arithmetic+ - * /% (mod)/ (integer division if both operands are ints)Math.Pow(x, y)Assignment= += -= *= /= %= &= |= = = = + -Bitwise& | Logical& | & | !Note: & and | perform short-circuit logical evaluationsString Concatenation+Comparison= = !=Arithmetic+ - *

8、 /% (mod)/ (integer division if both operands are ints)Math.Pow(x, y)Assignment= += -= *= /= %= &= |= = = + -Bitwise& | Logical& | & | !Note: & and | perform short-circuit logical evaluationsString Concatenation+Java Choices C#greeting = age list = new ArrayList();list.add(10); / boxing converts to

9、instance of Integerlist.add(Bisons);list.add(2.3); / boxing converts to instance of Doublefor (Object o : list)System.out.println(o); collection using System.Collections;ArrayList list = new ArrayList();list.Add(10);list.Add(Bisons);list.Add(2.3);foreach (Object o in list)Console.WriteLine(o);Java A

10、rrays C#int nums = 1, 2, 3; or int nums = 1, 2, 3;for (int i = 0; i nums.length; i+)System.out.println(numsi);String names = new String5;names0 = David;float twoD = new floatrowscols;twoD20 = 4.5; int jagged = new int5; jagged0 = new int5; jagged1 = new int2; jagged2 = new int3; jagged04 = 5; int nu

11、ms = 1, 2, 3;for (int i = 0; i nums.Length; i+)Console.WriteLine(numsi);string names = new string5;names0 = David;float, twoD = new floatrows, cols;twoD2,0 = 4.5f; int jagged = new int3 new int5, new int2, new int3 ; jagged04 = 5; Java Functions C#/ Return single valueint Add(int x, int y) return x

12、+ y; int sum = Add(2, 3); / Return no valuevoid PrintSum(int x, int y) System.out.println(x + y); PrintSum(2, 3); / Primitive types and references are always passed by valuevoid TestFunc(int x, Point p) x+; p.x+; / Modifying property of the objectp = null; / Remove local reference to object class Po

13、int public int x, y; Point p = new Point(); p.x = 2; int a = 1; TestFunc(a, p);System.out.println(a + + p.x + + (p = null) ); / 1 3 false / Return single valueint Add(int x, int y) return x + y; int sum = Add(2, 3); / Return no valuevoid PrintSum(int x, int y) Console.WriteLine(x + y); PrintSum(2, 3

14、); / Pass by value (default), in/out-reference (ref), and out-reference (out) void TestFunc(int x, ref int y, out int z, Point p1, ref Point p2) x+; y+; z = 5; p1.x+; / Modifying property of the object p1 = null; / Remove local reference to object p2 = null; / Free the object class Point public int

15、x, y; Point p1 = new Point(); Point p2 = new Point(); p1.x = 2; int a = 1, b = 1, c; / Output param doesnt need initializing / Accept variable number of argumentsint Sum(int . nums) int sum = 0;for (int i : nums)sum += i;return sum; int total = Sum(4, 3, 2, 1); / returns 10TestFunc(a, ref b, out c, p1, ref p2); Console.WriteLine(0 1 2

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号