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 to , approximately to . The default value is . Examples: .double: Represents 64-bit double-precision floating-point numbers. It's used for real numbers and offers a very large range up to approximately with high precision. The default value is . Examples: .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, eithertrueorfalse. It's conceptually 1 bit but its size can vary depending on the JVM implementation. The default value isfalse.byte: Represents 8-bit signed integers. Its range is from to . The default value is .short: Represents 16-bit signed integers. Its range is from to . The default value is .long: Represents 64-bit signed integers. Its range is from to , a significantly larger range thanint. The default value is .float: Represents 32-bit single-precision floating-point numbers. It has less precision thandouble. The default value is . 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,
Stringis a class in Java (java.lang.String), meaning string variables hold references to objects, not the values directly.Stringobjects are immutable, meaning their content cannot be changed after creation.Defined as:
String myString = "This is a string";orString 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.,
myVaris different frommyvar).
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
camelCasewith 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.