Literals and Naming
Part of: Primitive Types
What Is a Literal? A literal is a fixed value written directly in source code. The type of a literal is decided by how you write it: - int literal : digits with no decimal point: 42, 0, -7. - double literal : has a decimal point or exponent: 3.14, 2.0, 0.5, 1.5e3. - boolean literal : the reserved words true and false. The distinction matters. 5 is an int literal; 5.0 is a double literal. They look similar but behave differently in arithmetic, as later lessons show. Identifiers and Naming Rules The name of a variable is its identifier . Java requires: - Start with a letter, underscore , or $ (use letters in AP CSA). - Followed by letters, digits, or underscores. - No spaces and no reserved words like int or class. - Identifiers are case-sensitive : total and Total are different. Convention: camelCase By convention, variable names use camelCase : start lowercase, capitalize each later word: Good names describe the data they hold. numStudents is far clearer than x. The exam rewards readable code, and clear naming reduces bugs. final Constants Adding the keyword final makes a variable a constant : its value cannot change after initialization. Constants use ALL CAPS by convention: Attem
Challenge: Rectangle Constant Area