I am doing very simple int division and I am getting odd results.
This code prints 2 as expected:
public static void main(String[] args) {
int i = 200;
int hundNum = i / 100;
System.out.println(hundNum);
}
This code prints 1 as not expected:
public static void main(String[] args) {
int i = 0200;
int hundNum = i / 100;
System.out.println(hundNum);
}
What is going on here?
(Windows XP Pro, Java 1.6 running in Eclipse 3.4.1)
The value 0200 is an octal (base 8) constant. It is equal to 128 (decimal).
From Section 3.10.1 of the Java Language Specification:
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.
The value 0200 is an octal, which is 128 in decimal.
For further information, see the literals section of the Primitive Data Types explanation.
Observed an interesting behavior here.
If I do an Integer.parseInt("0200"), I get 200 as o/p.
Howzzat ?!
Related
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.
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.
I tried to compute this as i=i*++i therefore i=56*57 which gives me 3192 but my program says the value is 2162:
class Demo {
public static void main(String args[]) {
short i=056;
i*=++i;
System.out.println(i);
}
}
The problem is before the multiplication - it's here:
short i=056;
That's an octal literal, with decimal value 46. So you're actually getting the results of 46 * 47, which is indeed 2162.
I would strongly advise you not to use code like i *= ++i though. It's simpler for everyone concerned to use i *= i + 1. I'd also advise you not to use octal literals.
I have a problem with remainder operator in java:
Why this:
(int)2147483648l % 10
gives a negative number (-8)?
That's because (int) 2147483648l is -2147483648. You're casting the long to int and it is out of bounds.
Casting problem .Data loss due to narrowing. You are converting long to int.
Read more about conversion.
From JLS
The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.
from Narrowing Primitive Conversions JLS 5.1.3
So 2147483648l will cast into int that will be -2147483648 and then based on (a/b)*b+(a%b) = a
Value should be -8 what you are getting.
use "long" in place of "int".
you can use it without typecasting also
Following Example might be useful:
public class Example1
{
public static void main(String args[])
{
int b = (int)2147483648l;
System.out.println("Value of b: "+ b);
System.out.println("Output1: "+b % 10);
long a = 2147483648l;
System.out.println("Value of a: "+ a);
System.out.println("Output2: "+ a % 10);
}
}
output
Value of b: -2147483648
Output1: -8
Value of a: 2147483648
Output2: 8
You're getting a negative number because you're converting a long to an int. A possible work around in your case simply takes advantage of the fact that any decimal x mod 10 is simply the digit in the lowest decimal place (in the ones place). For example, 156 mod 10 is 6, because 156 divided by 10 is 15 + (6/10). So you could do something like this
//get the number and make it a string
String numberAsString = String.valueOf(number);
//get the integer value of the last character in the string (basically the lowest place)
int mod10 = Integer.parseInt(numberAsString.charAt(numberAsString.length() - 1));
This works for any integer number as long as what you want is number % 10
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...