• 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

operator statements

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will this code successfully execute>

short x = 14;
double y = 30;
float z = 13;

double a = x+y+z;

I am thinking it will bring out an error because the float value does not have  "f" in front of the value.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should work. According to (https://alvinalexander.com/java/java-int-double-float-mixed-type-division-arithmetic-rules) all values in a mixed arithmetic equation are converted to doubles so it should not give you any errors. If it still does not work try adding the f.
 
femi Joseph
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tells me to change type of b to Boolean
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

femi Joseph wrote:I tells me to change type of b to Boolean


Where is b ?
 
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

Golly Seldoon wrote:It should work. According to (https://alvinalexander.com/java/java-int-double-float-mixed-type-division-arithmetic-rules) all values in a mixed arithmetic equation are converted to doubles . . . .

I am afraid that website is mistaken. The type of this sort of expression (line 3) is not double, but float.The correct rules are in the Java® Language Specification (link for multiplicative operators), which includes a link (look at its point 2) telling you clearly that the int is automatically widened to a float. Since the primary rule in Java® about expressions is that you go left to right, the widening conversion (=numeric promotion) only occurs when you reach the wider datatype; in this case the left part of the expression will count as an int and widening to a double only occurs when you reach the * operator:-This discussion includes the same code, and explains why you don't need a letter f in the float declaration.

I would never use a float in real life, unless some bit of code requires one. This constructor is about the only place I can think of that actually requires a float, along with the related constructor for a Set implementation (HashSet).
 
femi Joseph
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Joe wrote:

femi  Joseph wrote:I tells me to change type of b to Boolean


Where is b ?

 f rather . Not be
 
femi Joseph
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

femi Joseph wrote:

John Joe wrote:

femi  Joseph wrote:I tells me to change type of b to Boolean


Where is b ?

 f rather . Not be

sorry. Its float Z and not b or f
 
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

femi Joseph wrote:will this code successfully execute>

short x = 14;
double y = 30;
float z = 13;

double a = x+y+z;

I am thinking it will bring out an error because the float value does not have  "f" in front of the value.



This code (when placed correctly) compiles just fine.

The first one, x, is straightforward.
The second and third, y and z, are first assumed an int, then they get promoted to double and float respectively.
The last line just sum up all the values (14 + 30.0 + 13).
 
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 welcome to the Ranch
reply
    Bookmark Topic Watch Topic
  • New Topic