how to set value of octal in java? - java

I am trying to write following code.but it gives me error kindly help me.
int six=06;
int seven=07;
int abc=018;
int nine=011;
System.out.println("Octal 011 ="+nine);
System.out.println("octal O18 =" + abc);
why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable.
Why this happen? what's the reason behind this Kindly tell me.
I got Following error
integer number too large: 018
int eight=018;

Octal is base-8 number system, so it means digit can be from 0 to 7, you can't use digit 8 (and 9 too) in octal number system.

// Decimal declaration and possible chars are [0-9]
int decimal = 495;
// HexaDecimal declaration starts with 0X or 0x and possible chars are [0-9A-Fa-f]
int hexa = 0X1EF;
// Octal declaration starts with 0 and possible chars are [0-7]
int octal = 0757;
// Binary representation starts with 0B or 0b and possible chars are [0-1]
int binary = 0b111101111;
If the number is string format then you can convert it into int using the below
String text = "0b111101111";
int value = text.toLowerCase().startsWith("0b") ? Integer.parseInt(text.substring(2), 2)
: Integer.decode(text);

why i cant give 018 and 019 to variable.
Because an integer literal prefixed with 0 is treated as octal, and '8' and '9' aren't valid octal digits.
From section 3.10.1 of the 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.
Trying to use '8' in an octal number is like trying to use 'G' in hex... it's simple not part of the set of symbols used in that base.

Octal numbers (base 8) can only use the following figures: 01234567. The same way that decimal numbers (base 10) can only use 0123456789.
So in octal representation, 17 + 1 is 20.

The prefix 0 indicates octal(8 base)(digits 0-7).
public class MainClass{
public static void main(String[] argv){
int intValue = 034; // 28 in decimal
int six = 06; // Equal to decimal 6
int seven = 07; // Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011; // Equal to decimal 9
System.out.println("Octal 010 = " + eight);
}
}

why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable.
The leading zero signifies an octal literal. However, 8 and 9 are not valid octal digits. This makes 018 and 019 invalid.

When an integer literal starts with 0 in Java, it's assumed to be in octal notation. The digits 8 and 9 are illegal in octal—the digits can range only between 0 and 7.

Because it's octal, an octal number has 8 digits which spans from 0 to 7 inclusive. For the same reason 12 would be an invalid binary number.
You need at least base 9 to have 18 and a normal decimal base for 19.

For your query.....you assign an invalid value to the variable....your assigned value is started with 0(zero) ..which means that you are assigning an octal value to the variable and when you assign a value higher then 7 such as 018 in your case...the value excedes the range of octal variables and hence show an error...so try entering simply 18 so it would take it as an integer rather than an octal variable data type...

Related

Convert int which start with leading zero to string

I'm trying to convert int's which start with 0 to strings to be stored in a phone directory as the telephone numbers can start with 0.
I've tried -
int num = 0125;
String.format("%04d",num);
and
Integer.toString(num);
and
DecimalFormat df = new DecimalFormat("0000");
df.format(num);
Each time I get the output 0085 rather than 0125.
How do I convert an int with a leading zero to a string in decimal format?
An int value starting with a zero is considered to be a octal number (having numbers from 0 - 7) similar to hexadecimal numbers. Hence your value:
0125
is equal to: 1 * 82 + 2 * 81 + 5 * 80 == 64 + 16 + 5 == 85
Don't try to represent a phone-number as an int. Instead use a String and validate it using a regex expression. If you combine both, you may as well represent a phone number by its own type like:
public class PhoneNumber {
private final String number;
public PhoneNumber(String number) {
if (number == null || !number.matches("\\d+([-]\\d+)?")) {
throw new .....
}
this.number = number;
}
}
The regex is just an example matching phone numbers like: 1234 or 0123-45678.
A numeric literal that starts with 0 is considered to be Octal (base 8). 125 base 8 is 85 base 10 (decimal).
Also, int i = 09 will throw a compiler error for the same reason.
See 09 is not recognized where as 9 is recognized
0125 is actually 85. Why?
Numbers that starts with 0, are octal numbers. So 0125 is:
5*80 + 2*81 + 1*82 = 85
See the JLS - 3.10.1. Integer Literals:
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.

3*012 = 30 and not 36. Why is that? [duplicate]

This question already has answers here:
why is not (123 == 0123) in java?
(3 answers)
Closed 9 years ago.
I am confused why this is and I cannot seem to find an answer why. This is from the assignment:
x=1, y=2, z=3;
z=(int)(x/y*3.0+z*012);
System.out.printf("%d %d %d", x, y, z);
Answer is :
1 2 30; << from eclipse
How I arrived here:
(1/2) = 0 * 3.0 = 0 + (z*012)= 30. I wanted to say 36 but I guess it is 30 according to the IDE.
012 is octal number not decimal which decimal value is 10.
z=(int)(x/y*3.0+z*012);
is equals -
z=(int)(1/2*3.0+3*10);
For reference
Numeric starts with 0 is octal number.
Numeric starts with 0x is hexadecimal number.
Numeric starts with 0b or OB is binary number.(Since Java edition 7 - Binary Literals)
In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity. Here 012 is an octal number which has a decimal value f 10
So your multiplication will come like
z = (int) (1/2 * 3.0 + 3 * 10);
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.
012 is an octal, because it starts with 0:
012 = (0 * 8^2) + (1 * 8^1) + (2) = 10
Therefore:
012 * 3 = 10 * 3 = 30
Notes:
Remember that an octal is a number in base 8 (decimal is base 10), so it can't have digits larger or equal to 8.
Similarly, hexadecimal numbers starts with 0x, for example: 0x12 = 1*16 + 2 = 18
See the 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.
So,
012 = 0 * 82 + 1 * 81 + 2 * 80 = 10
In Java 7, you can use underscores in numeric literals which might help you interrupting the value.

how the value changes dynamically

I have compiled the following code,
import java.lang.*;
public class Test
{
public static void main(String[] args)
{
int x=010;
System.out.println("x(010):="+x);
}
}
After compiling the code, i got the value 8 but the actual value of x is 10, how it happens to print the value 8, could you please someone explain me the reason.
It starts with a 0, so it's in octal notation.
Actually, the value of x is 1*8^1 + 0*8^0 = 8.
As the JLS states:
An octal numeral consists of an ASCII digit 0 followed by one or more
of the ASCII digits 0 through 7
OctalNumeral:
0 OctalDigits
int x = 010 the prefix "0" in the value of x made the compiler treat it as an octal value.
In java if you start with 0 then you are telling that the number is in octal.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1
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.

Clarification about "int" number that begins with 0

public class Test {
public static void main(String[] args) {
int i = 012;
System.out.println(i);
}
}
Why the output is : 10?
If a number starts with 0 it's an octal number with base 8.
012 is in decimal a 10
See the 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.
It's a good practice to write:
int i = 0_12;
It might be clearer now, i in decimal is 2*80 + 1*81 = 10.
Octal Number :
Any number start with 0 is considered as an octal number (012) i.e. base-8 number system
Simple octal number evaluation :
1*8^1 + 2*8^0 = 10
Octal Number
For More information about Number System
012 is the octal value for 10 in decimal. So your telling java to print the integer at octal pos 012. Here: http://www.asciitable.com/ shows octal to decimal vaulue conversions.

why is not (123 == 0123) in java?

I am developing an application in Android using Eclipse. I wrote the following code and in tests the first and third "if" block is not reachable. Why?
When I add a leading zero to a number, the equal operator returns false.
int var = 123;
if (var == 0123) {
//not reachable
}
if (var == 123) {
//reachable
}
if (var == (int)0123) {
//not reachable
}
if (var == (int)123) {
//reachable
}
0123 is an octal number (leading 0), while 123 is a decimal number.
so 0123 actually equals to 83.
Any integer Number Leading With Zero is octal Number (base 8).
0123 is octal Number and 123 is Decimal Number
0123 = (3*8^0) +(2*8^1)+(1*8^2)+(0*8^4)
=3+16+64+0
=83
because 0123 in not decimal digit its octal (base 8)
so this is equal to 83
To convert a number k to decimal, use the formula that defines its base-8 representation:
0123 base-8 = 83 decimal
0123 = (3*8^0) +(2*8^1)+(1*8^2)+(0*8^4)
=3+16+64+0
=83
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.
Note: Octal values are denoted in java by leading zero normal decimal number cannot have a leading zero

Categories