• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A quick literals question

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have a way to check myself at the moment, but K&B7 says that

The result of an expression involving anything int-sized or smaller is always an int


It seems obvious, but that means either operand, right?

Actually, while I'm on the topic of primitives, is there a good thread here that summarizes all the 'rules' for assignment?

So far I know that:

- integer literals are ints.
- for longs i assume ^ this is still the case, but implicit widening occurs
- for anything smaller, if the literal is in the variable's scope then implicit narrowing occurs, otherwise you need to cast.
- if you're assigning an expression (where the variable is less than an int) then you have to cast to that type, unless its a compound assignment, in which case it's implicit.
- in addition, i *think* that the last point doesn't apply if the expression operands are compile time constants, or in scope literals themselves.

I'm sure it's not, but does this seem correct and/or complete? Anything obvious I've missed or got the wrong end of the stick on?

Thanks,

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:It seems obvious, but that means either operand, right?

Yes!

nick woodward wrote:is there a good thread here that summarizes all the 'rules' for assignment?

Not that I'm aware of because it's all pretty straightforward. Every expression results in an int, so if you want to assign it to something smaller (like byte or short), you'll need an explicit cast. There's a good thread about using == operator on wrapper classes.

=nick woodward- integer literals are ints.

True! If the literal is in the int range of course; otherwise you'll get a compiler error.

nick woodward wrote:- for longs i assume ^ this is still the case, but implicit widening occurs

Wrong! You must use the L (or l) suffix, otherwise your code won't compile. More detailed info in this post.

nick woodward wrote:- for anything smaller, if the literal is in the variable's scope then implicit narrowing occurs, otherwise you need to cast.

Correct!

nick woodward wrote:- if you're assigning an expression (where the variable is less than an int) then you have to cast to that type, unless its a compound assignment, in which case it's implicit.
- in addition, i *think* that the last point doesn't apply if the expression operands are compile time constants, or in scope literals themselves.

Spot-on! So regarding the compile-time constants remark: if you have a sum of 2 compile-time constants and the result is in the range of the data type, you don't need an explicit cast. If it's not in range, you need to add a cast; otherwise the code won't compile. Illustrated in these two code snippets

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:

Hope it helps!
Kind regards,
Roel


it does indeed roel, thanks! very interested to read that long link. for once i'm actually surprised it doesn't work the way i assumed!

regards,

nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:for once i'm actually surprised it doesn't work the way i assumed!

And what was a surprise for you? Because maybe there's a simple explanation for which makes it easier to understand.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:for once i'm actually surprised it doesn't work the way i assumed!

And what was a surprise for you? Because maybe there's a simple explanation for which makes it easier to understand.


normally i'd expect to be wrong :P


so to assign a literal (integer) to a long you have to use 'L'?

it seems so odd that the compiler can't pick that up.

so it isn't really about the assignment at all, its about the fact that you're trying to represent a long with an integer? can you cast the long (rather than use 'l') if it is within an int range/representable?

**edit - i'll try to answer my own question now i'm back on my pc - it seems like if its in the int range it is implicitly widened. so 'long l = 23' works fine.

thanks again!

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:so to assign a literal (integer) to a long you have to use 'L'?

No, probably there's a slight misunderstanding or my explanation was not clear enough.

If you have an int literal and want to assign it to a long primitive variable, it will be automatically widened. You don't need to do anything at all. Like in the example you providedBut now consider that you have a long literal (which is out of range for an int) and want to assign it to a long primitive variable. Because each integer literal is an int, you have to tell the compiler that this literal must be treated as a long. And that's why you must add the L (or l) suffix. Otherwise the code won't compile, because it's out of range for an int. Both are illustrated in this code snippetMore detailed information can be found in this post.

Apologies for the confusion!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah that was basically what i discovered in my edit/when i got back to my pc/text editor. thanks for the confirmation though, much appreciated!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:is there a good thread here that summarizes all the 'rules' for assignment?

Not that I'm aware of because it's all pretty straightforward.

Today I vaguely remembered a topic about widening, conversion, boxing,... of primitive data types like float and long. So using the forum's search engine, I found that topic. It's definitely worth a read and probably the closest you'll find here on CodeRanch to what you are looking for. And this one is about a long fitting in a float.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Roel De Nijs wrote:
nick woodward wrote:is there a good thread here that summarizes all the 'rules' for assignment?

Not that I'm aware of because it's all pretty straightforward.

Today I vaguely remembered a topic about widening, conversion, boxing,... of primitive data types like float and long. So using the forum's search engine, I found that topic. It's definitely worth a read and probably the closest you'll find here on CodeRanch to what you are looking for. And this one is about a long fitting in a float.


thanks, will definitely read both of those!

Nick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic