What is the difference between an int and an Integer in Java and C#?
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.
Difference between int and Integer
part 1
One example .. you can use Integer
as the key of HashMap
but you can't use int. Because an Object
is needed.
So where you need an int
value as an object there you need to use Integer
class.
part 2
++i is pre increment
i++ is post increment
for example
i = 0;
System.out.println(i++) //will print 0 then the i will be 1.
and
i = 0;
System.out.println(++i) // here i wil be incremented first then print 1.
Difference between int and new int()
int myNumber;
int myNumber = 0;
int myNumber = new int();
The effect should be exactly the same, you can try to ILDASM them if you're doubtful
Is there any difference between String and string in C# like int and Integer in Java?
No. string is an alias for String.
Same as:
int ==> Int32
double ==> Double
and others.
Difference between char (int) and int
As it can be seen, the integer splits. For quite a while, I am unable to figure it out?
You are not reading integers, you are reading bytes which have characters encoded as ?ASCII?
Is there a way where I can save the console value into an integer, as I would be using this value in the future?
The simplest way is to use a Scanner
Scanner scanner = new Scanner(in);
while (scanner.hasNextInt()) {
// read bytes up the next whitespace, parse as a int
int n = scanner.nextInt();
Difference Between int[] and list int
On the wire they will be indistinguishable - i.e. both Customer[]
and List<Customer>
are going to look (roughly) like:
<Customers>
<Customer name="Fred" ... />
<Customer name="Barney" ... />
</Customers>
So there is no point having logic that treats the two differently. Ultimately, int[]
is a pain to work with (Add
etc), so I would tend to use List<T>
- of course, wsdl.exe
nicely defaults to arrays, IIRC - but there is a command line switch (or perhaps there is for wse/wcf).
What is the difference between int, Int16, Int32 and Int64?
Each type of integer has a different range of storage capacity
Type Capacity
Int16 -- (-32,768 to +32,767)
Int32 -- (-2,147,483,648 to +2,147,483,647)
Int64 -- (-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807)
As stated by James Sutherland in his answer:
int
andInt32
are indeed synonymous;int
will be a little more
familiar looking,Int32
makes the 32-bitness more explicit to those
reading your code. I would be inclined to use int where I just need
'an integer',Int32
where the size is important (cryptographic code,
structures) so future maintainers will know it's safe to enlarge an
int
if appropriate, but should take care changingInt32
variables
in the same way.The resulting code will be identical: the difference is purely one of
readability or code appearance.
Difference between C# and Java's ternary operator (? :)
Looking through the C# 5 Language Specification section 7.14: Conditional Operator we can see the following:
If x has type X and y has type Y then
If an implicit conversion (§6.1) exists from X to Y, but not from Y to X, then Y is the type of the
conditional expression.If an implicit conversion (§6.1) exists from Y to X, but not from X to Y, then X is the type of the
conditional expression.Otherwise, no expression type can be determined, and a compile-time error occurs
In other words: it tries to find whether or not x and y can be converted to eachother and if not, a compilation error occurs. In our case int
and string
have no explicit or implicit conversion so it won't compile.
Contrast this with the Java 7 Language Specification section 15.25: Conditional Operator:
- If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression. (NO)
- If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T. (NO)
- If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type. (NO)
- Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases: (NO)
- Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2.
The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7). (YES)
And, looking at section 15.12.2.7. Inferring Type Arguments Based on Actual Arguments we can see it tries to find a common ancestor that will serve as the type used for the call which lands it with Object
. Object
is an acceptable argument so the call will work.
Related Topics
How to Use Linq with a MySQL Database on Mono
Conditional Operator Assignment with Nullable<Value> Types
Open Existing File, Append a Single Line
Assigning Out/Ref Parameters in Moq
Most Common C# Bitwise Operations on Enums
Sqlcommand Parameters Add VS. Addwithvalue
How to Bind a Tabcontrol to a Collection of Viewmodels
When Should I Create a Destructor
Selectsinglenode Returns Null When Tag Contains Xmlnamespace
C# Execute a Terminal Command in Linux
Run Async Method Regularly with Specified Interval
Why Do Assignment Statements Return a Value
How to Bind a List to a Combobox
Code for Decoding/Encoding a Modified Base64 Url (In ASP.NET Framework)
What Does a Lock Statement Do Under the Hood