On some subtle differences between Java and C

  • 2020-05-24 06:02:34
  • OfStack

Due to my work and previous study, I need to use Java and C# at the same time. In recent years, I have been using J2EE and.NET platforms for development. When it comes to the languages C# and Java (syntax, data types, etc.), I personally think that there is probably more than 90% similarity, or even almost one. But in my work, I also find some subtle differences, and I often confuse small details.

Such as:

Strings are defined in Java, usually using "String" (first letter uppercase), and in C#, 1 is usually defined in string (first letter lowercase). When I use Microsoft Visual Studio and Eclipse at the same time, I often confuse the case of "string type".

Moreover, Java defines the Integer type, while C# does not have this syntax, but they all want to describe the same thing, only in different ways.

Constant declaration: Java USES the final keyword for this, while C# USES the const or readonly keyword.

C# provides all data types available in Java and adds support for unsigned Numbers and new 128-bit precision floating point types.

In Java, for each base data type, the core class library provides a wrapper class to represent it as an Java object. For example, the Integer class wraps the int data type, while the Double class wraps the double data type.

In C#, all basic data types are objects in the System namespace. For each C# data type, provide one abbreviation or alias. For example, int is short for System.Int32, while double is short for System.Double. Because C# represents all basic data types as objects, it is possible to invoke object methods based on the basic C# data type. Such as:


    int i=10;  
    Console.WriteLine(i.ToString()); 

There is also the use of the generic collection List in Java, which is usually defined in this way (C# is defined in such a way that it will fail to compile) :

List<int> list = new ArrayList<int>();

In C#, the generic collection List must be defined in such a way that the compilation does not pass if Java is used in such a way.

  List<int> list = new List<int>();

The 1 small example described above is typical. And it's mostly related to syntax, data types, and so on. Due to my limited level and time, for those more detailed and in-depth content (such as: why must be defined this way...) I won't go into details for the moment. In the future, I will continue to sort out, conduct in-depth research and update.

This article is just a brick to attract jade, I hope that readers can understand, enthusiastic friends can also 1 from research, summary, collation for everyone to learn and share.


Related articles: