Definition and basic usage of constant values from Java to C ++

Source: Internet
Author: User
Tags constant definition


A program usually uses a constant value to define some relatively fixed and practical values. For example, you want to define a playing card category. Playing cards have two attributes: Color and number. However, the color is only red peach (Red Heart), square, black plum, and black peach, in this case, you can define four constants to represent the four colors respectively. The benefit of this definition is that each time you assign a value to a color, you can only use the name of the constant value, which is not prone to errors; if a new method of playing poker requires a new color, only a constant value can be added, and the scalability is strong;

Definition of constants in Java

The most common definition of constants in Java is as follows:

Public Static Final T[CONSTNAME] = [VALUE];

T is a data type, which can be a basic data type or a class. The name of a CONSTNAME constant is usually expressed in uppercase. The VALUE constant VALUE must be a definite VALUE, or it is a static variable that determines the value during compilation. The advantages of this constant definition method can be elaborated by modifying its three keywords:

Public: Declares that this constant value can be accessed by other classes, not limited to the class itself;

Static: Declared that the constant value is static. You can call it directly by defining the Class Name of the constant without creating an object;

Final: Once defined, the constant value cannot be changed;

With regard to the definition and usage of constants, I believe that Android Developers will have a deep understanding of this definition and usage. the internal classes and IDs of various resources defined in the java file are all defined in this way. Of course, the definition of constants is not only in the above form, but can be non-public or non-static, which is the most used in the above form. Java is defined as follows:

/*** Card * @ author xiaoxu **/public class Card {/*** Hongtao (Red HEART) */public static final int HEART = 0; /*** square */public static final int DIAMOND = 1;/*** black plum */public static final int BLACKBERRY = 2; /*** taotao */public static final int SPADE = 3;/*** cards, four cards at random * @ return returns the array of cards, for example, c [0] [0] indicates the number (size) of the first card, c [0] [1] indicates the color of the first card */private int flowerColor; private int size; public Card () {super ();} public Card (int flowerColor, int size) {this. flowerColor = flowerColor; this. size = size;} public int getFlowerColor () {return flowerColor;} public void setFlowerColor (int flowerColor) {this. flowerColor = flowerColor;} public int getSize () {return size;} public void setSize (int size) {this. size = size ;}}

Some people may think that the red card color is a set of values of the same concept, why not define the enumeration type. In C ++, you can define an enumeration type, because enumeration members of the enumeration type in C ++ are constant values, but in Java, enumeration is actually a class type, enumeration type is not efficient and inconvenient to use. In C ++, a group of constants of the same concept can be expressed and enumeration type should be used to make the code readable. in Java, enumeration type is not recommended for all objects that can be expressed using final constants unless some of them make the code hard to understand and have to be used.

Definition of constants in C ++

The definition of constants in C ++ uses const, which is similar to the final of Java. Const is defined as follows:

Const T [CONSTNAME] = [VALUE];

T is a data type, which can be a basic data type or a class. The name of a CONSTNAME constant cannot be changed once the CONSTNAME object is created. The VALUE constant VALUE, it can be a specific value or a constant expression.

Class T {public: T (): I (0) {}; int I ;}; int getValue () {return 12 ;}void PointerTest () {T t; const t constname = t; // use the int value of the T-type constant object = 5; const int MIN = value; // use a constant value variable to initialize the const object. const int MAX = 10; // you can use a numeric value to initialize int max = MIN; // you can also use a constant value to assign a const int CONST_VALUE = getValue () to a non-const variable. // use a constant expression to initialize the const variable. cout <t. I <endl; cout <MIN <endl; cout <MAX <endl; cout <CONST_VALUE <endl ;}

Definition and use of const constants in a class:

Class Card {public:/***** red peach (Red HEART) */static const int HEART = 0;/*** square */static const int DIAMOND = 1; /*** black plum */static const int BLACKBERRY = 2;/*** black peach */static const int SPADE = 3; /*************************************** ********************************** define other content /*************************************** *********************************/}; int main () {cout <Card: BLACKBERRY <endl; return 0 ;}



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.