java and c# comparison - java 与 c# 语法对比总结

上传人:第*** 文档编号:34063874 上传时间:2018-02-20 格式:DOC 页数:13 大小:94.50KB
返回 下载 相关 举报
java and c# comparison - java 与 c# 语法对比总结_第1页
第1页 / 共13页
java and c# comparison - java 与 c# 语法对比总结_第2页
第2页 / 共13页
java and c# comparison - java 与 c# 语法对比总结_第3页
第3页 / 共13页
java and c# comparison - java 与 c# 语法对比总结_第4页
第4页 / 共13页
java and c# comparison - java 与 c# 语法对比总结_第5页
第5页 / 共13页
点击查看更多>>
资源描述

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

1、Java and C# Comparison / Java 与 C# 语法对比总结Java Program Structure C#package hello;public class HelloWorld public static void main(String args) String name = Java;/ See if an argument was passed from the command lineif (args.length = 1)name = args0;System.out.println(Hello, + name + !); using System; n

2、amespace Hello public class HelloWorld public static void Main(string args) string name = C#;/ See if an argument was passed from the command lineif (args.Length = 1)name = args0;Console.WriteLine(Hello, + name + !); Java Comments C#/ Single line/* Multipleline */* Javadoc documentation comments */

3、Single line/* Multipleline */ XML comments on a single line/* XML comments on multiple lines */Java Data Types C#Primitive Typesbooleanbytecharshort, int, longValue Typesboolbyte, sbytecharshort, ushort, int, uint, long, ulongfloat, doubleReference TypesObject (superclass of all other classes)String

4、arrays, 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 intdouble z = 3.5; x = (int) z; / x is 3 (truncates decimal)float, double, decimalstructures, enumerationsReference Typ

5、esobject (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 to intdouble z = 3.5; x = (int) z; / x is 3 (truncates decim

6、al)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 Enumerations C#enum Action Start, Stop, Rewind, Forward;/ Special type of class enum

7、 Status Flunk(50), Pass(70), Excel(90);private final int value;Status(int value) this.value = value; public int value() return value; enum Action Start, Stop, Rewind, Forward;enum Status Flunk = 50, Pass = 70, Excel = 90;No equivalent.; Action a = Action.Stop;if (a != Action.Start)System.out.println

8、(a); / Prints StopStatus s = Status.Pass;System.out.println(s.value(); / Prints 70 Action a = Action.Stop;if (a != Action.Start)Console.WriteLine(a); / Prints StopStatus s = Status.Pass;Console.WriteLine(int) s); / Prints 70Java Operators C#Comparison= = !=Arithmetic+ - * /% (mod)/ (integer division

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

10、 -= *= /= %= &= |= = = + -Bitwise& | Logical& | & | !Note: & and | perform short-circuit logical evaluationsString Concatenation+Java Choices C#greeting = age list = new ArrayList();list.add(10); / boxing converts to instance of Integerlist.add(Bisons);list.add(2.3); / boxing converts to instance of

11、 Doublefor (Object o : list)System.out.println(o); / foreach can be used to iterate through any 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 Arrays C#int nums = 1, 2, 3; or int num

12、s = 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 nums = 1, 2, 3;for (int i = 0; i nums.Le

13、ngth; 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 + y; int sum = Add(2, 3); / Return no

14、valuevoid PrintSum(int x, int y) System.out.println(x + y); / Return single valueint Add(int x, int y) return x + y; / Return no valuevoid PrintSum(int x, int y) Console.WriteLine(x + y); PrintSum(2, 3); / Primitive types and references are always passed by valuevoid TestFunc(int x, Point p) x+; p.x

15、+; / Modifying property of the objectp = null; / Remove local reference to object class Point 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 / Accept variable number of argumentsint Sum(int . nums) int sum = 0;for (int i : nums)sum += i;return sum; int tot

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


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

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