This question already has answers here:
How does a leading zero change a numeric literal in Java?
(3 answers)
Closed 7 years ago.
Here is some code in java on datatypes:
class Test
{
public static void main(String args[])
{
int i = -0777;
System.out.println(i);
}
}
The output of the above code is -511
If the code is changed to :
class Test
{
public static void main(String args[])
{
int i = -777;
System.out.println(i);
}
}
The output is -777.
Why is the output differing??? What are the calculations done behind this code???
-0777 is treated by the compiler as an octal number (base 8) whose decimal value is -511 (-(64*7+8*7+7)). -777 is a decimal number.
Related
This question already has answers here:
how to set value of octal in java?
(9 answers)
Closed 2 years ago.
public class Main
{
public static void main(String[] args) {
char a='3';
int b=011;
System.out.println(a+b);
}
}
Output is 60
can someone explain why java behaves like this ?
Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1. To define integer literals as octal value in Java is effortless.
This question already has answers here:
Java Round up Any Number
(7 answers)
Closed 3 years ago.
public class MyClass {
public static void main(String args[]) {
double x=120.38;
System.out.println(Math.round(x));
}
}
Output: 120
But I want an output of 121
Rounding off any decimal values to 1 whole number
Replace Math.round with Math.ceil
Try to use : Math.ceil(x)
It will works for you
This question already has answers here:
"Integer too large" for a small compile time constant
(4 answers)
Closed 5 years ago.
when I add integer number it's show me "integer too large" even if I make it double howto solve this
public class Three {
public static void main(String[] args) {
int i = 05955555;
}
Try this instead:
int i = 5955555;
In Java, an integer number starting with 0 is interpreted as being in octal base - and in that base, you can't have the digit 9.
This question already has answers here:
Java Integer parseInt error
(4 answers)
Closed 6 years ago.
I need to convert the string of hex to decimal in java..
My hex value is "00000156A56BE980".
My required output is 1471654128000
I have done the following in java,
public static void main (String args [])
{
String hexValue = " 00000156A56BE980 ";
Integer result = Integer.parseInt(hexValue, 16);
System.out.println(result);
}
but I am getting the following error,
Number format Exception for input string "00000156A56BE980"
I have tried by giving long also the same error coming.. For other hex value it's coming but when we give hex string of larger value it shows the error.
How can we convert this number to decimal?
Can anyone solve this issue for me?
Try it like so
import java.math.*;
class Main {
public static void main (String args [])
{
String hexValue = "00000156A56BE980";
BigInteger result = new BigInteger(hexValue, 16);
System.out.println(result);
}
}
See also this repl.it
The problem is probably because your value does not fit within the value range (-231 to 232-1) of Integer - see the docs
The number is too large for a 32-bit int
Try using a long instead.
public static void main(String[] args) {
String hexValue = "00000156A56BE980";
long result = Long.parseLong(hexValue, 16);
System.out.println(result);
}
Note: you can't have spaces in a number. You can call .trim() to remove them.
2 thing in order the code can work:
remove spaces trimming the String
the result doenst fit in an integer so use either Long or BigInteger instead
public static void main(String[] args) {
String hexValue = " 00000156A56BE980 ";
long result = Long.parseLong(hexValue.trim(), 16);
System.out.println(result);
}
The number is too big for integer (> 2^32).
(The value represented by the string is not a value of type int.)
Take a look here
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
String a = "a*3";
a = a.replace("a","45");
// a contains 45*3
How do you I convert that to a DOUBLE which outputs 135.00 ??
Note : a can have any arthimetic operation... ( + , - , /)
Simplest solution is to pass this off to Java's built in JavaScript engine.
public class Eval {
public static void main(String[] args) throws ScriptException {
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
engine.put("a", 45);
Number val = (Number) engine.eval("a*3");
System.out.println(val.doubleValue());
}
}