Java Knowledge Point Finishing--(2) Programming Basics

Source: Internet
Author: User

I. Java naming conventions

    1. Class name: For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.

    2. Method Name: All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.

Ii. Java data types

  1. Basic data type (built-in data type)

    Byte 8-bit, signed,-128-127, default value 0
    Short 16-bit, signed,-32768-32767, default value 0
    Int 32-bit, signed, -2,147,483,648-2,147,483,647, default value 0
    Long 64-bit, signed, -9,223,372,036,854,775,808-+, default value 0L
    Float 32-bit, signed, IEEE 754-compliant floating-point number, default 0.0F
    Double 64-bit, signed, IEEE 754-compliant floating-point number, default 0.0
    Boolean 1-bit, true/false, all lowercase
    char 16-bit Unicode characters, constant with '
      1. java for Unicode support with is the UTF-16 encoding implementation; Under UTF-16, a Unicode character in Java is represented by a 1 or 2 char (code unit).

        utf-32 and Unicode code table basic One A corresponding, fixed four bytes.
        unicode the scope of the definition is too large, in fact 99% of people use no more than 2 bytes of character encoding, so if you unify 4 bytes, data redundancy is too large and 16 bits are the best. Characters that are more than 16 digits long can be represented by proxy technology, with 32-bit identification. So now most machines implement Unicode with UTF-16 scheme
    1. Objects, arrays are reference data types. The default value for all reference types for

    2. is null.

    3. A reference variable can be used to reference any type that is compatible with it.

  2. Type conversions

    (1) Automatic type conversion

    Integer, Real (constant), character-type data can be mixed. Operations, different types of data are converted to the same type first, and then the operation is performed. Conversions from low to advanced.

    Low------------------------------------------------------------> High

    Byte, short, char-> int-> long-> float-> Double

    (2) Forced type conversion

    Forced type conversions must be used when converting large-capacity types to small-capacity types.

    Format: (type) value

    Note: The data type of the conversion must be compatible, and the Boolean type cannot be cast.

Three, constant

    1. Variable scope

      (1) Local variables

      1. A local variable is declared in a method, a construction method, or a block of statements;

      2. Local variables are created when methods, construction methods, or block of statements are executed, and when they are executed, the variables are destroyed;

      3. Access modifiers cannot be used for local variables;

      4. A local variable is only visible in the method, construction method, or block of statements that declares it;

      5. Local variables are allocated on the stack.

      6. The local variable has no default value, so the local variable is declared and must be initialized before it can be used.

(2) member variable (instance variable)

      1. Instance variables are declared in a class, but outside of methods, construction methods, and statement blocks;

      2. Instance variables are created when the object is created and destroyed when the object is destroyed;

      3. Instance variables can be declared before use or after use;

      4. An access modifier can decorate an instance variable, and an instance variable has a default value;

      5. The default value for a numeric variable is 0, the default value for a Boolean variable is false, and the default value for the reference type variable is null.

      6. Instance variables can be accessed directly from the variable name.

(3) class variable (static variable)

      1. Declared with the static keyword in a class, but must be outside of the constructor and statement blocks.

      2. No matter how many objects a class creates, the class has only one copy of the class variable.

      3. Static variables are seldom used except when declared as constants. Static variables are stored in a static storage area.

      4. Static variables are created at the beginning of the program and are destroyed at the end of the program.

      5. has similar visibility to instance variables. However, in order to be visible to the consumer of a class, most static variables are declared as public types.

      6. The default value is similar to the instance variable. The default value for numeric variables is 0, the Boolean default is False, and the reference type default value is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method. In addition, static variables can be initialized in static statement blocks.

      7. Static variables can be accessed by: Classname.variablename.

      8. Class variable names are generally recommended to use uppercase when the class variable is declared as public static final type. If the static variable is not public and final, it is named in the same way as the instance variable and the local variable.

Iv. variables

Five, modifier

  1. Access modifiers

    Modifiers are used to define a class (interface), method, or variable, which is usually placed at the very front of the statement.

    Modifier Current class In the same package Descendant class
    Other Package Classes
    Public
    Y Y Y Y
    Protected
    Y Y Y
    Default Y

    Y



    Private Y


    Note: An empty cell row on the previous table represents N.

    (1) Access control inheritance relationship

      1. Methods declared as public in the parent class must also be public in the subclass.

      2. A method declared as protected in a parent class is either declared as protected in a subclass, or declared as public, and cannot be declared as private.

      3. The method declared as private in the parent class cannot be inherited.

  2. Non-access modifiers

    1. static: Used to create class methods and class variables


      declares a static variable independent of an object, no matter how many objects a class instantiates, its static variable has only one copy. A static variable is also a class variable. A local variable cannot be declared as a static variable.
      static method
      access to class variables and methods can be used directly classname.variablename  and  classname.methodname  way access.
    2. final:final decorated class cannot be inherited, the decorated method cannot be redefined by the inheriting class, the modified variable is constant, is not modifiable.

      final variable final variables can be explicitly initialized and initialized only once. A reference to an object declared final cannot point to a different object, but the data in the final object can be changed, meaning that the final object's reference cannot be changed, but the value inside can be changed. The final modifier is typically used with the static modifier to create a class constant.
      final method

      final class
      final class cannot be inherited, and no class can inherit any of the properties of the final class.
    3. Abstract: Used to create abstraction classes and abstract methods.

      Abstract method
      An abstract method is a method that does not have any implementation, and the specific implementation of the method is provided by the subclass. Abstract methods cannot be declared final and static. Any child class that inherits an abstract class must implement all the abstract methods of the parent class, unless the subclass is also an abstract class. If a class contains several abstract methods, the class must be declared as an abstract class. Abstract classes can contain no abstract methods. The declaration of an abstract method ends with a semicolon, for example: Public abstract sample ();.
      Abstract class
      An abstract class cannot be used to instantiate an object, and the sole purpose of declaring an abstract class is to augment the class in the future. A class cannot be both abstract and final decorated. If a class contains an abstract method, the class must be declared as an abstract class. Abstract classes can contain both abstract and non-abstract methods.
    4. Synchronized

    5. Transient



Java Knowledge Point Finishing--(2) Programming Basics

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.