Difference between converting and loading in java? [closed] - java

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.

Related

Casting primitive types [closed]

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.

Why can a double value be initialized as a word? [closed]

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.

What does it do when a number is followed by a letter? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What significance does a letter after a number have? For example the bellow table for primitive types in Java has letters after the default values of long float and double.
I tested and as far as I can tell they never make a difference. I've also seen things like this in C and C++, for example how the NULL macro sometimes expands to 0L.
It does make a difference. It just depends on what your values are.
First and foremost, Java will treat all integral declarations as an int unless you specify the L (or l) suffix.
This means that, while this declaration is invalid:
System.out.println(5_000_000_000_000); // too large for an int
...this declaration would be:
System.out.println(5_000_000_000_000L);
Java will also treat all floating-point declarations as a double unless you specify f or F. You could also specify d or D, but this is an optional and implied declaration that your literal type is a double.
Another example: while this declaration is valid for a double:
System.out.println(1.17e200);
...this one isn't:
float f = 1.17e200f; // too large
The behavior for other languages (C, C++) would be specific to which standard you're using, but it's not quite what you're thinking - a macro is simply a pre-compiler text replace, so wherever the compiler sees NULL, it would replace it with 0.

Java error "possible loss of precision" [closed]

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.

Rationale behind methods that convert values [closed]

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.

Categories

Resources