double d34324.34
doesn't assign a value of 34324.34 but tries to declare a variable with the invalid name d4324.34 .
You probably wanted
double myDouble = 34324.34d;
long myLong = 34324l;
Answer from Denys Séguret on Stack Overflowjava - Double, float and long declaration - Stack Overflow
Java doubles and 0.0
When should I use double and when should I use float for Java?
Print exact value of double
Videos
double d34324.34
doesn't assign a value of 34324.34 but tries to declare a variable with the invalid name d4324.34 .
You probably wanted
double myDouble = 34324.34d;
long myLong = 34324l;
A dot is not a valid character for an identifier (a variable name, a method name, a class name, a parameter name, etc.) It's right in section §3.8 of the Java Language Specification. So this will never work:
double d34324.34; // error
float f3342.34; // error
A valid identifier in Java can only contain letters, numbers, "_" and "$", and it must not start with a number. Notice that this excludes dots.
And why would you want to name a variable with a number? are you sure you didn't mean something like this, instead?:
double d = 34324.34;
float f = 3342.34f;
I am learning Java, and am confuse on when to choose double or float for my real numbers or int. It feels like, it doesn’t matter because from my limited experience (with Java) both of them deliver the same results, but I don’t want to go further down the learning curve with Java and have a bad habit of using either messing up my code, and not having a clue as to why. So, when should you use float and double?
When I do
System.out.printf("%.50f", 0.3);it outputs 0.30000000000000000000000000000000000000000000000000 but this can't be right because double can't store the number 0.3 exactly.
When I do the equivalent in C++
std::cout << std::fixed << std::setprecision(50) << 0.3;
it outputs 0.29999999999999998889776975374843459576368331909180 which makes more sense to me.
My question is whether it's possible to do the same in Java?