This question already has answers here:
How does a leading zero change a numeric literal in Java?
(3 answers)
Closed 7 years ago.
How does this snippet of code prints "-511" as the output on the console?
class Test
{
public static void main(String[] args) {
int i = -0777;
System.out.printf("%d",i);
}
}
Is it to do with the way Java stores Negative numbers?
Integer numbers prefixed with 0 are octal numbers. To use decimal numbers remove the 0 prefix:
int i = -777;
Numbers starting with 0 are treated as being in octal by Java. -077 is equivalent to -63, which is what I get when I run your program.
When a number in java code starts with a 0, it interprets it as octal format
Related
This question already has answers here:
What is the radix parameter in Java, and how does it work?
(6 answers)
Closed 4 years ago.
In java when I was working on Netbeans Ide I saw method of nextInt(int radix). I tried to implement this and passed an integer number, but that gave an exception of InputMismatchException. My code is as follows:
import java.util.*;
import java.lang.*;
public class HelloWorld{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(2);
System.out.println(n);
}
I want know that what is radix argument? Also,what is its use?
The radix is the base in which the number will be interpreted. You can use it to make the nextInt method interpret the number as non-decimal.
For example, the following expects a number in binary:
s.nextInt(2)
> 11111 //input
31 //result in decimal
And this expects a hex number:
s.nextInt(16)
> abc12 //input
703506 //result in decimal
You'll also be interested to see other methods using a radix, such as Integer.toString(int, int), Integer.parseInt(String, int), which format/parse numbers in a specified radix (similar methods are provided by Long, Short, etc)
This question already has answers here:
Integer to two digits hex in Java
(7 answers)
Closed 1 year ago.
I'm trying to pad a hexadecimal digit with 0's in the beginning so that the length of it is always 4. For example, the FF is going to be padded as 00FF. I have tried to use String.format("%04d", number) but it didn't work as my hexadecimal digit is actually a string.
I have also tried using StringUtils.leftPad() but for some reason, IntelliJ couldn't detect it.
Please note my count is going to change, but I haven't represented it here as I'm yet to implement it. This is my sample code. Thanks.
public static void main(String[] args) {
int count = 0;
String orderID = Integer.toHexString(count).toUpperCase();
System.out.println(String.format("%04d", Integer.valueOf(orderID)));
}
String.format("%04x", 255)
with output
00ff
(or use X for uppercases hex digits)
As #user16320675 point, more info about Formatter here.
This question already has answers here:
long value with 0 on left
(5 answers)
Closed 5 years ago.
class LetsComp {
public static void main(String[] args) {
int a = 10, b = 0010;
System.out.println(a == b); // this gives false, even if both values in actual are same
}
}
In java 10 and 0010 are not the same.
0010 is in octal equivalent to 8 (in decimal), while 10 is already in decimal format.
From JLS:
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
how to generate a long that is in the range of [0000,9999] (inclusive) by using the random class in java? The long has to be 4 digits.
If you want to generate a number from range [0, 9999], you would use random.nextInt(10000).
Adding leading zeros is just formatting:
String id = String.format("%04d", random.nextInt(10000));
A Java int will never have leading 0(s). You'll need a String for that. You could use String.format(String, Object...) or (PrintStream.printf(String, Object...)) like
Random rand = new Random();
System.out.printf("%04d%n", rand.nextInt(10000));
The format String %04d is for 0 filled 4 digits. And Random.nextInt(int) will be [0,10000) or [0,9999].
This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 7 years ago.
public static void main(String str[]){// String type
System.out.println("Main()");
main(0100);}
public static void main(int a){//integer type
System.out.println(a);
}
answer should be 100 rather 64
In Java, if you put a 0 before a primitive literal, it is interpretted as octal. 100 in octal is 64 in decimal.