Java 的运算符

之前小编写了关于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.

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值