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.
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 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 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 2 years ago.
Improve this question
when i try to get the longValueExact() :
BigDecimal bigDecimal = new BigDecimal(432.900).divide(new BigDecimal(1), 2, RoundingMode.FLOOR);
System.out.println(bigDecimal.longValueExact());
Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4151)
at java.math.BigDecimal.needIncrement(BigDecimal.java:4207)
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4115)
at java.math.BigDecimal.setScale(BigDecimal.java:2455)
at java.math.BigDecimal.longValueExact(BigDecimal.java:3093)
at com.tessi.bmd.specific.actil.utils.ActilUtils.main(ActilUtils.java:1281)
new BigDecimal(432.900)
This is a bad idea. 432.900 is a double literal and is therefore highly unlikely to actually represent 432.900. You're using BigDecimal, so presumably you know that there are only at most 2^64 numbers in existence that are exactly representable by a double. 432.900 is not one of them. Do not use this constructor - it has warnings all over it. Use new BigDecimal("432.9").
.divide(new BigDecimal(1),
Okay, divide by 1, not going to do anything. Also, use BigDecimal.ONE for this.
The value is still 432.899999999999434 or whatnot.
System.out.println(bigDecimal.longValueExact());
Of course that doesn't work - a long value can only hold integral values, and 432.9 (or something close to that) isn't.
Are you perhaps thinking that 432.900 is just a way of writing 432900 that is more readable to humans from certain locales where . is used as thousands separator?
. is the decimals separator. 432.900 is a double literal that represents the nearest representable double to the number 432 + 9/10ths. If that's your intend, remove the . - if you want to create some horizontal space for the yes, use _ which is legal in number literals and meaningless.
If that's not your problem and you really want 432.9 as an exact long - I guess, go back to square one and start learning java. Soon (as in, within a day or two, no doubt) you'll hit the part of the tutorial that explains the primitive data types. Pay extra attention to this section.
It is the normal behavior because the value you want to display as a long has decimal part (432.89, not 432.90 due to instatiating BigDecimal from a double) so is not an interger number. From the javadoc:
Converts this BigDecimal to a long, checking for lost information. If
this BigDecimal has a nonzero fractional part or is out of the
possible range for a long result then an ArithmeticException is
thrown.
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 5 years ago.
Improve this question
class Pattern4 {
public static void main(String args[]) {
for(int i=1;i<=5;i++) {
for(int j=1;j<=5;j++) {
System.out.print((char)(i+64));
}
System.out.println();
}
}
}
In this program if I don't provide () to char keyword, I am getting a compile time error. So my question is why is it necessary to write (char) and then (i+64) and why not char(i+64)?
(i+64)
is an int, because it's the sum of an int and an int.
If you want to print it as an int, you don't have to do anything.
System.out.println(i + 64);
If you want to print it as a char, you have to convert it to one:
System.out.println((char)(i + 64));
That's simply the syntax Java uses for casting.
Because char(65) is method call syntax. You're not calling a method named char with 65 as a parameter, you're casting 65 to a char.
Remember that, unlike spoken languages, programming languages are carefully designed to avoid ambiguity of any kind. This is both for "user friendliness" (i.e. to make the code clearer) and from the practical necessity of writing compilers and creating correct software. Anything that would introduce ambiguity could also "break" compilers and cause subtle, difficult-to-track bugs.
(char)int is syntax to type cast. This syntax does not take any input and its as per language design (Its not a function but language syntax). This tells compiler to accept the type as char and not as int. Java is strongly typed language and it ensures type safety.
We need to tell compiler explicitly when we need to change type otherwise compiler will complain.
Hope this clarifies why this does not take input in parenthesis like:
abc(input);
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.
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.