The difference between equals and "= =" in Java, string special

Source: Internet
Author: User

public class TestString {


/*
* Data types in Java can be divided into two categories:
* 1, the basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean
* Compare them, apply the double equals sign (= =), compare their values.
* 2, composite data type (Class)
* When they compare with (= =), they compare their storage address in memory, so unless it's the same new object,
* Their comparison results are true, otherwise the comparison result is false.
*
* All classes in Java inherit from the base class of object, and a method of equals is defined in the base class in object.
* The initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overwritten,
* such as string,integer,date in which equals has its own implementation, and is no longer a comparison class in the heap memory of the storage address.
*
* For the Equals comparison between composite data types, if the Equals method is not covered,
* The comparison between them is based on the address value of their storage location in memory,
* Because the Equals method of object is compared with the double equals sign (= =), the result of the comparison is the same as the double equals sign (= =).
*
*/

Method_1 compile run result is S1==S2
Description: S1 and S2 refer to the same Sring object---"Monday"
public static void Method_1 () {
String S1 = "Monday";
String s2 = "Monday";
if (S1==S2) {
System.out.println ("S1==s2");
}else{
System.out.println ("S1!=s2");
}

if (s1.equals (S2)) {
System.out.println ("S1 equals S2" +s1.hashcode () + ":::" +s2.hashcode ());
}else{
System.out.println ("S1 ont equals S2");
}

}


/* The S2 is created with the new operator, and the program outputs the result;
* S1!=S2
* S1 equals s2
* Description S1 and S2 refer to two "Monday" string objects respectively
*/
public static void Method_2 () {
String S1 = "Monday";
String s2 = new String ("Monday");

if (S1==S2) {
System.out.println ("S1==s2");
}else{
System.out.println ("S2!=s2");
}

if (s1.equals (S2)) {
System.out.println ("S1 equals S2");
}else{
System.out.println ("S1 not equals S2");

}
}

/* String Buffer pool
* Originally, the program will create a string buffer when running, when using s2= "Monday" expression is the time to create a string,
* The program will first find the same worthy object in this string buffer pool, in the first program, S1 was first placed in the pool,
* So when S2 is created, the program finds a S1 with the same value, and S2 references the object "Monday" that S1 refers to.
*
* In the second procedure, the new operator was used, and he understood the program: "I want a new, not old",
* A new "Monday" string object is created in memory. Their values are the same, but the locations are different,
* One in the pool useful a rest on the shore, is really a waste of resources, clearly is the same must be divided into what?
*
* Re-modify the program as follows:
*/

public static void Method_3 () {
String S1 = "Monday";
String s2 = new String ("Monday");

S2 = S2.intern ();

if (S1==S2) {
System.out.println ("S1==s2");
}else{
System.out.println ("S1!=s2");
}

if (s1.equals (S2)) {
System.out.println ("S1 equals S2");
}else{
System.out.println ("S1 not equals S2");
}
}
/* Join S2 = S2.intern (); The output of the program is:
* S1==S2
* S1 equals s2
* Original, (java.lang.String) of the Intern () method
* "ABC". The return value of the Intern () method is also the string "abc", which looks as if the method is of little use.
* But in fact, it does a little trick: Check the string pool for the existence of "ABC" such a string,
* Returns a string in the pool if it exists, and if it does not exist, the method adds "ABC" to the string pool and returns its reference.
*
*
*
*
*
* I. Memory allocation strategy in Java and comparison of Heap and stack
*
* 1. Memory allocation policy
*a, according to the idea of compiling principle, the program runs memory allocation has three kinds of strategies, namely static, stacked, and heap-type
* Static storage allocation refers to the need to determine the storage space at compile time for each data target at run time, so that they can be allocated a fixed amount of memory at compile time.
* This allocation policy requires that the existence of mutable data structures (such as variable groups) not be allowed in the program code, and that nested or recursive structures are not allowed to appear.
* Because they all cause the compiler to be unable to calculate the exact storage space requirements.
*B, stack storage allocations can also be called dynamic storage allocations, and are implemented by a stack-like run stack. Instead of static storage allocation, in a stacked storage scenario,
* The requirements of the program on the data area are completely unknown at compile time, only to be able to know when running, but it is required to enter a program module in the operation.
* You must know the size of the data area required by the program module to allocate memory for it. Like the stacks we know in data structures,
* Stack storage allocation is distributed according to the principle of advanced post-out.
*c, static storage allocation requires that all variables are stored at compile time, and the stack storage allocation requires that all storage requirements be known at the entrance of the process.
* While heap storage allocation is specifically responsible for the memory allocation of data structures that cannot be determined at compile time or at runtime module entrances,
* such as variable-length strings and object instances. The heap consists of large chunks of available or free blocks, and the memory in the heap can be allocated and freed in any order.

* 2, Heap and stack comparison
* The above definition is summarized from the compiling principles of the textbook, in addition to static storage allocation, it is very rigid and difficult to understand, the following aside static storage allocation, centralized comparison heap and stack:
* From the heap and the function of the stack and the role of the popular comparison, the heap is mainly used to store objects, the stack is mainly used to execute the program, and this difference is mainly due to the characteristics of the heap and stack:
*a, in the compilation, for example, C + +, all method bars are used through the stack, all local variables, formal parameters are allocated from the stack memory space.
* It's not actually a distribution, just up the top of the stack, like a conveyor belt in the factory (conveyor belt), stack pointer will automatically guide you to where you put things,
All you have to do is just put the things down. When you exit the function, you can modify the stack pointer to destroy the contents of the stack, so the mode is the fastest, and of course it is used to run the program.
* It is important to note that the size of the data area should be known in advance when allocating data for a program module that is about to be called.
* That is, although allocations are made at run time, the size of the allocations is deterministic and unchanging, and this "size" is determined at compile time, not at runtime.
*b, Heap is when the application is running to request the operating system to allocate its own memory, due to the memory allocation from the operating system management, so in the allocation and destruction of time to occupy,
* Therefore the efficiency of the heap is very low. But the advantage of the heap is that the compiler doesn't have to know how much storage to allocate from the heap, or how long the stored data stays in the heap,
* Therefore, there is greater flexibility in storing data with heaps. In fact, object-oriented polymorphism, heap memory allocation is essential.
* Because the required storage space for polymorphic variables can only be determined after the object is created at run time. In C + +, when you want to create an object, you only need to compile the relevant code with the new command.
* When executing this code, the data is saved automatically in the heap, and of course, to achieve this flexibility, it is necessary to pay a certain amount of code: Allocate storage space in the heap will take longer time!
* This is the reason why we have just said that the efficiency is low.
*
* 3. Heap and Stack in JVM
*a, the JVM is a stack-based virtual machine. The JVM allocates a stack for each newly created thread. In other words, for a Java program, it runs through the operation of the stack.
* The stack holds the state of the thread in frames. The JVM operates on the stack in only two ways: stack and stack operations in frames.
*b, we know that the method that a thread is executing is called the current method of this thread. We may not know that the frame used by the current method is called the current frame.
* When a thread activates a Java method, the JVM will press a new frame into the Java stack of threads. This frame is naturally referred to as the current frame. During this method, this frame will be used to hold parameters, local variables, intermediate calculation processes, and other data
* This frame is similar to the concept of the activity record in this and the compilation principle.
*c, from the Java allocation mechanism, the stack can be understood as follows: (stack) is the operating system in the establishment of a process or a thread (in support of multi-threaded operating system is a thread)
* The storage area established for this thread, which has an advanced post-out feature.
*d, each Java application uniquely corresponds to a single JVM instance, and each instance uniquely corresponds to one heap. All class instances or arrays created by the application in the run are placed in this heap,
* The disease is shared by all threads applied. In contrast to C + +, allocating heap memory in Java is automatically initialized. The storage space for all objects in Java is allocated in the heap.
* But the reference to this object is allocated on the stack, which means that the memory is allocated from two places when an object is created.
* Allocating memory in the heap actually establishes this object, and the memory allocated on the stack is just a pointer (reference) to the heap object.
*e, the heap is stored in the creation of objects, Java string Object memory implementation, in the heap opened a small piece of memory, called a string constant pool, used to hold a particular string object.
*f, colloquial Java stack
*java Heap:
* 1. Dynamic allocation of memory size during program operation
* 2. The Java garbage collector automatically reclaims data that is no longer in use
*java Stack:
* 1, stack data can be shared
* 2, the existence of the stack of data size and lifetime must be determined.
* 3, the stack mainly contains some basic types of variables (Int,short,char,long,byte,float,doubel,boolean) and object references.
*
*
* Second, string is a special wrapper class data. Can be used:
*string str = new String ("abc");
* The steps to create a string object using new are as follows, and a new object is created each time it is called.
* 1. First create a String object containing the specified content in the heap (not the constant pool) and point the string reference to the object.
* 2, go to the string constant pool to see if there is an object that contains the content.
* 3, if any, the new string object is associated with the same object in the string constant pool.
* 4. If not, create a string object in the string constant pool that contains the content, and associate the objects in the heap with the newly created objects in the string constant pool.
*
* The benefits of a string-specific memory mechanism are: compare only two strings in a constant pool where the objects are the same, regardless of the size of the string, compared to the same speed.
*string str = "ABC";
* 1, first create a heap in the stack object reference variable of String class str
* 2, then look for the heap in the constant pool there is no storage "ABC"
* 3. If not, store "ABC" in a constant pool and point str to "ABC"
* 4, if there is already "ABC" directly to the STR point to "ABC"
*
*
* Third, special circumstances
The integers below the *127 are equal.
*integer i = 100;
*integer j = 100;
*system.out.println (I==J);//Print True
*128 and above are treated as different objects.
*integer i = 200;
*integer j = 200;
*system.out.println (I==J);//Print False
*/

}

The difference between equals and "= =" in Java, string special

Related Article

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.