Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Casting

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !
Its everybody's secret that for narrowing conversions we seek the help of casting and not for widening conversions. Now, the ascending flow is a byte,char,short,int,long,float,double. Now we need casting for the movement from the right to the left and not vice versa. Here long takes 64 bits and float takes only 32 bits. That being the case for converting a float to a long we should not require casting which is not the case in reality. Why ?
Please do explain.
Regards, Swamy
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that some precision in decimal places may potentially be lost. This is the same reason that a double literal cannot be assigned to a float variable without a cast even if it is within the range of the float type.
The width of the type is not the ONLY criterion for casting or not casting.
Marilyn
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i was interested in the problem and ran a small sample as follows :
/* This program checks typecasting results */
public class TypecastResults
{
public static void main(String args[])
{
byte b = 1;
short s = 2;
int i = 3;
long l = 4L;
float f = 5.0F;
double d = 6.0;


System.out.println( " Byte value : " + b );
System.out.println( " Short value : " + s );
System.out.println( " Integer value : " + i );
System.out.println( " Long value : " + l );
System.out.println( " Float value : " + f );
System.out.println( " Double value : " + d );

// Check what happens when all other types are assigned to byte

/*** Following line does not compile due to loss of precision
b = s;
System.out.println( " Byte value after assigning a short : " + b );
***/
// CANNOT ASSIGN A SHORT TO A BYTE WITHOUT CASTING
b = (byte)s;
System.out.println( " Byte value after assigning a short : " + b );
b = (byte)i;
System.out.println( " Byte value after assigning a integer : " + b );
b = (byte)f;
System.out.println( " Byte value after assigning a float : " + b );
b = (byte)d;
System.out.println( " Byte value after assigning a double : " + b );
// WHAT HAPPENS WHEN AN OUT OF RANGE VALUE IS ASSIGNED ?

s = 1000;
b = (byte)s;
System.out.println( " Byte value after assigning a short greater than 127 : " + b );
}
}
The output :
Byte value : 1
Short value : 2
Integer value : 3
Long value : 4
Float value : 5.0
Double value : 6.0
Byte value after assigning a short : 2
Byte value after assigning a integer : 3
Byte value after assigning a float : 5
Byte value after assigning a double : 6
Byte value after assigning a short greater than 127 : -24

It was indeed an elaborate checking out -- if i typecast to byte, and if i am within range, then i am able to assign short, int, long, float and double to byte type variable. But once i gross the range, it compiles but gives erroneous result.
My question is :
If i assign 1000 (which is a valid short value but out of range for a byte ) i get -24 every time i run the program. Is there any correlation -- like the sequence in the ASCII set or something ?
bye,
Shree
 
Marilyn de Queiroz
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If i assign 1000 (which is a valid short value but out of range for a byte ) i get -24 every time i run the program. Is there any correlation -- like the sequence in the ASCII set or something ?


1000 (decimal) = 1111101000 (binary)
You'll notice that the 8th digit from the right is a 1. That means negative number in byte language. 1101000 (the seven right-most digits) = 104 decimal; and 128 (the maximum if all 8 bits are 1's minus 104 = 24. So, it's negative because the 8th bit is a 1 and it's -24 because that's the way -24 is written in binary. The two left most 1's just get chopped off.
Marilyn
 
Do not set lab on fire. Or this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic