简体   繁体   English

为什么java和c#在简单的添加方面有所不同

[英]why does java and c# differ in simple Addition

I have two snippets, one in Java and one in c# . 我有两个片段,一个用Java编写,一个用c#编写

float a = 1234e-3f;
float b = 1.23f;
float ca = 1.234e3f;
float d = 43.21f;
long e = 1234L;
int f = 0xa;
int g = 014;
char h = 'Z';
char ia = ' ';


byte j = 123;
short k = 4321;

System.out.println(a+b+ca+d+e+f+g+h+ia+j+k);

the Java snippet returns 7101.674 Java代码段返回7101.674

and in c# 并在c#

float a = 1234e-3f;
float b = 1.23f;
float ca = 1.234e3f;
float d = 43.21f;
long e = 1234L;
int f = 0xa;
int g = 014;
char h = 'Z';
char ia = ' ';


byte j = 123;
short k = 4321;

Console.WriteLine(a+b+ca+d+e+f+g+h+ia+j+k);

produces a result of 7103.674 . 得到7103.674的结果。

why am I off by 2 and what is correct? 为什么我关闭2,什么是正确的?

The difference is in the 区别在于

int g = 014;

It's Octal in case of Java ( 014 == 12 ) and Decimal in case of C# ( 014 == 14 ). 对于Java( 014 == 12 ),它是八进制 ,对于C#( 014 == 14 ),它是十进制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM