资源描述:
《java and c comparison - java 与 c 语法对比总结》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、JavaandC#Comparison/Java与C#语法对比总结JavaProgramStructureC#packagehello;publicclassHelloWorld{ publicstaticvoidmain(String[]args){ Stringname="Java"; //Seeifanargumentwaspassedfromthecommandline if(args.length==1) name=args[0]; System.out.println("Hello,"+name+"
2、!"); }}usingSystem;namespaceHello{ publicclassHelloWorld{ publicstaticvoidMain(string[]args){ stringname="C#"; //Seeifanargumentwaspassedfromthecommandline if(args.Length==1) name=args[0]; Console.WriteLine("Hello,"+name+"!"); } }}JavaC
3、ommentsC#//Singleline/*Multiple line *//**Javadocdocumentationcomments*///Singleline/*Multiple line *////XMLcommentsonasingleline/**XMLcommentsonmultiplelines*/JavaDataTypesC#PrimitiveTypesbooleanbytecharshort,int,ValueTypesboolbyte,sbytecharshort,ushort,int,uint,long,longfloat
4、,doubleReferenceTypesObject (superclassofallotherclasses)Stringarrays,classes,interfacesConversions//inttoStringintx=123;Stringy=Integer.toString(x); //yis"123"//Stringtointy="456"; x=Integer.parseInt(y); //xis456// doubletointdoublez=3.5;x=(int)z; //xis 3 (truncatesdecimal)ulon
5、gfloat,double,decimalstructures,enumerationsReferenceTypesobject (superclassofallotherclasses)stringarrays,classes,interfaces,delegatesConvertions//inttostringintx=123;Stringy=x.ToString(); //yis"123"//stringtointy="456";x=int.Parse(y); //orx=Convert.ToInt32(y);// doubletointdou
6、blez=3.5;x=(int)z; // xis 3 (truncatesdecimal)JavaConstantsC#//MaybeinitializedinaconstructorfinaldoublePI=3.14;constdoublePI=3.14;//Canbesettoaconstoravariable.Maybeinitializedinaconstructor.readonlyintMAX_HEIGHT=9;JavaEnumerationsC#enumAction{Start,Stop,Rewind,Forward};//Special
7、typeofclassenumStatus{ Flunk(50),Pass(70),Excel(90); privatefinalintvalue; Status(intvalue){this.value=value;} publicintvalue(){returnvalue;}enumAction{Start,Stop,Rewind,Forward};enumStatus{Flunk=50,Pass=70,Excel=90};Noequivalent.};Actiona=Action.Stop;if(a!=Action.Start) System.out
8、.println(a);