思归呓语

衣带渐宽终不悔,为伊消得人憔悴
随笔 - 413, 评论 - 3007, 引用 - 245

导航

关于

标签

每月存档

最新留言

  • re:xUnit.net
    <p>collection of woman's dresses and casual dresses on this site <a href="http://www.o...
    by dress(注册) on 2010/9/1 17:35:51
  • reply
    Free time is a great stuff but not any student is able to use that. Naturally college students stay ...
    by Kerr30SYBIL(注册) on 2010/8/27 23:51:37
  • re:【翻译】Entity Framework 特性CTP全程示范: Code Only
    <p> <p>Welcome to fashion goods online store,</p> <p>[b][url=http://www.chan...
    by chicken24(注册) on 2010/8/27 17:59:53
  • re:《C#与.NET 3.0高级程序设计(特别版)》
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:11:15
  • re:【翻译】实体框架中的POCO支持 - 第三部分 - POCO的变动跟踪
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:08:14
  • re:【翻译】Entity Framework 特性CTP全程示范: Code Only
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:08:06
  • re:【翻译】Code Only增强
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:07:59
  • re:站在巨人的肩膀上
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:07:48
  • re:Oslo
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:01:44
  • re:Oslo
    <p><span style="font-family: Simsun; font-size: medium;"><a href="http...
    by h3300723(注册) on 2010/8/24 13:01:28
  • reply this post
    The superb data referring to this topic must be free for people, because they demand <a href=&quo...
    by ClariceFlowers27(注册) on 2010/6/20 4:43:26
  • answer this topic
    Are you willing to get <a href="http://www.prime-resume.com">buy resume</a>, ...
    by BeverleyCopeland20(注册) on 2010/6/18 19:35:15
  • respond this topic
    I really loved the post. Although specially I think authority is satisfactory . As long as it does...
    by HodgesLara(注册) on 2010/6/18 3:46:16
  • respond this post
    People have an opportunity order the buy research paper and <a href="http://www.gogetessays....
    by FINCHMorgan(注册) on 2010/6/17 19:39:43
  • answer this topic
    Different persons should discus often about your superior article. I guess that, the best <a href...
    by AlisaEmerson25(注册) on 2010/6/15 6:11:01

广告

 

Joshua Bloch 是Java语言组的设计师,去年离开Sun加盟Google,他的《Effective Java》一书在Java界影响很大。最近与Google的同事Neal Gafter (也是前Sun雇员) 合写了《Java Puzzlers: Traps, Pitfalls, and Corner Cases》。该书的几个条目以及全部Source Code可以在www.javapuzzlers.com下载到

我不想介入Java与C#间的比较,但还是不禁想比较一下里面的例子在C#里的行为。

下面是第二章里的几个例子的比较,是在.NET 2.0.50215 和Java 1.5.0下做的,至于结果为什么不一样,建议参考2门语言的Specifications。

1。奇偶性 

Java:

    public static boolean isOdd(int i) {
        return i % 2 == 1;
    }

C#:
    public static bool isOdd(int i) {
        return i % 2 == 1;
    }


输出结果是一样的,这里涉及负整数的余数的问题,但输出结果也许跟你想的也许不一样

isOdd i=-2 i=-1 i=0 i=1 i=2
Java false false false true false
C# False False False True False

2。浮点数的减法

Java: System.out.println(2.00 - 1.10);

C#: System.Console.WriteLine(2.00 - 1.10);

输出结果不一样

Java 0.8999999999999999
C# 0.9

对C#这个结果有点怀疑,大概是格式化的原因,因为如果用ILDASM看的话,是这样的

  IL_0000:  ldc.r8     0.89999999999999991
  IL_0009:  call       void [mscorlib]System.Console::WriteLine(float64)

3。大整数除法

Java:
        final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
        final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;

        System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);

C#:
        const long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
        const long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;

        System.Console.WriteLine(MICROS_PER_DAY / MILLIS_PER_DAY);

在C#里编译出错,使用unchecked后输出相同,但输出结果也许跟你想象的输出结果不一样

Java 5
C# 5

4。16位数的加法

Java: System.out.println(Long.toHexString(0x100000000L + 0xcafebabe));

C#:  System.Console.WriteLine("{0:x}", 0x100000000L + 0xcafebabe);

输出结果不一样

Java cafebabe
C# 1cafebabe

5。多重转换

Java: System.out.println((int) (char) (byte) -1);

C#: 

 unchecked
 {
         System.Console.WriteLine((int) (char) (byte) -1);
 }

输出结果不一样

Java 65535
C# 255

6。 整数交换

Java: 
        int x = 1984;
        int y = 2001;
        x ^= y ^= x ^= y;
        System.out.println("x = " + x + "; y = " + y);

C#:
        int x = 1984;
        int y = 2001;
        x ^= y ^= x ^= y;
        System.Console.WriteLine("x = " + x + "; y = " + y);

输出结果一样

Java x = 0; y = 1984
C# x = 0; y = 1984

7。条件运算符

Java:
        char x = 'X';
        int i = 0;
        System.out.print(true  ? x : 0);
        System.out.print(false ? i : x);

C#:
        char x = 'X';
        int i = 0;
        System.Console.Write(true  ? x : 0);
        System.Console.Write(false ? i : x);

输出结果不一样

Java X88
C# 8888

打印 | 张贴于 2005-08-19 11:03:00 | Tag:.NET  书籍

留言反馈

#上周技术关注:.NET和J2EE该相互学习什么 编辑
Ping Back来自:blog.csdn.net
2005-08-29 02:11:00 | [匿名:曾登高]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0