Java Beginner FAQ _ string serialization _ mutable immutable _ pass(value, reference)

Serialization,
Converting an object to binary (byte sequence) and storing it in a file is serialization
and deserialization.
Restoring a binary (byte sequence) object in a file into a class is deserialization


◆Note; it is generally necessary to provide a serialization code during serialization to ensure that the recovery is still pointing to the same memory area

The role of serialization
Makes objects permanently stored in Disk.

mutable serialization;
After the object is created, the content located in the object can still be changed, such as StringBuffre

Immutable serialization;
After the object is created, the content in the memory is immutable, such as String

What are mutable and immutable classes?
Mutable class; when you create an instance of this class, you can change the content of this class instance
Immutable class; when an instance of this class is created, it cannot be changed.


Pass by value; (method parameter is, primitive data type) When passing an actual value to this parameter, it is used to initialize this parameter,
When the value of the formal parameter changes,
  ★It will not change the actual value passed over★
Because the two are located in the memory space, the addresses are different, and when calling this
method does not affect (change) the actual value.
The type passed by value is a primitive data type.
Is a value copy process.

Conclusion; the value passed in and located in memory has not been changed.



Pass by reference; (is an object, reference type) When a method is called, the parameter is an object or an array, and the object calls a method in real time
actually point to the same address value. And when actually using this method is to use
The address value space in memory operates on the actual parameters.
And when the method ends,
★These operations (modified) ★
properties are preserved and a
For instance space located in memory
parameters will be modified.
Pass by reference is a reference type, and the address value is passed when passing.

Conclusion; changed the value of the passed object located in memory.


What are mutable characters, and immutable characters?
Strings are immutable character sequences, and mutable character sequences.

Immutable character sequence (String); The value of String is stored in the constant pool in the method area of ​​the memory, and the String class is used
final modified, and the underlying storage of this class is storage using an array, and its array is
Modified with final. Make it immutable in memory.
So when a new string is created, a new one is created in memory
new strings, even with the + concatenator, because
final is immutable and done once every time
Connections are created again in memory.

And; when the content of 2 characters is the same, it will point to the same memory space,
Instead of creating one again in the constant pool for this string here.
It is also a memory saving mechanism.

Mutable character sequences (StringBuilder and StringBuffer);
When the append() method is used, the value of append is stored in the array. When toStirng is called, the value of the array is stored.
The value is copied to the StringBuiler object (pass by reference), thereby implementing an additional function

◆注;String,StringBuilder,StringBuffer
 String; is located in the constant pool
 StringBuilder, in the underlying storage, uses an array to store, and the default empty parameter constructor array is 16. When the length of the array is exceeded
  , then *2+2.
 
 StringBuffer, in the underlying storage, is stored using an array, and the default empty parameter constructor array is 16. When the length of the array is exceeded
  , then *2+2.




Why do classes in different packages inherit Object?
Because Object is a superclass, the language is more flexible, and the class library can be extended to make it compatible with all class libraries.



Does an interface have a parent class?
No, there is no constructor and it is for top-level design, ```` and no `` inherits from Object
Unless the interface inherits the interface, how to get the address




when the inner class overrides the toString method

The use of classpath variables in JAVAEE Baidu


download Oracle sql




rewrite hashCode equals comparator toString and other methods
hashCode is a hash code used to help fast lookup, hashCode is to calculate the memory address of each instantiated object instance

Experiment; 〓 Whether the automatic promotion as a type is -1

◆Note; (The String class uses its value as a parameter and then operates to obtain the hashcode
In other words, as long as the String with the same value is an object or not, the hash values ​​are all equal
because of the underlying storage and pointing mechanism).

Why does a collection override the equals method?
Because contains is through the equals method, if it is a custom class, it is called
The equals method of the custom class.


getClass() gets the class of the current object.




When doing Scanner input, if it is to throw an exception, and input it again, use while and add a next() to the exception handling.
The incoming error throws the exception thrown to this empty next() at the location of the exception handling
However; if it is a reference type, it is not used, because the reference type will automatically do equal 
The basic data types will not, so add an empty next() again
       
       Meaning; it will throw the value of the exception to this empty next();


When a generic type is specified, a specified generic type should be given to the anonymous class when overriding with an anonymous class.


Why use Set to override both HASHCODE and EUQuals methods?
Because the underlying storage is stored according to the value calculated by the HASHCODE hash, EQUALS is used to judge when two HASHCODE values ​​are the same
Whether the content is the same, if they are the same, they will not be added, and if they are different, they will be added.
If the HASHCODE values ​​of the two values ​​are the same and the EQUALS are not the same, they will be located at their element positions, followed by an empty space.
out. Used to store 2 elements at the same time.


Restriction of generics;
(List) <? extends class A> indicates that class A is a subclass of the following, and ? is a List
Can store this class and
subclasses class

(Collection)<? super class A> means? is the
parent
Only this class and parent classes can be stored.




Generic methods can be used static








SERIALIZEBLE
serializeble is a serialization interface, which implements this interface can be serialized, and implements the interface to add a sequence to the implementation class
After the number, it can be ensured that when the file is read out, there will be no loss, etc., and the implementation of this interface can store
to the state of this class object.




Why COMPARATOR doesn't have to rewrite EQUALS
2 entities EQUALS is TRUE, then the HASHCODE of the 2 entities must be the same (it makes no sense to write it yourself!)
When rewriting COMPARATOR or COMPARE, it is recommended to rewrite the EQUALS method, because the latter one cannot be avoided if the two values ​​are the same.
It is added, so it is recommended to rewrite the EQUALS method at the same time, and the HASHCODE method, HASHCODE is to determine 2 values
location in memory.





Code of dichotomy
   public static int search(int[] arr, int key) {
    //record the first position of the array
       int start = 0;
       //record the last position of the array
       int end = arr.length - 1;
       while (start <= end) {
           //Start from the middle to find
           int middle = (start + end) / 2;
           
           //If the input element is less than the middle element then
           if (key < arr[middle]) {
           //Remove The previous element starting with the middle element
               end = middle - 1;
           
           //If the input element is larger than the middle element, then    
           } else if (key > arr[middle]) {
           
           //Start with the middle element, and finally An element ends
               start = middle + 1;
           
           } else {
           //otherwise return the middle element
               return middle;
           }
       }
       return -1;
   }
}

If you assign a value to a string, then this character is also an object, because this string is stored in the constant pool in the memory method area. and this
Strings have an address value.

Array to collection method. Collection to array
Array.adList(arr); toArray();

recursive; keep passing until the end
You can use the method to achieve the effect of recursion


\r\nThe function
\r is a newline, usually a blank line
\n is a carriage return and a newline, a line is the end of a line

What type of value does the switch language in java support


byte short int char 
· Note: After JDK7.0, add support for String of strings.

Write collection traversal method to the summary

. The role of class.class and this
Running class

Note ; when using Thread instead of Runable, and in the form of a synchronized method,
If the variable is static, the method must also be static, otherwise synchronized will not work
Because the default synchronization method calls this, and the static call is the class. Class


multicast is mandatory, broadcast is non-mandatory

declare 
i number(30) := 0;
begin 
loop 
exit when i > 999999

insert into emp(empno,ename) values(i,'XX');

i := i+1;
dbms_output.put_line(i);
end loop;
end;
/

exception 





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326759866&siteId=291194637