no title...

Page 51

Figure 2.17 – Declare and Initialize Constants

Program Output: The total of hours in a week is 168

Lines six and seven declare and initialize Constants using keyword modifier final. Constant name DAYS_IN_WEEK assigned “7”, while DAILY_MAX_HOURS assigned “24”. The assigned Constant values will not change in the program. An error states “The final local variable NAME_OF_CONSTANT cannot be assigned” if there is an attempt to change a Constant.

Default Constant Data Types Data types int and double are default Constant types in their respective category. However, the default type can be modified by appending a letter of the target type. An int data type changes to a long data type by attaching the letter “l” or “L.” For example, a value of 34 indicates an int data type by default but 34l or 34L indicates a long data type. The same is true with a double data type. A value of 12.34 specifies a double data type by default, but 12.34f or 12.34F specifies a float data type.

Note: This section points out the default constant data types. However, a constant can be declared as any primitive data type.

Escape Characters Escape characters (known as backslash character constants) represent a group of characters and non-graphical characters. According to Beginning Java® Programming (2015), “escape characters are used for displaying text in specific ways, either for inserting tabs or enters where desired, or by displaying a character that’s normally reserved for code syntax” (page 29).


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.