• Post Reply Bookmark Topic Watch Topic
  • New Topic

primitive types  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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?
 
Aiglee Castillo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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;
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • 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: 55772
163
  • Mark post as helpful
  • send pies
  • 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
  • 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:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the beginner forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 55772
163
  • Mark post as helpful
  • send pies
  • 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
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!