• Post Reply Bookmark Topic Watch Topic
  • New Topic

operator statements  RSS feed

 
femi Joseph
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Golly Seldoon
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tells me to change type of b to Boolean
 
John Joe
Ranch Hand
Posts: 437
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
femi Joseph wrote:I tells me to change type of b to Boolean

Where is b ?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • 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: 15
  • Mark post as helpful
  • send pies
  • 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: 15
  • Mark post as helpful
  • send pies
  • 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
 
Marin Tapnoi
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • 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: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!