• Post Reply Bookmark Topic Watch Topic
  • New Topic

implicit conversion  RSS feed

 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a quick question if anyone can help.

been trying to wrap my head around binary numeric promotion and its had a bit of an impact on how i think about assignment statements.

i guess my question is: are byte, char or short assignments always implicitly converted?

so is this: short s = 3; an implicit narrowing? (because 3 is an int that just so happens to be in the scope of a short).

i understand that binary promotion has little to do with it, but wondered if similar logic applied. feeling a little confused but will keep reading!


 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

In this case the compiler knows that 3 fits into a short so there is no conversion.

If you are interested in the rules -- please reply.

If this is good and answers your question, great.

-steve
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Fahlbusch wrote:Greetings,

In this case the compiler knows that 3 fits into a short so there is no conversion.

If you are interested in the rules -- please reply.

If this is good and answers your question, great.

-steve


Hi steve, thanks for the reply.

Yes, I am interested in the rules please if you've got the time! (or a good link)

Nick
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The different kinds of conversion are described in the Java® Language Specification. You won't find it easy to read.
The specification about how multiplication is done is to be found here. It tells you whether promotion is done. You can find other expressions from that link by scrolling up and down.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, Just popped in to link to the JLS but you did it --- thanks
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks - although i've just read the first few paragraphs...... you weren't kidding about it not being an easy read!

how much of this is at the java programmer I level? i didn't think it went into this much depth, but then the enthuware practice exam caught me out with (which i understand now) - so now i'm not sure... i know it never hurts to know, and i would prefer to, but i've got to get on and pass this thing, and I have a habit of getting distracted!!

thanks again,

nick
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick,

No it is not an easy read, which is why i asked if you wanted the rules or if you were ok with the answer.

And at level Java 1 - you shouldn't need this but...... These are the rules for the language. you should know where they are and what they mean.

And yes they are not easy to read, but important if you need to find out what the language means.

At this point in your java career i would suggest that you concentrate on using java, but remember you always have the specification to go back to.

-steve
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote: . . . . . .
Does that actually compile?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the compiler convert numeric and string operations using only compile time constants to additional compile time constants?
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but the right half of that assignment is a double.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Fahlbusch wrote: . . . No it is not an easy read, which is why i asked if you wanted the rules or if you were ok with the answer. . . .
It is always useful to know where the rules are. And you can always remember the parts you can actually understand
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, but the right half of that assignment is a double.


I'm pretty sure Java doesn't actually care about the type of compile time constants if their value is small enough to fit in a smaller datatype.

But then magnitude is completely different from precision of course...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Campbell Ritchie wrote:Yes, but the right half of that assignment is a double.


I'm pretty sure Java doesn't actually care about the type of compile time constants if their value is small enough to fit in a smaller datatype.

But then magnitude is completely different from precision of course...


Campbell is correct. You will simply need to remember the rules, as there doesn't seem to be a reason why certain cases are not allowed.

For example, a long compile time constant value, that fits into a short, is not allowed to be assigned to a short variable implicitly.

Henry
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
nick woodward wrote: . . . . . .
Does that actually compile?


thats why it caught me out and got me looking at conversions.

it won't allow implicit narrowing from a double to a float. so i think it has to be cast / or written 0 * 0.5f. which now makes sense in my mind because you can't have a 'range' of doubles that could fit in a float because they can have a greater precision at every value (correct?)

bit of a nasty question imo, but it just highlighted that I don't know primitives and assignment as well as i might need to.

just found this video which seems to have a useful table of rules at 1 min 30 (or so). https://www.youtube.com/watch?v=GABQgTXg4d8 seems half decent.

@henry why the hell can't a long be implicitly narrowed to a short! stupid java

anyway, thanks for all the help and advice

nick
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe, why do we have a short datatype in the first place?
If you look at the Streams API, you find there are ordinary Streams, and Streams representing three of the five primitives. That is because you do arithmetic and counting with ints, longs and doubles. So those three datatypes have corresponding Streams: 1 2 3, but you won't find the other 5 as Streams.
If you are going to write an expression which is a long in its format, even though 123L will fit into a byte, why should you expect to assign it without a cast? There is a concession for ints being assigned to narrower datatypes if they are compile‑time constants, but that is probably because there isn't a format 123b which defaults to byte type.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Or maybe, why do we have a short datatype in the first place?
If you look at the Streams API, you find there are ordinary Streams, and Streams representing three of the five primitives. That is because you do arithmetic and counting with ints, longs and doubles. So those three datatypes have corresponding Streams: 1 2 3, but you won't find the other 5 as Streams.
If you are going to write an expression which is a long in its format, even though 123L will fit into a byte, why should you expect to assign it without a cast? There is a concession for ints being assigned to narrower datatypes if they are compile‑time constants, but that is probably because there isn't a format 123b which defaults to byte type.


i understood almost all of that, but that is much more than i would've at the beginning of the week. progress! now to sit down and properly read that link!

i do get the gist of what you're saying though, i was too concerned with looking for an over-arching 'rule' than thinking if there was a reason to have the conversion in the first place.

thanks again for your time (and everyone else), much appreciated

Nick
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to hear of progress

And, you're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!