Java Relational operators

Source: Internet
Author: User

1. The relational operator generates a Boolean result. They evaluate the relationship between the operands ' values. If the relationship is true, the relational expression generates true (True), and False (false) if the relationship is not true.


1. Check if the objects are equal (= = or! =)

integer n1 = new integer (Integer n2 =);  new integer (47); System.out.println (N1 == N2);     //result is falseSystem.out.println (n1 != n2 );     //result is true because = = and! = are the handles of the comparison object, N1 and N2, although the integer object is the same, the object content is the same, but the handle differs 2. equals () using the integer  n1 = new integer (+); Integer n2 = new integer (47); System.out.println (N1.equals (N2));     //tureequals compares object content, so ture;class value { Int i;} Public class equalsmethod2 {public static void main (String[] args)  { Value n1 = new value (); Value n2 = new value ();n1.i = n2.i = 10; System.out.println (N1.equals (N2));    //false}} The results are now back to false! This is because the default behavior of  equals () is to compare handles. So unless you change the Equals () in your new class, it's impossible to show the behavior we want. 

This article is from the "12380959" blog, please be sure to keep this source http://12390959.blog.51cto.com/12380959/1909179

Java Relational operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.