CS 121 - Chapter 2: Primitive Data and the for loop

Java Variables

  • Primitive Types: Java's fundamental building blocks for data storage, directly holding simple values in memory. There are 8 such types: int, double, boolean, char, float, byte, short, long.

  • Primitive Type Details: These types are pre-defined by Java and are not objects. They store simple values directly in their memory location.

    • int: Represents 32-bit signed integers. Its range is from 231-2^{31} to (2311)(2^{31}-1), approximately 2.147extbillion-2.147 ext{ billion} to 2.147extbillion2.147 ext{ billion}. The default value is 00. Examples: 42,3,042, -3, 0.

    • double: Represents 64-bit double-precision floating-point numbers. It's used for real numbers and offers a very large range up to approximately 1030810^{308} with high precision. The default value is 0.0d0.0d. Examples: 3.1,0.253.1, -0.25.

    • char: Represents a single 16-bit Unicode character. Values are enclosed in single quotes. The default value is \u0000 (the null character). Example: char myChar = 'X';

    • boolean: Represents logical values, either true or false. It's conceptually 1 bit but its size can vary depending on the JVM implementation. The default value is false.

    • byte: Represents 8-bit signed integers. Its range is from 128-128 to 127127. The default value is 00.

    • short: Represents 16-bit signed integers. Its range is from 32768-32768 to 3276732767. The default value is 00.

    • long: Represents 64-bit signed integers. Its range is from 263-2^{63} to (2631)(2^{63}-1), a significantly larger range than int. The default value is 0L0L.

    • float: Represents 32-bit single-precision floating-point numbers. It has less precision than double. The default value is 0.0f0.0f. It's useful for memory-constrained applications.

Declaration and Assignment of Variables

  • Variable Declaration: This process reserves a name and memory space for a variable of a specific type. You must declare a variable before you can use it.

    • Syntax: variable_type name_of_variable;

    • Examples: int variable1; double myVariable;

  • Variable Assignment: This process gives a value to a declared variable using the assignment operator (=).

    • Syntax: name_of_variable = expression;

    • Example: a = 4;

  • Variable Initialization: This is a combination of declaration and assignment in a single statement. It's good practice to initialize variables to prevent potential errors.

    • Syntax: variable_type name_of_variable = expression;

    • Example: int count = 10;

Escape Sequences

  • Purpose: Escape sequences are special character combinations within string literals that represent characters that are difficult or impossible to type directly. They always start with a backslash (\\).

  • Common Escape Sequences:

    • \n - Newline (moves the cursor to the next line)

    • \t - Tab (inserts a horizontal tab space)

    • \" - Double quote (allows embedding a double quote within a double-quoted string)

    • \' - Single quote (allows embedding a single quote within a single-quoted character literal, or string)

    • \\ - Backslash (allows embedding a backslash character itself)

Strings

  • Strings are not primitives: Unlike the 8 primitive types, String is a class in Java (java.lang.String), meaning string variables hold references to objects, not the values directly. String objects are immutable, meaning their content cannot be changed after creation.

  • Defined as: String myString = "This is a string"; or String anotherString; anotherString = "Hello";

Variable Naming Conventions

  • Rules: Adhering to these rules is mandatory for variable names:

    • Must start with a letter, dollar sign ($), or an underscore (_).

    • Cannot begin with a number.

    • Cannot be a reserved Java keyword (e.g., public, static, int).

    • Are case-sensitive (e.g., myVar is different from myvar).

  • Common Practices: Following these conventions improves code readability and maintainability for other developers (and your future self):

    • Use lowercase for variable names (e.g., quantity, userName).

    • If using multiple words, subsequent words can be capitalized using camelCase.

    • Constants (values that do not change) are typically named in uppercase with underscores separating words (e.g., MAX_VALUE, PI).

Camel Case Naming Convention

  • Usage: Camel case is a widely adopted convention where words are combined into a single identifier, with the first letter of each subsequent word capitalized to improve readability.

  • Standard Conventions:

    • Variables: Use camelCase (also known as lower camel case), where the first letter of the first word is lowercase, and the first letter of subsequent words is uppercase (e.g., string firstName, int studentIdNumber).

    • Classes/Interfaces: Use PascalCase (also known as upper camel case), where the first letter of every word, including the first, is uppercase (e.g., class Customer, interface Serializable).

    • Methods: Use camelCase with verbs describing actions for clarity (e.g., public static void calculateTax(), private String getUserName()).

Conclusion

  • End of Lecture: This lecture has provided a comprehensive overview of Java's variable types, including primitive and String types, their declaration and assignment, and crucial naming conventions. A firm understanding of these concepts is essential for writing effective and maintainable Java code.