Understanding java-2.3 Self-increment and relational operators from the beginning

Source: Internet
Author: User

In this chapter, let's talk about the self-increment decrement and the relationship operator.

1. Self-increment in fact, there is no particular need to pay attention to, as long as the basic principles to understand.

Package Com.ray.ch03;public class Test {public static void main (string[] args) {int a = 0; System.out.println ("before + +:" + a); System.out.println ("+ +:" + a++); System.out.println ("After + +:" + a); System.out.println ("--------"); System.out.println ("before + +:" + a); System.out.println ("+ +:" + (++a)); System.out.println ("After + +:" + a); System.out.println ("--------"); System.out.println ("Before--:" + a); System.out.println ("--:" + a--); System.out.println ("After--:" + a); System.out.println ("--------"); System.out.println ("Before--:" + a); System.out.println ("--:" +--a); System.out.println ("After--:" + A);}}

Output:

Before ++:0
++:0
After ++:1
--------
Before ++:1
++:2
After ++:2
--------
Before--:2
--:2
After--:1
--------
Before--:1
--:0
After--:0


From the code as well as the output, we can see a change in each stage, as long as you figure this out.


2. Relational operators

Actually, it's >,<,=,!=.

(1) Equivalence of objects

Package Com.ray.ch03;public class Test {public static void main (string[] args) {Dog Jack = new Dog ();D og rose = new Dog (); System.out.println (Jack = = Rose); System.out.println (Jack.equals (Rose)), Integer a = new integer (1), Integer b = new integer (1); System.out.println (A = = B); System.out.println (A.equals (b)), Integer d = new Integer (100000), integer e = new Integer (100000); System.out.println (d = = e); System.out.println (D.equals (e)); int f = 1;int g = 1; System.out.println (f = = g);//System.out.println (F.equals (g));//Error}}class Dog {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;}}

Output:

False
False
False
True
False
True
True


The comparison of dog's is not said, we focus on the number of the comparison.

= = is generally a reference to a comparison object, one a==b,d==e returns false, and equals is used to compare the values inside two objects because the Equals method has been overridden in the integer.

public boolean equals (Object obj) {        if (obj instanceof Integer) {            return value = = ((Integer) obj). Intvalue ();        }        return false;    }

Instead of the object's reference, he compares the value with the Equals

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

He only compares the references of both to the same object.

One thing to note: Basic types cannot use equals


Let's look at the following two examples:

Package Com.ray.ch03;public class Test {public static void main (string[] args) {Dog Jack = new Dog ();D og rose = new Dog (); Jack.setname ("+"); Rose.setname ("100"); System.out.println (Jack = = Rose); System.out.println (Jack.equals (Rose));}} Class Dog {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;}}

Output:

False
False


Although both objects set the same value, they are using the equals of object, just comparing the references, and returning false at the same time, we modify the above code.

Package Com.ray.ch03;public class Test {public static void main (string[] args) {Dog Jack = new Dog ();D og rose = new Dog (); Jack.setname ("+"); Rose.setname ("100"); System.out.println (Jack = = Rose); System.out.println (Jack.equals (Rose));}} Class Dog {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic boolean equals (Object obj) {if (obj instanceof Dog) {return ((Dog) obj). GetName (). Equals (name); return super.equals (obj);}}

Output:

False
True

We have rewritten the Equals method by ourselves, just by comparing the names of the two, so the return is false and true.


Summary: This section briefly describes the self-increment and relational operators, as well as the areas where = = and equals need to be noted.


This chapter is here, thank you.

-----------------------------------

Directory


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding java-2.3 Self-increment and relational operators from the beginning

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.