Java programming ideology Reading Notes-1 (Chapter 5-7)

Source: Internet
Author: User

Chapter 2 object of everything
1. All objects must be created by you
1. Where is the storage?
1. Register: we cannot control it in the program
2. stack: stores basic data and object reference, but the object itself is not stored in the stack, but in the Heap.
3. Heap: stores data generated with new
4. Static storage: static members defined by Static in objects
5. Constant storage: stores constants.
6. NON-RAM: hard disk and other permanent storage space
2. Special Case: Basic Type
The basic type of data is stored in the Stack. When an object is generated, only the object reference is stored in the stack to point to an object, and the object itself is stored in Heap.
3. arrays in Java
When you generate an array of storage objects, the actually generated array is used to store the reference. After an array is created, each reference is automatically set to null, indicating "no object pointing ".
2. Create a new data type: Class
1. Data members and functions
1.1 default value of basic members
1) when a member of a class belongs to a basic type, Java ensures that it has a default value even if you do not provide an initial value for it.
2) Java ensures that the initial value is provided only when the variable identity is a member of the class.
3. Functions (Mehtods), arguments, return values)
1. Cited Series
When an object is passed as a reference, the reference of the object is passed.
4. embedded document for Annotation
Java provides two annotation styles:/* XXXX */, // XXXX

Chapter 4 control procedure
1. Use Java Operators
1. Relational operators
1) When two objects are compared using Relational operators, the object reference is compared, for example:

  1. IntegerN1 =New Integer(3 );
  2. IntegerN2 =New Integer(3 );
  3. System. Out. println (n1 = n2 );

The result is false because the values of the two object reference (n1 and n2) are different.
2) The default behavior of quals () is also compared with referenct. However, the class in Java overwrites the equals method, for example:
  1. IntegerN1 =New Integer(3 );
  2. IntegerN2 =New Integer(3 );
  3. System. Out. println (n1.quals (n2 ));// The value is true.

2. logical operators
1) only and, or, and not can be applied to boolean values. If the values on both sides of the logical operator have non-boolean values, an error occurs, for example:
  1. IntTest1 = 1;
  2. System. Out. println (test & 1 <2 );// An error occurred while editing. test is a non-boolean value.
3. Displacement Operators
If the operated displacement object is char, byte, or short, the value of the displacement object will be promoted to int first before the displacement action occurs, and the calculation result will be int.
2. Process Control
1. iteration)
1.1 comma Operator
The comma operator can only be used in the initialization and step sections of the for loop control expression, for example, for (int I = 0, j = I + 1; I <5; I ++, j = I * 2)
1.2 break and continue
Break indicates that the loop is exited; continue indicates that the loop is exited and the start position of the loop is returned.
1.3 label
The label takes effect only before the iteration statement. inserting any statement between the label and iteration statement does not take effect.
2. Switch
The selector in the switch must be int or char type, for example:
  1. FloatI = 2;
  2. Switch(I)// An error will occur because I is not one of int or char

3. Computing details
1) converting from float or double to an integer is always done by completely dropping decimal places.
4. The output range of Math. random () is [0, 1].

Chapter 4 initialization and cleanup
1. Use constructor to ensure Initialization
If a class has a constructor, Java will automatically call the constructor before the user has the ability to perform operations on the object, so that the initialization action will be executed.
2. Method overloading)
1. differentiate between overloaded functions
Since two functions can be distinguished only from the function name and function reference series, and the overload function has the same function name, each overload function must have a unique reference series.
2. Default constructor
1) The default constructor is a constructor without any reference. If the class you develop does not have any constructor, the compiler will automatically generate a default constructor for you.
2) if you define any Constructor (no matter whether there are any arguments), the compiler will not generate the default constructor for you.
3) if a class is defined, such
  1. ClassBush {
  2. Bush (IntI ){}
  3. }

An error occurs when you want to use new Bush (); to generate a class instance. Because the constructor has been defined when the class is defined, the compiler will not generate a default constructor for the class. When we use new Bush () to generate an instance, we will try to call the default constructor, but there is no default constructor in the class, so an error will occur. For example:
  1. ClassSundae
  2. {
  3. Sundae (IntI ){}
  4. }
  5. Public ClassIceCream
  6. {
  7. Public Static VoidMain (String[] Args)
  8. {
  9. // Sundae x = new Sundae (); Compilation error, no constructor Sundae ()
  10. Sundae y =NewSundae (1 );
  11. }
  12. }

*: When defining a class, if you define your own constructor, you 'd better define a default constructor at the same time.
3. Keyword this
1) this is only used within the function to obtain the object reference that "calls this function.
2) In constructor, you can use this to call other constructor in the same class, as shown in
  1. Public ClassFlower {
  2. Flower (IntPetals

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.