• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

operators &assigment,

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a doubt in int conversion to
float.

in my program
int x=2;
float y=2.0;
if(x==y) //2==2.0 ,2 promoted to 2.0
System.out.println(x); //2 output

this comparison is valid as comipler converts int(32 bits ) to float (32 bits)
but in System.out.println again ,Is x -converted back to int.as it shows 9 not 2.0 as output??
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think your program should compile.

Because, compiler thinks 2.0 as a double and without explicit conversion you cannot assign a double to a float. The assignment should be as follows


Its true that, in the comparison x's value converts to float but it doesn't change the value of x actually. So the output will be 2 instead of 2.0
[ July 29, 2007: Message edited by: Al Mamun ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posted Today 1:31 AM
--------------------------------------------------------------------------------
i have a doubt in int conversion to
float.

in my program
int x=2;
float y=2.0;

here--------------
float y=2.0;
2.0 is double
it will give error
it should be 2.0f or 2.0F

Again
comparison is only limited to if block
for condition checking.
Inside block there is no again conversion to int
it's oroginally int.
[ July 30, 2007: Message edited by: Vilas Lawande ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear your program will throw exception..

due to float y=2.0; it should be float y=2.0f/F

and if(x==y) - x is int and y is float so internally x will prompted to float but it`s for evaluation only.. after if x will remain 2 not 2.0

so better to go though "type promotion" in java....

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from double to float
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
originally...........

kaushik vira
greenhorn
Member # 142893
posted Today 12:25 PM
--------------------------------------------------------------------------------
Dear your program will throw exception


>>>

it will give compile time error
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic