• Java细节


    native关键字用法

    native是与C++联合开发的时候用的!java自己开发不用的!

    使用native关键字说明这个方法是原生函数,也就是这个方法是用C/C++语言实现的,并且被编译成了DLL,由java去调用。 这些函数的实现体在DLL中,JDK的源代码中并不包含,你应该是看不到的。对于不同的平台它们也是不同的。这也是java的底层机制,实际上java就是在不同的平台上调用不同的native方法实现对操作系统的访问的。

    1. native 是用做java 和其他语言(如c++)进行协作时用的 也就是native 后的函数的实现不是用java写的
    2. 既然都不是java,那就别管它的源代码了,呵呵

    native的意思就是通知操作系统, 这个函数你必须给我实现,因为我要使用。 所以native关键字的函数都是操作系统实现的, java只能调用。

    java是跨平台的语言,既然是跨了平台,所付出的代价就是牺牲一些对底层的控制,而java要实现对底层的控制,就需要一些其他语言的帮助,这个就是native的作用了

    打印对象到底会打印什么?

    调用对象的toString()方法,toString()方法实际打印的是hashCode()方法。

    System.out.println(Object x) == System.out.println(x.toString())

    public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    
    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
    
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    

    hashCode()equals()

    Java hashCode() 和 equals()的若干问题解答

    既然Object.java中定义了equals()方法,这就意味着所有的Java类都实现了equals()方法,所有的类都可以通过equals()去比较两个对象是否相等。 但是,我们已经说过,使用默认的“equals()”方法,等价于“==”方法。因此,我们通常会重写equals()方法:若两个对象的内容相等,则equals()方法返回true;否则,返回fasle。

    equals()==

    ==它仅仅比较(也只能用于)内存地址是否相等,而equals()仅在 Object类中采用了==进行比较,也就是不进行重写equals()方法 和 ==是一样的。

    重写之后的equals()还可以用于比较内容的相等性。

    Object 类中equals 方法

        public boolean equals(Object obj) {
            return (this == obj);
        }
    

    String 类中equals 方法

        public boolean equals(Object anObject) {
            if (this == anObject) {
                return true;
            }
            if (anObject instanceof String) {
                String anotherString = (String)anObject;
                int n = value.length;
                if (n == anotherString.value.length) {
                    char v1[] = value;
                    char v2[] = anotherString.value;
                    int i = 0;
                    while (n-- != 0) {
                        if (v1[i] != v2[i])
                            return false;
                        i++;
                    }
                    return true;
                }
            }
            return false;
        }
    

    为什么主函数的命令行参数不可以去掉

    这是一种规定,有些语言可以没有,C/C++,就没有,java强制要求有

    Why is String[] args required in Java?

    It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed? I suppose Java could look for a main method with the same number of parameters as command line arguments, and then try to parse them using Integer.parseInt etc... but this would be pretty complicated, and would still be inadequate in many situations.

    As for it being mandatory - basically the Java designers decided to stick to a single method signature, which is frankly simpler than allowing for it to be optional. It would be possible though - in C#, you can have any of

    static void Main()
    static void Main(string[] args)
    static int Main()
    static int Main(string[] args)
    

    Ultimately it doesn't make a lot of difference, to be honest. There's no significant downside in having to include the parameter.

  • 相关阅读:
    Java基础——方法
    JavaScript-JSON解析
    JavaScript—事件
    Window 浏览器窗口对象
    JavaScript 事件
    JavaEE——css字体样式效果
    JavaEE——CSS字体样式
    JavaEE——CSS3选择器
    JavaEE——css边框样式
    JavaEE——XML简介
  • 原文地址:https://www.cnblogs.com/morethink/p/5943541.html
Copyright © 2020-2023  润新知