Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
For example, the class is Car:
public Car(double speed)
But you can initialize it, like:
Car honda = new Car(speed);
I thought double was a decimal?
There are quite a few misconceptions here.
Concerning this:
public Car(double speed) { ... } // A
...
Car honda = new Car(speed); // B
Why can a double value be initialized as a word?
The speed in statement B is not a "word". (In fact, there no "word" type in Java. Perhaps you meant String? But this is not an example of a String either.)
In fact, occurrences of speed in declaration A and statement B are identifiers. And in these contexts, those identifiers must be the names of variables.
(They will be a different speed variables. The speed variable defined in declaration A won't be the one that is used in B. That's OK. In Java, the same name can be used for different things, provided that the names are declared in the respective contexts.)
In fact, new Car(speed) means: "create a Car, passing the value of the speed variable to the constructor".
We cannot see the declaration for the speed variable that is used in statement B is used. However, if the code compiles, the type of that speed variable must be compatible with double. Either the type is double, or it must be convertible to double using one of the conversions that the Java language permits. (It could be an int or a Double for example. But not a String!)
Now we could write this:
Car honda = new Car("speed"); // incorrect !
This attempts to pass the word "speed" (represented as a String) to the Car constructor. Of course it won't work. Java won't attempt to convert a string to a number ... and this string isn't a number anyway.
I thought double was a decimal?
No. It is NOT decimal. Decimal means base 10, and Java double is NOT a base 10 representation.
In fact, double is a primitive binary (base 2) floating point type.
Java does not have any primitive decimal types (either fixed point or floating point) but there is a Java SE class called BigDecimal which is an indefinite precision floating point decimal type.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I investigated a lot about this but I didn't find an answer that convinces me; for that reason I decided to ask it here.
When you do casting, what occurs internally with the compiler? For example:
double b= 5.67
a = (int) b
Does java convert the type of the variable or converts the internal value of?
Normally, the Java compiler will prevent you from assigning primitives that might result in a loss of data. You don't make it clear, but I am assuming that a is an int. Assigning a double to an int can result in information being lost, so the compiler objects.
If you add a cast the compiler ignores the issue and adds code to do whatever is necessary to convert the value from one type to the value of the other (in this case, assuming a is an int, simply truncating the double in many cases) and ignores any data loss. The type of the variables remains unchanged. Only the value changes.
There is a bit more to it than that for certain values. For example, if the double has the special value NaN (not a number) it is converted to int 0. See in the Java Specification 5.1.3. Narrowing Primitive Conversion.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a question regarding converting and loading in java. If you use e.g. long='A'. The Java Virtual Machine Specification at https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-6.html specifies this as a conversion. Here the char is converted to a long.
If, however, a value is in a variable or an array, then Java does not speak any more of converting, but of loading. Here it says that then the array value is loaded and not converted. What I also knew before that if one acts with a variable of another data type that this variable is not converted, but by intermediate steps its information is taken and the information of it is converted, but not the variable. What exactly is the difference between converting and loading as Java describes here: https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-6.html?
JVMS usage of convert
JVMS usage of load
A conversion changes a value from one type to another.
As the documentation for the d2f instruction says, a double value on the top of the stack is converted to a float and pushed back onto the stack as a float.
Loading is moving a value of the same type from one place to another, without any changes to the value.
You are talking about JVM instructions, which is what Java is compiled into.
Java performs conversions as needed, for instance:
double d = 1.0;
float f = (float)d;
will cause a conversion from double to float, presumably implemented using the d2f instruction above.
double[] a = new double[5];
double d = a[0];
will load a value from a cell in the array to the variable d, but no conversion takes place.
float f = (float)a[0];
would perform a conversion.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm not sure if this question has been answered(couldn't find it when I did a google search).
I saw http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html that the math class square root method returns a double. I experimented with it in eclipse with some ints that have whole square roots - 4, 9 and found that the square root with those always returned a floating point value with one decimal - 4.0, 9.0. I was curious as to why it even returned that extra decimal? I thought that ints could be considered as double values too. To me returning just 1 makes more sense cause you conserve more memory(i guess more memory is needed to store that extra decimal point) I even tested it out
public static double control(){
return 1;
}
and saw it was valid to just return 1.
[I] found that [Math.sqrt(x) where x is a perfect square] always returned a floating point value with one decimal.
You are mistaking a particular printed representation of a double value with the value itself. A double does not have a decimal point. A double is a bit pattern that represents a particular real number (4 for example).
Decimal points only appear in a particuular decimal representation of real numbers. If I write "0.25", that obviously has a decimal point. If I write "1/4", there is no decimal point. But those are just two different representations of the same real number. So is the particular bit pattern that represents the double value returned by the Java expression, 1.0/4.0.
I don't know why Double.toString(4) returns the string, "4.0" instead of returning "4", but I'm guessing that somebody wanted to make it consistent with numeric literals in the Java language. When a "4" appears in your program, that's an int literal, and when "4.0" appears in your program, that's a double literal.
That method returns a double. It's going to display as a double because of the return type the method is set to. There is a solution to this here on stackoverflow to return the in if it's say 4.0 and show the double if it isn't. Solution on stackoverflow
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am in a computer science class (11th) for hw i have to do some code following a picture
--------------------Configuration: --------------------
C:\Users\I_SLAY_NOOBS\Desktop\Variables.java:26: error: possible loss of precision
float floatOne = 58.5678;
^
required: float
found: double
1 error
Process completed.
You assign a double which is more precise than a float to a variable declared as float. Float values end with an f in Java. Plain floating point numbers are automatically considered to be doubles...
Either do:
float f = 58.5678f;
or:
double d = 58.5678;
If you truly want a float (single precision IEEE754), you can use:
float floatOne = 58.5678f;
Me, I'd simply go with the double type everywhere since it provides more precision than float:
double doubleOne = 58.5678;
The default for floating point constants in Java (and C and C++, among others) is double precision and you're trying to shoehorn that into a single-precision variable, hence the message. Appending f to a floating point constant tells the compiler that you want it to be single precision.
Unless you have vast arrays of them (and limited space to store them), doubles are generally preferred.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What is the purpose of having methods converting one value into another value of a different type. For example a method that converts a string of digits into an int and another method converting an int into a string of digits? What exactly would be the advantages to doing one over the other? Is it there is no advantage and the methods for conversion exist only to provide compatible values for the arguments of the constructors of different classes?
Simply, it's because those different types exhibit different behaviors:
String str = "42";
System.out.println(str + 1);
System.out.println(Integer.parseInt(str) + 1);
421
43
You need to have methods like Integer.parseInt() if you want to perform normal addition as opposed to string concatenation, for example.
A tangible example of this can come up when you read a number as input from a user; more often than not you will want to treat this number as a number (double, int, etc.) as opposed to a string.
The different forms serve different purposes beyond providing compatible values for methods and constructors.
For the int type, mathematical operations are most easily performed on this type (and similar primitive types). User input is usually given in the form of a String, so to perform a mathematical operation on user input, one must convert it into an int (or a double, long, float, byte, or short as appropriate).
For converting to a String: This is how numerical output is displayed. We may code System.out.println(myInt);, but behind the scenes, Java is converting the number to a String for display purposes.
Since you used the "Java" tag, I'll answer you regarding java.
It is because, Java was written this way . There are languages that do not need types (lisp for example) and you can read a string (from Std. In) and raise it to a power (for example) if it is a number. But Java needs types. The compiler wants to know it ahead.
One useful advantage of being able to convert numbers to string is that you are able to use very large numbers, storing them as strings, instead of integers. However you cannot work with strings when you use the regular operators (+,-,*,/).
Another advantage: when you have a TextField and you read user input, it is given as a string. So you need to get a number out of it to work with it.