Strange value while converting long into int [duplicate] - java

This question already has answers here:
Safely casting long to int in Java
(10 answers)
The range of int in Java
(5 answers)
Closed 1 year ago.
Please, explain to me, why do I get longToInt value == -9 when converting long into int? I understand that my long is higher than max value of int and I expected to get some warning by IDE or some error by compiler, not -9. Thx!
long someLong = 21474836471L;
int longToInt = (int) someLong;
System.out.println(longToInt);

Related

Java casting in wrapper Number classes [duplicate]

This question already has answers here:
Java: Why can't I cast int to Long
(3 answers)
Wrapper classes - why integer literals fail for Long but work for anything smaller
(2 answers)
Closed 1 year ago.
Why it's correct:
Long l = new Long(10);
but it's not correct:
Long l2 = 10;
I understand that int is substituted here, but why is new Long(10) correct?

how to set a variable to 0.001 in JAVA (android) [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
when i want to get the result of 100/100000..
i got only 0.
Example
int one = 100;
int two = 100000;
int result = one/two;
toast(result); //Result is 0
Hey there "int" data type only stores integer values and not the decimals.
So if you divide 3 with 2 you would get 1 as answer instead of 1.5 .
Int just ignores the decimals .
You need to choose float or double data type for this to work.
Your variable named result must be declared and casted to float data type.
Appreciate the effort and mark this as answer if it helps you.....

Why am I am getting an incompatible types error? [duplicate]

This question already has answers here:
Promotion in Java?
(5 answers)
Closed 6 years ago.
I'm learning java and creating some simple test programs with notes in them and I'm getting an error saying "incompatible types: possible lossy conversion from int to short" with short shortVal= val5 + val6; I'm looked and this error meaning I'm trying to put an int value into a short variable but the value I'm storing in the short is only 27, so I'm a bit confused as to what is wrong.
public class test{
public static void main(String[] args){
double val1=4;
float val2=9;
long val3=30;
int val4= 8;
short val5= 15;
short val6=12;
byte val7=20;
short shortVal= val5 + val6; //why the error here?
}
}
The result of short + short is, somewhat paradoxically, int. So you're trying to assign an int to a short variable (shortVal).
short + short will result in an int. You need an int variable to store the result :
int shortVal = val5 + val6;

Convert a string value into an int [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
How to do an Integer.parseInt() for a decimal number?
(10 answers)
Closed 9 years ago.
How to convert string value into int? I am getting number format exception.
String s = "20.00";
int i = (Integer.parseInt(s));
System.out.println(i);
Result should be like i=20.
What about:
int i = (int) Double.parseDouble(s);
Of course, "20.00" is not in a valid integer format.
String s = "20.00";
is not valid Integer value that is the reason its throwing NumberFormatException.
Format your number using either Double or Float then using narrow casting cast you number to int but you may loose precision if exists.
i.e. int I = (int) Double.parseDouble(str);

Java compiler shows error that "integer is too large" [duplicate]

This question already has answers here:
"Integer number too large" error message for 600851475143
(8 answers)
Java long number too large error?
(2 answers)
Closed 10 years ago.
I am writing a code in which I am trying to assign a value in long variable.
But java compiler is showing error that too large integer number.
I am trying to store 600851475143 in long type still.
class Sum {
static public void main(String args[]){
long num=600851475143;
}
}
append 'L' or 'l' at the end of the number to make it a long literal.you can use both lowercase(l)or uppercase(L), but uppercase(L) is recommend for readability.
long num=600851475143L;
An integer literal is of type long if it ends with the letter L or l;
otherwise it is of type int. It is recommended that you use the upper
case letter L because the lower case letter l is hard to distinguish
from the digit 1.
Reference
So use this -
long num=600851475143l;
or better
long num=600851475143L;

Categories

Resources