Values stored in long variable is not as expected [duplicate] - java

This question already has answers here:
Why do these two multiplication operations give different results?
(2 answers)
Closed 6 years ago.
I am confused how it's happening. Output is not as expected.
public class Test2 {
public static void main(String arg[]){
int interval = 43200;
long tempInterval = interval * 60000;
System.out.println(tempInterval);
}}
Expected output is 2592000000 but I'm getting -1702967296. It might be naive
question but I'm stuck with this.

Add a L after 60000.
Java assumes that you're using int multiplication, which causes an int overflow before it's casted to a long.

Related

TypeCasting from double to long in JAVA [duplicate]

This question already has answers here:
Java Long primitive type maximum limit [duplicate]
(4 answers)
Closed 6 years ago.
class test{
public static void main(String... args){
double d=5.637;
System.out.println("double is:"+d);
long a=(long)d;
System.out.println("long is:"+a);
}
}
The output of above code is long is : 5, which is as expected.
But when I ran the following code:
class test{
public static void main(String... args){
double d=12345678901234567890.637;
System.out.println("double is:"+d);
long a=(long)d;
System.out.println("long is:"+a);
}
}
The output is not as expected. the the result is long is:9223372036854775807
I want to ask why does it happen when I take huge number in double.
for safest casting you should use Math.round(d) and max long size is 9,223,372,036,854,775,807 you are trying to assing bigger double value to long
edit: you can use BigInteger instead of Long

having trouble creating a rounding program java using command lines [duplicate]

This question already has answers here:
Variable might not have been initialized error
(12 answers)
Closed 6 years ago.
I'm fairly new to coding and I've been learning by creating simple programs.
I'm trying to create a program called roundGrade to round a grade to one decimal place by calling onto the command line.
The error stated:
Error: variable roundGrade might not have been initialized
Here's the code I've written so far:
public static String roundGrade(double grade){
String roundGrade;
double R = Double.parseDouble(roundGrade);
R = Math.round(grade*10)/10;
roundGrade = Double.toString(R);
return roundGrade;
}
You are attempting to parse roundGrade before you set it to anything (and for no apparent purpose). This
double R = Double.parseDouble(roundGrade);
R = Math.round(grade*10)/10;
should be something like
double R = Math.round(grade*10)/10;
And your entire method could be
return String.format("%.1f", grade);

Arrays.toString returning odd result (Appear to be memory addresses) [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 7 years ago.
I'm trying to create this two dimensional array in java and print the output, but when it outputs I am not getting the numbers or a two-dimensional structure. Java appears to be giving me what looks like memory addresses for each array result.
My code
public static int[][] generateSampleTable(int smallest, int largest, int sampleSize)
{
int sampleTable[][] = new int[sampleSize][2];
for (int i = 0; i < sampleSize; i++)
{
sampleTable[i][0] = 5150;
sampleTable[i][1] = 2738;
}
return sampleTable;
}
public static void main(String[] args){
System.out.println(Arrays.toString(generateSampleTable(1,1,10)));
}
My output appears like this:
[[I#803816, [I#22b4b7, [I#1079ed1, [I#231c5d, [I#178cf47, [I#d65c13, [I#1fca60e, [I#1f99518, [I#fa7e97, [I#90925f]
Ignore the smallest and largest arguments in that method as I haven't implemented the calculations for them yet and am not using them right now.
Other than what you see here, I guess I should tell you that yes, I imported java.util.Arrays
Not sure what is going on. Any help would be appreciated.
Thanks
Arrays.toString() knows how to print 1 dimensional arrays. For 2-dimensional arrays you should use Arrays.deepToString().

Double number returns stackoverflow [duplicate]

This question already has answers here:
What is a StackOverflowError?
(16 answers)
Closed 8 years ago.
The highest number that double can represent is extremely high, I thought.
Though following code throws an exception. It's actually my full code.
public class Summe {
public static void main(String[] args) {
System.out.println(summe(20000));
}
public static double summe(double s) {
return s == 0 ? s : s + summe(s-1);
}
}
Thanks for the answers so far. My question is: How can I make my code work?
The problem here isn't the size of number a double can hold - the problem is the size of the stack. Here, you have 20K nested call to summe, which is way too much for the stack to handle, and hence, it overflows. If s were an int instead of a double, you'd have the exact same problem.
You made too many recursive call to summe.
You should read this question carefully to get a full explanation : What is a StackOverflowError?

int A does not equal int B [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have two integers that I want to compare. The first integer is created from a byte of utf-8 and the second integer is the one I want to check to see if it equals.
int a = 106;
int b = (int)byteArray[0]; //which actually equals 106 when you printstatement it
but....
(a==b) //always equals false
int i = 0; While(b != i) { println(i); i++;} //eventually escapes the loop and is true
Are primitives also referenced when created? And why does a never equal b unless I count all the way up to 106?
Is there a better way to compare bytes? because I've tried all forms of variables and they do not work either.
The problem is somewhere else in your code (in the part that you are not showing). This is why you are being suggested to provide an SSCCE.
The following works as expected (i.e. prints true):
public class Test
{
public static void main(String[] args) throws Exception
{
byte[] byteArray = new byte[] { 106 };
int a = 106;
int b = (int) byteArray[0];
if (a == b)
System.out.println("true");
}
}
Most probably, in your code byteArray[0] does NOT contain 106. An SSCCE would show this.

Categories