What is the difference between an int and an Integer in Java and C#?

前端 未结 26 1484
生来不讨喜
生来不讨喜 2020-11-22 12:00

I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an i

相关标签:
26条回答
  • 2020-11-22 12:40

    This has already been answered for Java, here's the C# answer:

    "Integer" is not a valid type name in C# and "int" is just an alias for System.Int32. Also, unlike in Java (or C++) there aren't any special primitive types in C#, every instance of a type in C# (including int) is an object. Here's some demonstrative code:

    void DoStuff()
    {
        System.Console.WriteLine( SomeMethod((int)5) );
        System.Console.WriteLine( GetTypeName<int>() );
    }
    
    string SomeMethod(object someParameter)
    {
        return string.Format("Some text {0}", someParameter.ToString());
    }
    
    string GetTypeName<T>()
    {
        return (typeof (T)).FullName;
    }
    
    0 讨论(0)
  • 2020-11-22 12:42

    In C#, int is just an alias for System.Int32, string for System.String, double for System.Double etc...

    Personally I prefer int, string, double, etc. because they don't require a using System; statement :) A silly reason, I know...

    0 讨论(0)
  • 2020-11-22 12:42

    In Java, the int type is a primitive data type, where as the Integer type is an object.

    In C#, the int type is also a data type same as System.Int32. An integer (just like any other value types) can be boxed ("wrapped") into an object.

    0 讨论(0)
  • 2020-11-22 12:43

    int is predefined in library function c# but in java we can create object of Integer

    0 讨论(0)
  • 2020-11-22 12:43

    In java as per my knowledge if you learner then, when you write int a; then in java generic it will compile code like Integer a = new Integer(). So,as per generics Integer is not used but int is used. so there is so such difference there.

    0 讨论(0)
  • 2020-11-22 12:43

    int is a primitive data type. Integer is a wrapper class. It can store int data as objects.

    0 讨论(0)
提交回复
热议问题