About Question enthuware.ocajp.i.v7.2.1154 :
Moderator: admin
-
- Posts: 18
- Joined: Fri May 31, 2013 1:18 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1154 :
Is there some easy way to determine what type will a result of mathematical expression be? Like here, because it's divided by 4.0, it returns double. Are there some other simple rules to remember?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1154 :
There can be only four return types in case of numeric - int, long, float, and double.
The type of the expression will be the largest required. i.e. if the largest constituent in long, then the return type will be long.
For example:
long x = 10L;
int p = x/2; //will not work because x/2 is long
long p = x/2.0; //will not work because x/2.0 is now double
The return type of an expression is never a byte, char, short. For example,
byte x = 10;
byte p = x*1; //will not work because x*1 is an int.
HTH,
Paul.
The type of the expression will be the largest required. i.e. if the largest constituent in long, then the return type will be long.
For example:
long x = 10L;
int p = x/2; //will not work because x/2 is long
long p = x/2.0; //will not work because x/2.0 is now double
The return type of an expression is never a byte, char, short. For example,
byte x = 10;
byte p = x*1; //will not work because x*1 is an int.
HTH,
Paul.
Last edited by admin on Wed Jun 25, 2014 7:29 pm, edited 1 time in total.
If you like our products and services, please help us by posting your review here.
-
- Posts: 18
- Joined: Fri May 31, 2013 1:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1154 :
Thank you, finally this is clear. I have not found this anywhere before and yet it is so simple.
-
- Posts: 2
- Joined: Mon Dec 30, 2019 9:48 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1154 :
You say that "There can be only three return types in case of numeric - int, long, float, and double." That is FOUR types, right? How do you know when float will be returned versus when double will be returned?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1154 :
Yes, I should have written four above (corrected). Thanks for pointing it out.
float will be returned when the largest component of an expression is a float, for example:
int a = 1;
float b = 2.0f;
double d = 2.0;
float c = a*b; //valid, a*b will return a float because the larget component of a*b is b, which is a float.
float x = a*d; //invalid because a*d will return a double
Paul.
float will be returned when the largest component of an expression is a float, for example:
int a = 1;
float b = 2.0f;
double d = 2.0;
float c = a*b; //valid, a*b will return a float because the larget component of a*b is b, which is a float.
float x = a*d; //invalid because a*d will return a double
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Mon Dec 30, 2019 9:48 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1154 :
Thank you Paul that is a great explanation.
Who is online
Users browsing this forum: No registered users and 65 guests