Java 的运算符

来源:互联网 发布:windows禁用数字签名 编辑:程序博客网 时间:2024/06/01 10:23

之前小编写了关于Java的一些介绍,现在,就讲一下有关运算符的知识点。下面还是照样大部分采用英文的格式来介绍。

Operators
1. Almost all operators work only with primitives (not class types)
[1] And the operators are those that you know (except there is no sizeof operator)

  1. =, == and != work on all objects of any type (Even class types!!!)
    [1] But, if you use them with a reference to an object - you are just manipulating the references.
    [2] = causes two object references to point to the same object (feels like shallow copy!)
    [3] == and != compares two references to see if they are pointing to the same object (or not)!
    [4] And, since there is no operator overloading - we can’t change this to do a deep copy!
    [5] This is because Java allows us to use references truly as aliases. You can cause a deep copy to happen simply by coping each of the members directly that are part of a class or calling a member function to do this

  2. The String class also supports + and +=

Equals() method
If you want to do a deep comparison,
[1] You “must” can call a method (equals()) that exists for all objects of class type.
[2] Of course, the default behavior of equals() is to just compare the references.
[3] So you “must” override the equals() so that it actually compares the memory contents of the object.

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Syntax:
Here is the syntax of this method –
public boolean equals(Object anObject)

Parameters:
Here is the detail of parameters –
anObject - the object to compare this String against

Return value:
This method returns true if the String are equal; false otherwise.

举个例子:
String a = new String(“aa”);
String b = new String(“bb”);
boolean result;
result = a.equals(b);

这个例子显示的结果是false.

希望这么一点内容对大家的学习有所帮助!

原创粉丝点击