Learning Java 2th
Suppose you are familiar with C#, so this article will not tell the most basic Java features, all I want to talk about is comparisons between Java and C#. Here we go.
1. Naming conventions
In Java, every method's name begins with a lowercase. The same to the package name.
2. Special reservedwords
1)final:similar to sealed and const in C#, but there is a litter difference from them. See the examples:
/* *java code *you can initialize the final field in a constructor *you can define a method to final whatever you want */ public class Test{ private final String PI; public Test(){ PI = "test"; } public final void call(){ } }
/* *C# code *you can only initialize the const field when you declare it *sealed must be used with override when you define a method */ public class Test { private const string PI = “test”; public sealed override int GetHashCode()
{ return 1; } }
2)import:similar to using in C#
3)@Override: it is a sign to show that this method override from the parent class. You can choose to add it or not. And I strongly recommend to add it, because it gets better complier support, in complie time, the complier could help to check out whether this method overrides from the parent class.Compared with C#, I would like to say that C# is more strict, because it requires you to add override to the method if it really override from the parent class.
4) package: similar to namespace in C#,but it's a little difference. The package name represents reality directory tree.
5) instanceof: similar to is in C#
6) super: similar to base in C#
7) protected: similar to protected internal in C#

浙公网安备 33010602011771号