Why output is not 311? [duplicate] - java

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.

Related

how to convert OX12 to decimal [duplicate]

This question already has answers here:
Convert hex string to int
(9 answers)
Closed 2 years ago.
I need to convert hexadecimal value which is OX12 to decimal in JAVA,
as I knew hexadecimal is based 16 (1..9, a..f), but in this case, I don't know how to convert with that value.
Can someone help me?
Thanks so much
If the number is a String, you can use the parseInt method of the Integer class with first argument "12" (the number in hex) and second argument 16, the radix, or base, of the number
int number = Integer.parseInt("12", 16);
System.out.println(number);
// output is 18
If the number is not a String, you can simply do the following:
int number = 0x12;
System.out.println(number);
// output is 18

How to round up a floating-point number in Java [duplicate]

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

Java String.format() leading zeros and 1 decimal place [duplicate]

This question already has answers here:
Add leading zeroes to number in Java? [duplicate]
(5 answers)
Closed 4 years ago.
I need a string like 50 to appear as 050.0. I am using String.format, but I can't figure out how to do leading zeros and a single decimal place at the same time. So far, I have tried String.format("%3.2f", number);, but that isn't working as I still get 50.0 rather than 050.0
Use DecimalFormat to control the number of mandatory digits:
DecimalFormat df = new DecimalFormat("#000.0");
System.out.println(df.format(50)); // 050.0
where
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
You can use StringBuilder class to create a string with number 0 and then append it with you number and insert the decimals at the end.
int num = 50; /*Your number*/
StringBuilder s_num = new StringBuilder("0");
s_num.append(num);
s_num.append(".0");
String f_num = s_num.toString();

Java - parse binary to long [duplicate]

This question already has an answer here:
Java 8: Why can't I parse this binary string into a long?
(1 answer)
Closed 6 years ago.
I have a binary represenation of a number and want to convert it to long (I have Java 8)
public class TestLongs {
public static void main(String[] args){
String a = Long.toBinaryString(Long.parseLong("-1")); // 1111111111111111111111111111111111111111111111111111111111111111
System.out.println(a);
System.out.println(Long.parseLong(a, 2));// ??? but Long.parseUnsignedLong(a, 2) works
}
}
This code results in Exception in thread "main" java.lang.NumberFormatException: For input string: "1111111111111111111111111111111111111111111111111111111111111111"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
1111111111111111111111111111111111111111111111111111111111111111
at java.lang.Long.parseLong(Long.java:592)
What is wrong here? Why Long.parseLong(a, 2) doesn't work?
Long.parseLong() doesn't treat the first '1' character as a sign bit, so the number is parsed as 2^64-1, which is too large for long. Long.parseLong() expects input Strings that represent negative numbers to start with '-'.
In order for Long.parseLong(str,2) To return -1, you should pass to it a String that start with '-' and ends with the binary representation of 1 - i.e. Long.parseLong("-1",2).
Eran is right, and the answer to your question is:
System.out.println(new BigInteger(a, 2).longValue());

Meaning of Zero Before Number in Java [duplicate]

This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 7 years ago.
I have a code like this, but I don't know why result variable have false value after execution the code
int x = 234;
boolean result = (x<0250);
and also why the following code doesn't work properly?
System.out.println(0250);
it prints 168 !! why?!
Integer literals starting with 0 are octal (base 8) not decimal (base 10).
Your options are
hexadecimal = 0x0C;
decimal = 12;
octal = 014;
binary = 0b1100;
A number that looks like 0x followed by an integer (using the digits 0-9 and A-F) is a hexadecimal (base 16) number.
A number that looks like 0 followed by an integer (using the digits 0-7) is an octal (base 8) number.

Categories