Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Widening and narrowing conversion questions

 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm studying the operators and assignments and i have a couple of questions:
1. The author says that implicit narrowing primitive conversions on assignment can occur when the source is an int constant whose value can be determined to be in the range of the destination type at compile time. He also says that the type could be either byte, short or char. In its example the author states that:
byte tiny = (byte) 128; //This because int value is not in range. What does it mean? Is not in range because between byte and int is there short or because of the value?
2. The author says that an int negative value is not determinable and therefore a cast is required, referring to the following example:
int i = -20;
byte b2 = (byte) i; //Value of i not determinable
Why this value is not determinable?
Regards,
Marco
:roll:
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marco Tedone:
I'm studying the operators and assignments and i have a couple of questions:
1. The author says that implicit narrowing primitive conversions on assignment can occur when the source is an int constant whose value can be determined to be in the range of the destination type at compile time. He also says that the type could be either byte, short or char. In its example the author states that:
byte tiny = (byte) 128; //This because int value is not in range. What does it mean? Is not in range because between byte and int is there short or because of the value?
2. The author says that an int negative value is not determinable and therefore a cast is required, referring to the following example:
int i = -20;
byte b2 = (byte) i; //Value of i not determinable
Why this value is not determinable?
Regards,
Marco
:roll:


Hope this helps:
The different primative types in Java have what is known as different bit depths (ie: the bit size of the type).
To illustrate:
byte 8-bit signed
short 16-bit signed
int 32-bit signed
long 64-bit signed.
To answer your quetion 1. The problem is not with the value or the fact that there is a type between byte and int. The problem is that if you cast an int(32 bits) to a byte(8 bits) without letting the JVM know, explicitly cast, the JVM will complain about "possible loss of precision". If you do this: int i = 20; byte b = (byte)i; You are telling the JVM that you understand that there will be precision loss and you still want to make that assignment. As a result, the compiler makes the conversion for you.
For question 2: I am running v1.4.1 of the JVM under linux. If I do this: int i = -20; long l = i; I get no conversion error.
If I do this: int i = -20; byte b = (byte)i; I get no conversion error.
If I do this: int i = -20; byte b = i; I get a conversion error.
So I guess, in a long winded sort of way, I do not have an answer for your second question.
Hope this helps.

scoon
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. The first question helped me in clarifying the needed for a cast and, according to it, i would say that also for question number 2 the needed for a cast from int to byte is required. The reason why the widening promotion doesn't give a compilation error is that when in the same range, i.e. int -> long -> float -> double promotions are transitive, it means that an int will be implicitly promoted to any type to double.
Marco
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skip, it is not quite like this.
bute b1 = 128 // compile error
byte b2 = 127 // ok athough the literal 127 is type int

The way I would put it, with the help of the Java spec would be:
1.Question 1
The Java lang spec says (5.2):
"...a narrowing primitive conversion may be used if all of the following conditions are satisfied:
a.The expression is a constant expression of type byte, short, char or int.
b.The type of the variable is byte, short, or char.
c.The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable."
the assignment
byte tiny = 128
doesn't satisfy condition c above since a byte's range is -128,127. That's why you have to use a cast to inform the compiler you know what you're doing.
Other examples:
final int i = 20;
byte b = i //ok, the compiler knows what i holds
// and it can compare it against the byte range
final long i = 20;
byte b = i //compiler error, violates the first rule(a) stated above
2.Question 2
Maybe the statement "an int negative value is not determinable and therefore a cast is required" is misleading. It doesn't have to be negative to be indeterminable. It can be any expression that violates the rules above and you end up with a "narrowing primitive conversion" (JLS 5.1.3) and that has to be done through an explicit cast. The assignment (the second one)
int i = 20;
byte b2 = i;
violates rule c above (variable i is not final so the compiler assumes it can have any int value including values out of the byte range) and it gives you the "..loss of precision.." message.
[ February 11, 2003: Message edited by: Dan Culache ]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short ridder on what is determinable...
final variables and literals are determinable - basically if the compiler is 100% sure that data will not be lost a cast is not needed (and he is only 100% sure on final and literal assignments).
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies, now the concepts of being in range and narrowing are clear.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic