• 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

SJCP

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d1=-1.0;
d2=0;
byte b=0;
b=(byte)d1/d2;
System.out.println(b);

the above code prints 0 why? i would like to have a breif description......
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result of d1/d2 will be double since d1 is already double. Since d2 is 0, you get -INF (a manifest constant NEGATIVE_INFINITY). The manifest constants are defined in java.lang.math package.
NEGATIVE_INFINITY is represented as (0xfff0000000000000)
When this bit pattern is converted into byte, all higher order bits are ignored and you get zero as result.
For your information, POSITIVE_INFINITY is represented as 0x7ff0000000000000 and NaN is represented as 0x7ff8000000000000
 
anilkuj
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u ramesh that was very much clear but still i am confused about the no. of bits in the rep. could u explain over it. I am giving my scjp this 29th could u give me any useful url's although i have a good collection of them are there any url's which are not found in common but very useful
thanx
 
anilkuj
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! ramesh
class test4
{
public static void main(String args[])
{
double d1=1;
double d2=0.0;
d1=d1/d2;
System.out.println("d1:"+(int)d1);
byte b=0;
b=(byte)d1;
System.out.println(b);
}
}

then according to you even this should give 0 but it isn't it's giving -1 why???
 
This tiny ad will self destruct in five seconds.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!