Tag: Java

A Conversation with Anders Hejlsberg, by Bill Venners with Bruce Eckel(August 4, 2003)

Posted by – 2009/01/05

A Conversation with Anders Hejlsberg,
by Bill Venners with Bruce Eckel
August 4, 2003

These conversation includes 8 parts:

  • In Part I: The C# Design Process, Hejlsberg discusses the process used by the team that designed C#, and the relative merits of usability studies and good taste in language design.
  • In Part II: The Trouble with Checked Exceptions, Hejlsberg discusses versionability and scalability issues with checked exceptions.
  • In Part III: Delegates, Components, and Simplexity, Hejlsberg discusses delegates and C#’s first class treatment of component concepts.
  • In Part IV: Versioning, Virtual, and Override, Hejlsberg explains why C# instance methods are non-virtual by default and why programmers must explicitly indicate an override.
  • In Part V: Contracts and Interoperability, Hejlsberg discusses DLL hell and interface contracts, strong names, and the importance of interoperability.
  • In Part VI: Inappropriate Abstractions, Hejlsberg and other members of the C# team discuss the trouble with distributed systems infrastructures that attempt to make the network transparent, and object-relational mappings that attempt to make the database invisible.
  • In Part VII: Generics in C#, Java, and C++, Hejlsberg compares C#’s generics implementation to Java generics and C++ templates, describes constraints in C# generics, and describes typing as a dial.
  • In Part VIII: CLR Design Choices, Hejlsberg discusses IL instructions, non-virtual methods, unsafe code, value types, and immutables.
  • Java程序向.NET移植过程中的笔记

    Posted by – 2008/11/20

    移植TE过程中遇到了一些Java与.NET平台的差异而产生的问题,在此记录。.NET中一些“隐藏功能”在这个过程中得到了很好的应用。
    这里采用手工移植的方式,也可以尝试JLCA等自动转换工具。参见MSDN Magazine上的介绍JLCA使用案例的文章。

    有关Java与.Net异同的一篇采访,A Conversation with Anders Hejlsberg(C#设计者) by Bill Venners with Bruce Eckel(Thinking in Java作者)。

    final vs virtual

    Java中除静态函数以外的方法默认都是虚函数,都可以被覆写,需要使用final关键字标记使其不能被覆写。但也有更准确的解释
    C#中必须用virtual关键字显式的声明一个方法可被覆写。第一个原因是性能,更重要的原因是版本。更具体的解释且看Anders Hejlsberg,Bruce Eckel等人的谈话

    final vs sealed

    Java中可以使用final关键字标记类,使其不能被继承。
    而.Net中使用sealed。对于类也可以用static关键字(会被自动编译为sealed)来防止被派生。

    public class Test {
      Integer testField;
     
      Integer methodA() {
        return testField;
      }
     
      final Integer methodB() {
        return testField;
      }
    }
     
    class Test1 extends Test {
      @Override
      Integer methodA() {
        return 1;
      }
     
      @Override              // error, it can not be overrided
      Integer methodB(){
        return 2;
      }
    }

    Descriptor

    Java中的Descriptor可以用字符串的形式描述字段和方法的类型。但在.Net中我没有类似的东西(如果有请告知)。通过反射查看.Net Assembly可以看到的方法完整的信息,例如变量、参数等。只是没有一个类似Java Descriptor的东西。囧

    多线程(Multiple Thread)

    Java中需要实现Java.Lang.Runnable接口
    .Net多线程只需要将一个方法赋给ThreadStart这个代理然后传给Thread并且调用Thread.Start()方法。
    相对而言后者有更好的同步或者异步的支持。.Net多线程有一个不错的在线教程

    Exception

    InvalidOperationException == IllegalStateException

    implicit & explicit

    implicit是一个关键字,实现隐式转换

    namespace implicitExample
    {
        class Digit
        {
            public Digit(double d) { val = d; }
            public double val;
            // ...other members
     
            // User-defined conversion from Digit to double
            public static implicit operator double(Digit d)
            {
                return d.val;
            }
            //  User-defined conversion from double to Digit
            public static implicit operator Digit(double d)
            {
                return new Digit(d);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Digit dig = new Digit(7);
                //This call invokes the implicit "double" operator
                double num = dig;
                //This call invokes the implicit "Digit" operator
                Digit dig2 = 12;
                Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
                Console.ReadLine();
            }
        }
    }

    泛型,泛型集合(Generic & Generic Collection)

    Java的泛型,在处理泛型集合时会遇到可写不可写的情况,参看这个讨论

    List<?>    // 类型未知,只能添加null
    List<? extends Number>    // 类型未知,限定为Number的派生类,只能添加null
    List<? super Double>    // 类型位置,限定为Double的基类,可以添加元素

    方形方法的例子

    void recordNonOveridableMethodCall(Reason reason, int lineNumber,
         MethodInfo toMethod, Variable methodThis,
         List parameters, Variable returnVariable) {
         //...
    }

    .NET的泛型

    void recordNonOveridableMethodCall(Reason reason, int lineNumber,
         MethodInfo toMethod, Variable methodThis,
         List parameters, Variable returnVariable) where T : Variable{
         //...
    }

    嵌套类(Nested Class in Java, Inner Class in .Net)

    .Net中嵌套类与Java中的有很大不同,内部类只能访问到外部类的静态字段、属性和方法。
    而Java中则都可以访问。

    class OuterClass
    {
        private static int outerStaticField;
        private int outerField;
     
        class InnerClass
        {
            InnerClass()
            {
                outerStaticField = 1;
                //outerField = 1; // compile time error
            }
        }
    }

    TreeSet

    java.util.TreeSet之类的集合在.NET Framework中没有找到对应的实现,C5这个项目提供了大量的数据结构实现。需要调研并加以采用。目前用到了TreeSet这个红白树(Red-Black tree)实现。

    关于RMI的理解

    Posted by – 2006/01/19

    RMI在EJB中又占有很重要得成份。不过完全没有必要因此而挡住了学习上前进的道路。


    RMI:指Remote Method Invocation (远程方法调用)。

    说到远程方法调用,就应该说一下计算机通信技术。

    计算机通信技术到目前为止主要有3种:

    1.Socket 2.RPC 3.RMI

    Socket在Java基础中已经学习,但是那只是皮毛而已。Socket得深入学习要对计算机通信底层技术非常熟悉,才会将Socket得代码写的非常好。否则Socket会让你痛不欲生。

    RPC和RMI差不多,主要都是屏蔽了底层得具体实现。而者区别主要在于:

    RPC是远程过程调用。

    RMI是远程方法调用。


    RMI实现过程。

    简言之,RMI机制在实现过程中,会产生2个对象,分别是:Stub和Skeleton。

    Stub在客户端,Skeleton在服务器端。Stub做为远程对象在客户端得代理,客户端在调用远程对象得过程,其实是调用Stub对象中相应得方法。随后Stub调用远程对象中相应得方法。

    Skeleton会根据Stub传过来得信息进行解码,然后调用对应得方法。在方法调用完成后,会返回给Stub一个处理结果。Stub在将处理结果返回给调用他的客户端。

    这样RMI就完成了基本得调用循环。