码迷,mamicode.com
首页 > 编程语言 > 详细

C# VS JAVA 差异

时间:2015-12-21 18:15:09      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

1. 静态构造函数

C#中有静态构造函数, Java中没有静态构造函数。其实Java中有一个类似静态构造函数的东东,称作静态初始化,或者静态代码块,可以通过这样的代码实现相同的功能:

但是Java中静态代码块和C#静态构造函数还是不一样的。C#中静态构造函数在其他静态成员初始化后再执行,而java中静态代码块和其他静态成员谁在先谁就先执行。

技术分享
 1 class Parent{
 2     public static StaticVariable staticVariable = new StaticVariable("Parent - Static Variable1");
 3     public StaticVariable inStaticVariable = new StaticVariable("Parent - Instant Variable1");
 4     
 5     static
 6     {
 7         System.out.println("Parent - Static block/constructor");
 8         System.out.println(staticVariable == null); 
 9         //System.out.println(staticVariable2 == null); not working because staticVariable2 is not defined
10         staticVariable2 = new StaticVariable("Parent - Static Variable2 -  Static block");
11     }
12     
13     {
14         System.out.println("Parent - Initializer Block");
15     }
16     
17     public static StaticVariable staticVariable2 = new StaticVariable("Parent - Static Variable2");
18     public StaticVariable inStaticVariable2 = new StaticVariable("Parent - Instant Variable2");
19     
20     public Parent()
21     {
22         System.out.println("Parent - Instance Constructor");
23     }
24 }
View Java Code

 

技术分享
 1     class StaticDemo
 2     {
 3         static int i = 1;
 4 
 5         static StaticDemo() 
 6         {
 7             Console.WriteLine(i);
 8             Console.WriteLine(j);
 9         }
10 
11         public static void Execute()
12         {
13         }
14 
15         static int j = 1;
16     }
View C# Code

 

2常量

Java中声明(实例/)常量使用关键词(final/static final)。C#中声明(实例/类)常量使用关键词(readonly/const 或者 readonly static).

C#中必须使用类名去访问类层级的变量。Java中可以使用实例去访问类层级的变量,但是编译时会有警告。

Java Code and C# Code

技术分享
 1 class ParentDef{
 2     public static final String STATICVALUE_STRING="Parent Static Variable";
 3     public String valueString="Parent Instant Variable";
 4 }
 5 
 6 class ChildRef extends ParentDef{
 7     public static final String STATICVALUE_STRING="Child Static Variable";
 8     public String valueString="Child Instant Variable";
 9 }
10 
11 public class BasicDemo {
12     public static void main(String[] args) {
13         //Child child = new Child();
14         
15         ParentDef pdf = new ParentDef();
16         ParentDef pcdf = new ChildRef();
17         ChildRef cdf = new ChildRef();
18         System.out.println("V1");
19         System.out.println(pdf.STATICVALUE_STRING);
20         System.out.println(pdf.valueString);
21         
22         System.out.println("V2");
23         System.out.println(pcdf.STATICVALUE_STRING);
24         System.out.println(pcdf.valueString);
25         
26         System.out.println("V3");
27         System.out.println(cdf.STATICVALUE_STRING);
28         System.out.println(cdf.valueString);
29         
30     }
31 
32 }
View Code
技术分享
 1     class InheritenceDemo
 2     {
 3         public static void Execute() 
 4         {
 5             ParentDef pdf = new ParentDef();
 6             ParentDef pc = new ChildDef();
 7             ChildDef cdf = new ChildDef();
 8 
 9 
10             Console.WriteLine("V1");
11             Console.WriteLine(pdf.value);
12 
13     
14             Console.WriteLine("V2");
15             Console.WriteLine(pc.value);
16 
17             Console.WriteLine("V3");
18             Console.WriteLine(cdf.value);
19             Console.WriteLine(cdf.READONLYSTRING);
20 
21         }
22     }
23 
24     class ParentDef
25     {
26         public const string Const_String = "Parent Const Varialbe";
27         public static string STATICVALUE_STRING = "Parent Static Variable";
28         public string value = "Parent Instant Variable";
29     }
30 
31     class ChildDef:ParentDef
32     {
33         public readonly string READONLYSTRING="Child readonly variable";
34         public readonly static string READONLYSTATICSTRING = "Child readonly  static variable";
35         public static string STATICVALUE_STRING = "Child Static Variable";
36         public string value = "Child Instant Variable";
37 
38         public ChildDef() 
39         {
40             READONLYSTRING = "NEW Child readonly variable";
41             //READONLYSTATICSTRING = "NEW Child readonly  static variable"; ERROR as satatic readonly variable can not be reassianged in instant constructor
42         }
43     }
View Code

 

3参数传递

C#中有ref关键词用来按引用传递参数。Java则没有,无法真正按引用传递参数。Java总是采用按值调用。方法得到的是所有参数值的一个拷贝,特别的,方法不能修改传递给它的任何参数变量的内容。

(1):“在Java里面参数传递都是按值传递”这句话的意思是:按值传递是传递的值的拷贝,按引用传递其实传递的是引用的地址值,所以统称按值传递。
(2):在Java里面只有基本类型和按照下面这种定义方式的String是按值传递,其它的都是按引用传递。就是直接使用双引号定义字符串方式:String str = “Java”;

C# code

  class RefExample 
  { 
  static void Method(ref int i) 
  { 
  i = 44; 
  } 
  static void Main() 
  { 
  int val = 0; 
  Method(ref val); // val is now 44 
  } 
  }

 

C# VS JAVA 差异

标签:

原文地址:http://www.cnblogs.com/1zhk/p/5054620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!