Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

c#

integer

int

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 int and an Integer in Java/C# (Object-Oriented Programming Languages).

So, what is the difference?

like image 802
CodingWithoutComments Avatar asked Aug 02 '08 21:08

CodingWithoutComments


People also ask

What is the difference between int and integer in Java?

A int is a data type that stores 32 bit signed two's compliment integer. On other hand Integer is a wrapper class which wraps a primitive type int into an object. int helps in storing integer value into memory. Integer helps in converting int into object and to convert an object into int as per requirement.

What is the difference between int and int in C?

Nothing. They are one and the same. The end result is same — we have two integer variables named a and b and they both are uninitialized. What is the difference between * (int *) and (int *) in C (pointers and development)?

Is int an integer in C?

In both languages (Java and C#) int is 4-byte signed integer.

What is an integer in Java?

An integer in Java is a memory location that can hold an integer, a positive or negative non-decimal number. It is denoted by the keyword, 'int'.


1 Answers

In Java, the 'int' type is a primitive, whereas the 'Integer' type is an object.

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


The differences between objects and primitives are somewhat beyond the scope of this question, but to summarize:

Objects provide facilities for polymorphism, are passed by reference (or more accurately have references passed by value), and are allocated from the heap. Conversely, primitives are immutable types that are passed by value and are often allocated from the stack.

like image 132
Matt Avatar answered Oct 09 '22 05:10

Matt