Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive assignments

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick query on primitive assignments if anyone minds clarifying. The quotes below make sense to me, but the context was pretty poor.


The most important point to remember is that a literal integer (such as 7) is always implicitly an int

ok


the compiler automatically narrows the literal value

ok


We tried to assign the sum of two bytes to a byte variable, the result of which (11) was definitely small enough to fit into a byte, but the compiler didn't care. It knew the rule about int -or- smaller expressions always resulting in an int


So is it correct to assume that both literal integers and expressions (involving ints or smaller) are implicitly ints, but that the former is implicitly cast, and the latter isn't?

Thanks,

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:So is it correct to assume that both literal integers and expressions (involving ints or smaller) are implicitly ints, but that the former is implicitly cast, and the latter isn't?

Have a look at this code snippets which adds 2 bytesThis code doesn't compile, because the add expression (b1 + b2) results in an int and although the actual result (100) fits into a byte, you can't assign an int to a byte without an explicit cast. So the correct statement should be

You could even use compile-time constants of type int and the code will still compileorBut if you use "normal" constants (not compile-time constants), you'll need an explicite cast; otherwise the code will fail to compile as this example illustrates

And finally, if the result of the expression is outside the range (of the type the result is assigned to), you'll need an explicit cast as well. Another example (which will not compile)

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so much better than the K&B explanation! surprising, seeing as the OCA/OCP book is one of my favourites.

so its not the expression and the literal that differ, its the fact that a literal is known at compile time, and the expression the book used (byte c = a + b) was not.

thanks!

Nick

PS - to others who might read this. henry wong's compile time post was useful: http://www.coderanch.com/t/454384/java/java/compile-time-constant
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:

I known such we couldn't declare final variable without initialization. But after testing this code I see that we can declare final variable without initalization for local variables. Maybe this initalization rule belongs to instance and static variables. Maybe I should read compile-time constants and normal constants.
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can also declare final instance fields without initializing them, as long as they're initialized before the end of the constructor body.

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:No, you can also declare final instance fields without initializing them, as long as they're initialized before the end of the constructor body.


It is clear, we can initialize final instance fields at the same line, in constructor and in instance initializer block. But if we doesn't do one of these steps we get compile error. But it doesn't happen with local final variable.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe my previous understanding wasn't quite sound enough, because I'm not sure why compound assignments are allowed.

so why byte b = 3; b+=7; is ok, but b = b+7 is not ok. why isn't b+=7 implicitly an int?

not that i'm fussed - studying has been going well this morning for once
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:so why byte b = 3; b+=7; is ok, but b = b+7 is not ok. why isn't b+=7 implicitly an int?

Very easy! The statement is equivalent to So an explicit cast is added automatically for you. This means you can get "weird" results if the result of the addition is beyond the range of the primitive data type, as shown in this code snippet

Hope it helps!
Kind regards,
Roel

 
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
Stephan van Hulst wrote:No, you can also declare final instance fields without initializing them, as long as they're initialized before the end of the constructor body.

I like to add this tiny remark: you could initialize an instance field in an instance initializer block as well. But the Java compiler copies instance initializer blocks into every constructor. So despite this difference in syntax, the statement is correct

You'll have 3 ways to initialize a final instance variable: assign a value instantly OR in a constructor OR in an initializer block. A nice overview of initializing (final) variables (static, instance and local) and the consequences can be found in this great post. (Disclaimer: it's one of my own posts )
 
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
Mushfiq Mammadov wrote:I known such we couldn't declare final variable without initialization. But after testing this code I see that we can declare final variable without initalization for local variables.

As long as you don't use a local variable in your code, the variable doesn't need to be initialized. But if you use the local variable, it must be initialized; otherwise you'll get a compiler error.

This code compiles successfully (although a local variable and a final local variable weren't initialized)
Hope it helps!
Kind regards,
Roel
 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All primitive integer arithmetic entails numeric promotion to int unless you are already using ints (in which case there is no change).
Unless you have a long on either side of your arithmetic operator, in which case everything smaller than 64 bits is promoted to a long.
If you look in the Java┬« Language Specification (=JLS), you find that compound assignment operators implement widening and narrowing conversion. I think the JLS explains it better than I could, but the JLS is not easy reading.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:

It is an interesting example, thanks

Roel De Nijs wrote: As long as you don't use a local variable in your code, the variable doesn't need to be initialized.

Thanks!
 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That simply shows an overflow error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic