Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java learning problems encountered     - Linux use logs to troubleshoot (Linux)

- CentOS 6 kernel upgrade to Kernel 3.x (Linux)

- Rails project prepared some tips small experience (Linux)

- Ubuntu 14.04 install PostgreSQL 9.2 (Database)

- Grub2 Boots the openSUSE installation image (Linux)

- PHP generates a random password several ways (Programming)

- How to release the cache memory on Linux (Linux)

- Analysis of memory mapping process in Linux x86-32 mode (Linux)

- Python 2 Chinese garbage problem solved (Linux)

- Java reflection by calling the class method (Programming)

- How to add and delete bookmarks in Ubuntu (Linux)

- Protect against network attacks using Linux system firewall (Linux)

- Several configuration changes illustrate deployment of PHP (Server)

- Ubuntu 12.04 installed OpenCV 2.3.1, binary image (Linux)

- LVM mirrored logical volume to achieve (Linux)

- MongoDB 3.2 to upgrade from 3.0.7 (Database)

- How to manage Vim plugin (Linux)

- Ubuntu 14.10 Apache installation and configuration (Server)

- MongoDB collection data migration to MySQL database (Database)

- How to use GRUB2 files directly from the hard disk to run ISO (Linux)

 
         
  Java learning problems encountered
     
  Add Date : 2017-08-31      
         
       
         
  First Java generics how to compare the size of the inheritance Comparable class, then the only way to achieve its compareTo ():

1 public class GenericClass < E extends Comparable < E >> {
2 public int com (E e1, E e2) {
3 return e1.compareTo (e2);
4}
5}

Second Java parameters pass by value or reference semantics:

When passed by value means that when a parameter passed to a function, the function receives a copy of the original value. Thus, if a function modifies the parameters, change only the copy, and the original value remains unchanged.

When passed by reference means that when a parameter passed to a function, the function receives a copy of the original value of the memory address, rather than a copy of the value. Thus, if a function modifies the parameter, the calling code of the original value has changed. But the point to the original object reference does not change!

The following from the assignment list of methods and parameters of java two aspects are described as:

Assignment:

When Object type assignment is passed by reference; when the eight basic types (int, long, double, float, byte, boolean, char) object assignment is passed by value;

StringBuffer s = new StringBuffer ( "good");
StringBuffer s2 = s; // passed by reference, s and s2 points to the same memory address
s2.append ( "afternoon!");
System.out.println (s); // good afternoon

int i = 5;
int i2 = i; // passed by value
i2 = 6;
System.out.println (i); // 5

Java method parameter list:

Parameter list, the object type of object is passed by reference, but no matter what kind of operation carried out in vivo method, the argument does not change the reference object;

Eight basic types (int, long, double, float, byte, boolean, char) object is passed by value;

public class test {
    public static void main (String [] args) {
        StringBuilder s = new StringBuilder ( "good"); // good
        StringBuilder s2 = new StringBuilder ( "bad"); // bad
        test (s, s2); // copy passed by reference, but an internal matter test () method of s, the contents of the object pointed to by s2 made what changes to the object pointed to by s2 and s will not change
// Popular point is this: the s and s2 seen as a pointer, a pointer to the contents of an object regardless of the change, s and s2 pointer will not change
        System.out.println (s); // goodhah2, here is the content of the object pointed to by s from the good into the goodhah2
        System.out.println (s2); // bad
    }
    
    public static void test (StringBuilder s, StringBuilder s2) {
        System.out.println (s); // good
        System.out.println (s2); // bad
        s2 = s; // copy passed by reference
        s = new StringBuilder ( "new"); // s point to a new object
        System.out.println (s); // new
        System.out.println (s2); // good
        s.append ( "hah");
        s2.append ( "hah2");
        System.out.println (s); // newhah
        System.out.println (s2); // goodhah
    }
}

[to sum up]:

1. Whatever the type of Java parameters that will be passed a copy of the parameters. If Java is passed by value, then passed a copy of the value; if Java is passed by reference, then passed a copy of references.

2. Regardless of the assignment, or java method parameter list, the basic data types are passed by value (a copy), the type of object passed by reference (copy).

Triple comparator. Equals () and == of

1. equals method is to compare the contents of the string, it is more and it points to an object (reference address) are the same for non-string it is.

== Comparison operator whether the object is relatively the same point, that is, objects in memory in the first address.

2. equals method is java.lang.Object class method, Object class equals method is used to compare the "address", but the String class is redefined equals this method, but the comparison is a value, instead of the address.

3. If it is a basic type (byte, short, int, long, float, double, char, boolean) comparison, it can only be used == to compare, you can not use equals ();

4. For basic types of packaging types, such as Boolean, Character, Byte, Shot, Integer, Long, Float, Double, etc. reference variables, == is to compare address, and equals is more content.

5. Note that a basic principle: the definition of a class, if it comes to compare object should override equals () method.

IV. The java heap, stack, constant pool

Stack: eight basic types of data storage and object references, but the object itself is not stored on the stack, but stored in the heap

Heap: new store data generated by the garbage collector is responsible for the

[Stack variable to point to heap memory variables, which is a pointer in Java Thought]

Static field: located in a static object using the definition of static members

Constant pool: storage of constants. Constant pool (constant pool) refers is determined at compile time, and saved some data compiled .class file. In addition to all the basic type contains the code defined (such as int, long, etc.) and object type (such as String and Array) constant value (final) also contains some symbolic references in text form, such as: classes and interfaces the fully qualified name; field names and descriptors; the methods and the names and descriptors. The virtual machine must maintain a constant pool for each type is loaded. The constant pool is the type used in an ordered set of constants and including direct constants (string, integer and floating point constants) and references to other types of symbols, fields and methods. For String constant whose value is in the constant pool.

[to sum up]:

a. stack used to store some of the original data and the data type of the local variable references to objects (String, array. objects, etc.), but does not store the contents of the object

b. heap storage use new keyword to create the objects.

c. String is a special wrapper class whose reference is stored in the stack, and the contents of the object to be created in different ways according to fixed (constant pool and heap). compile some already created and stored in a string constant pool while some are run only be created using the new keyword, stored in a heap.
     
         
       
         
  More:      
 
- Java proxy use and proxy principle (Programming)
- How to set IonCube Loaders in Ubuntu (Linux)
- CentOS 6.4 OpenNebula installation (Linux)
- To install Docker under Ubuntu 15.04 (Linux)
- Python several standard types of built-in functions (Programming)
- Puppet subcommands Introduction (Server)
- Redhat 5 prohibit IPv6 (Linux)
- RCU lock in the evolution of the Linux kernel (Linux)
- Hands to teach you to solve Ubuntu error message (Linux)
- HTML5 Fundamentals study notes (Programming)
- Ubuntu installed Komodo editor by PPA (Linux)
- Oracle ORA-01691 error message, a single data file size limit problem (Database)
- Intrusion prevention network server security maintenance tips (Linux)
- Ubuntu study notes and related problem solving (Linux)
- Build your own CA services: OpenSSL command line CA Quick Guide (Server)
- Using IE can also be easily remotely control the computer (Linux)
- Ubuntu 14.04 CodeBlocks configuration manually compile Boost 1.57.0 (Linux)
- Firewall Configuration Red Hat Enterprise Linux 4 (Linux)
- Ubuntu derivative version of the user and how to install SmartGit / HG 6.0.0 (Linux)
- HTTP and HTTPS request response process difference (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.