0

My question is that if the following assignment in (1) involves type convernsion, i.e., from int to long?

(1)
    long i=0;
    ....
    i =2; 
(2)
    long i=0;
     .....
    i = 2L
Peaceful
  • 472
  • 5
  • 13

2 Answers2

2

Yes, it involves conversion. But with a literal like this, the conversion will normally happen at compile time, so appending the L won't make any real difference unless you find the code more readable with the L there (I normally don't).

There are a few cases where you can append a suffix to get a result that's actually different from what you'd get without the suffix, but the ones you've shown don't fall into this category.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Technically, yes, it's a conversion. The type of an integer literal without a suffix is int as long as the literal's value can indeed by represented by an int. This is certainly the case for the values 0 and 2. So, in i = 2, the right-hand side is int, and must be converted to long

However, it's only a conversion by the rules of the language. The compiler is undoubtedly going to generate code that directly sets the value of i to 2, rather than code that stores 2 in an int variable and then performs a run-time conversion.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312