1. Static constructors
There are static constructors in C #, and there are no static constructors in Java. In fact, Java has a similar static constructor in the east, called static initialization, or static code block, you can achieve the same function by this code:
But static code blocks in Java and C # static constructors are still different. Static constructors in C # execute after other static members are initialized, whereas static code blocks and other static members in Java are executed first.
1 classparent{2 Public StaticStaticvariable staticvariable =NewStaticvariable ("Parent-static Variable1");3 PublicStaticvariable instaticvariable =NewStaticvariable ("Parent-instant Variable1");4 5 Static6 {7System.out.println ("Parent-static block/constructor");8System.out.println (staticvariable = =NULL); 9 //System.out.println (StaticVariable2 = = null); not working because StaticVariable2 are not definedTenStaticVariable2 =NewStaticvariable ("Parent-static variable2-static block"); One } A - { -System.out.println ("Parent-initializer Block"); the } - - Public StaticStaticvariable StaticVariable2 =NewStaticvariable ("Parent-static Variable2"); - PublicStaticvariable InStaticVariable2 =NewStaticvariable ("Parent-instant Variable2"); + - PublicParent () + { ASystem.out.println ("Parent-instance Constructor"); at } -}View Java Code
1 classStaticdemo2 {3 Static inti =1;4 5 StaticStaticdemo ()6 {7 Console.WriteLine (i);8 Console.WriteLine (j);9 }Ten One Public Static voidExecute () A { - } - the Static intj =1; -}View C # Code
2 Constants
The declaration ( instance/ class ) constants in Java Use keywords (final/static final). the declaration ( instance/ Class) constants in C # use keywords (readonly/const or readonly static).
C # must use the class name to access the class-level variables. In Java, you can use an instance to access a class-level variable, but there is a warning at compile time.
Java Code and C # Code
1 classparentdef{2 Public Static FinalString staticvalue_string= "Parent Static Variable";3 PublicString valuestring= "Parent Instant Variable";4 }5 6 classChildrefextendsparentdef{7 Public Static FinalString staticvalue_string= "Child Static Variable";8 PublicString valuestring= "Child Instant Variable";9 }Ten One Public classBasicdemo { A Public Static voidMain (string[] args) { - //Child child = new Child (); - theParentdef PDF =Newparentdef (); -Parentdef PCDF =Newchildref (); -Childref CDF =Newchildref (); -System.out.println ("V1"); + System.out.println (PDF. staticvalue_string); - System.out.println (pdf.valuestring); + ASystem.out.println ("V2"); at System.out.println (PCDF. staticvalue_string); - System.out.println (pcdf.valuestring); - -System.out.println ("V3"); - System.out.println (CDF. staticvalue_string); - System.out.println (cdf.valuestring); in - } to +}View Code
1 classInheritencedemo2 {3 Public Static voidExecute ()4 {5Parentdef PDF =Newparentdef ();6Parentdef PC =Newchilddef ();7Childdef CDF =Newchilddef ();8 9 TenConsole.WriteLine ("V1"); One Console.WriteLine (pdf.value); A - -Console.WriteLine ("V2"); the Console.WriteLine (pc.value); - -Console.WriteLine ("V3"); - Console.WriteLine (cdf.value); + Console.WriteLine (CDF. readonlystring); - + } A } at - classParentdef - { - Public Const stringConst_string ="Parent Const Varialbe"; - Public Static stringStaticvalue_string ="Parent Static Variable"; - Public stringValue ="Parent Instant Variable"; in } - to classChilddef:parentdef + { - Public ReadOnly stringreadonlystring="Child readonly Variable"; the Public ReadOnly Static stringReadonlystaticstring ="Child readonly static variable"; * Public Static stringStaticvalue_string ="Child Static Variable"; $ Public stringValue ="Child Instant Variable";Panax Notoginseng - Publicchilddef () the { +Readonlystring ="NEW Child readonly variable"; A //readonlystaticstring = "NEW child readonly static variable"; ERROR as satatic readonly variable can not is reassianged in instant constructor the } +}View Code
3 parameter passing
There are ref keywords in C # that are used to pass parameters by reference. Java does not, and cannot really pass parameters by reference. Java is always called by value. The method obtains a copy of all parameter values, in particular, the method cannot modify the contents of any parameter variables passed to it.
(1): "In Java parameter passing is passed by value" This sentence means: Pass by value is a copy of the value passed, by reference is actually passed the value of the referenced address, so collectively by value passed.
(2): There are only basic types in Java and strings that are defined in the following way are passed by value, others are passed by reference. is to define the string directly using double quotes: string str = "Java";
C # code
class refexample { staticvoid Method (refint; } staticvoid Main () { int0; Method (ref// }}
C # VS JAVA differences