Hi again!
I've been playing a little today with Java, C and C++ and I've found something ratter interesting. Though, I could not find a reason for why this is happening.
Those 4 programs bellow (two in java one in C and the other one in C++) does exactly the very same simple thing. They will get a float value, convert it explicitly into an integer and the value (will be 10) will call an if-else case.
The thing is, with both C and C++ it works, but the one with Java does not. Do make it work, I've to use double instead of float. Question is... why? Since the float is a very small number (10.3) and should fit into float's bounds. Here they come, first in C:
Code:
// prog1.c
// Program show a simple float-to-integer explicit converting.
#include <stdios.h>
int main()
{
// Declare variables
float someFloat = 10.3;
int someInt = (int) someFloat; // here will convert float to int
if ( someInt > 10 ){
printf("%s", "someInt is bigger then 10");
} else if ( someInt == 10 )
printf("%s", "someInt is exactly 10");
} else {
printf("%s", "someInt is smaller then 10");
}
return 0;
}
Now in C++
Code:
// prog1.cpp
// Program shows a simple float-to-integer explicity converting
#include <iostream>
using namespace std;
int main()
{
// Declaring variables
float someFloat = 10.3;
int someInt = (int)someFloat; // converting int to float...
if ( someInt > 10 ) {
cout << " someInt is bigger then 10\n ";
} else if ( someInt == 10 ) {
cout << " someInt is exactly 10\n";
} else {
cout << " someInt is smaller then 10\n";
}
return 0;
}
As you can see, this is pretty possible in both C and C++. Now watch Java code closely. The following program will not compile and return error... saying that double was found, but expecting a float (?)
Code:
class prog1
{
public static void main ( String[] arguments )
{
// Declaring stuff
float someFloat = 10.3;
int someInt = (int)someFloat; // converting...
if ( someInt > 10 ) {
System.out.println ( "someInt is bigger then 10");
} else if ( someInt == 10 ) {
System.out.println ("someInt is exactly 10");
} else {
System.out.println ("someInt is smaller than 10");
}
}
}
As I said above, this program will not compile. But the next one will...
Code:
class prog1
{
public static void main ( String[] arguments )
{
// Declaring stuff
double someDouble = 10.3;
int someInt = (int) someDouble;
if ( someInt > 10 ) {
System.out.println ( "someInt is bigger then 10");
} else if ( someInt == 10 ) {
System.out.println ("someInt is exactly 10");
} else {
System.out.println ("someInt is smaller than 10");
}
}
}
I know I know... they are all hard-coded as well
. I just wanted to know why Java won't accept to convert float to integer since 10.3 is a pretty small number still under float bounds, but does, accept it as being double? Any ideas?
Thanks in advance
P.S: I've not copied-paste the code above. I've just wrote it here at the forum (I'm on win now and I've no compilers at all in this machine) so there might be some errors above
.
Happy New Year Everybody!!!