• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

primitive types

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:
-----

long x=1;
float f=x;

How is long variable 'x' assigned to float variable 'f' ,since long is holiding 8 bytes and float is only 4 bytes?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arun

It is true that a long is compose of 8 bytes, but if you give an integer value to a long variable, is taken as a literal, so x is taken as an integer (4 bytes) and that is why you can assign x to a float.

If you want x to be taken as a long, you should try this:

public class prueba
{
public static void main(String args[])
{
long x=1.0;
float f=x;
System.out.println("long: "+x);
System.out.println("float: "+f);
}
}

So you get "possible loss of precision"
found : double
required: long
long x=1.0;
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Aiglee Castillo.

But you have told us to get "possible loss of precision." That is not a run-time result, but a compiler error. CR
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and why are you posting this post in the advanced forum?
Look in the JLS, where it tells you about "widening primitive conversions." There is an example of how precision is lost. A long value such as 1234567890123456789 would be converted to a float value something like 1.23457E+18.

Unlike the second example, which throws a compiler error, this conversion can proceed.
 
Aiglee Castillo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell, it's great to be finally here. I have been lurking around here for months and months but today I decided to join


I know that I got a compiler error, I just wanted to show Arun what you get if you really tried to assign a long to a float. If my post wasn't clear, I'm sorry, english is not my first language and it's a bit more difficult if you are trying to explain something else and not just talking.

I thought too that his post wasn't suppose to be in the advance forum and then I realized that it was in the beginner forum too. If I shouln't have answered then I'm sorry and please tell me so, so I don't make the same mistake again.
:roll:
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the beginner forum.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Arun Prasath AV",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. All you'll need to do is to remove the "AV" at the end of your display name.

Thanks!
bear
JavaRanch Sheriff
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted by Aiglee Castillo.

I realized that it was in the beginner forum too.

You were quite right to reply, and I am sorry to have overreacted. But you need to be very clear when replying to beginners' questions; they have lots to learn and not a lot of time to learn it and some of them get confused very easily.

Now this sort of thing will compile happily:-Try it, then put different values in, eg 123456789123456789, 123456789.123456789, and see what happens. Remember both those numbers, having more than 15 digits in, are beyond the range of precision of a double or a float.
CR
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float has less bytes than long but is considered wider.

Compiler won't complain about converting long to float , although some information may be lost.

Opposite conversion must be done with explicit cast.


This has nothing to do with literals.
x is not literal and not complile-time constant, so no special rules for narrowing conversion of compile-time constants can take effect here. And the special rules are only for byte,char,short and int.
[ August 11, 2006: Message edited by: Vlado Zajac ]
reply
    Bookmark Topic Watch Topic
  • New Topic