Data Types, Variables, and Identifiers, CSE 101 – Study Notes
offline

Source: CSE 101 Final Exam Review, Arizona State University

Tags: Java data types, primitive types, identifiers, variable naming, type casting, CSE 101, ASU, reserved words, byte short int long float double char boolean

Difficulty: Beginner | Prerequisites: None, this is foundational material for the entire course.

Big Picture

Every Java programme starts with data: numbers, characters, and true/false flags. Before you can write a single method or loop, you need to know what kinds of data Java can hold, how to name your variables without the compiler shouting at you, and how Java converts between types when you mix them. This material underpins everything else in CSE 101, from conditionals to object-oriented design. If you understand types, identifiers, and casting, the rest of the course makes considerably more sense.

TL;DR

Java has eight primitive data types (byte, short, int, long, float, double, char, boolean) plus reference types like String. Variable names (identifiers) must start with a letter, underscore, or dollar sign and cannot be reserved words. Java will automatically widen a smaller type to a larger one, but narrowing (e.g. double to int) requires an explicit cast and truncates the decimal.


Key Terms

Primitive data type

A built-in type that holds a single value directly in memory, not a reference to an object. Java has exactly eight: byte, short, int, long, float, double, char, and boolean. In simple terms, these are the basic building blocks for storing numbers, characters, and true/false values.

Identifier

The name given to a variable, method, class, or other user-defined element in Java. Must begin with a letter, underscore (_), or dollar sign ($), followed by letters, digits, underscores, or dollar signs. Think of it as the label you stick on a box so you can find it later.

Reserved word (keyword)

A word that Java has claimed for its own syntax and cannot be used as an identifier. Examples: int, class, public, static, void, return, if, else, while. In simple terms, these words are already taken by the language, so you cannot name your variables after them.

Type casting

Converting a value from one data type to another. Widening (e.g. int to double) happens automatically. Narrowing (e.g. double to int) must be done explicitly with a cast operator and may lose information. Think of it as pouring liquid from a small cup into a big one (safe, automatic) versus squeezing from a big cup into a small one (you have to force it, and some spills).

String

A reference type (not a primitive) that represents a sequence of characters. Declared with a capital S: String name = "Harry";. Strings are objects with methods like indexOf(), substring(), and equals(). In simple terms, a String is how Java handles text.

boolean

A primitive type that holds exactly one of two values: true or false. Used for conditions, comparisons, and logical expressions. Think of it as a light switch: on or off, nothing in between.


Core Content: Java Primitive Data Types

Java's eight primitive types fall into four categories.

  • Integer types (whole numbers)

    • byte: 8 bits, range -128 to 127

    • short: 16 bits, range -32,768 to 32,767

    • int: 32 bits, the default for whole numbers, range roughly -2.1 billion to 2.1 billion

    • long: 64 bits, for very large whole numbers

  • Floating-point types (decimals)

    • float: 32 bits, single precision

    • double: 64 bits, double precision, the default for decimal numbers

  • Character type

    • char: 16 bits, holds a single Unicode character, declared with single quotes: 'A'

  • Boolean type

    • boolean: holds true or false only

The reference type String (capital S) is not a primitive. It holds a sequence of characters in double quotes: "Bob Smith". String comparison uses .equals(), not ==.

Example from the notes:

String Name1 = "Bob Smith";
String Name2 = "bob smith";
boolean b = Name1.equals(Name2);  // false, case-sensitive

Core Content: Identifier Rules and Naming

An identifier is any name you create in Java: variables, methods, classes.

  • Allowed characters

    • Must start with a letter (a-z, A-Z), underscore (_), or dollar sign ($)

    • After the first character, digits (0-9) are also allowed

    • No spaces, no other special characters

  • Cannot be a reserved word

    • Words like int, double, class, public, static, void, if, else, while, return are reserved by Java

    • You cannot name a variable "int" or "class"

  • Case-sensitive

    • myVar and myvar are two different identifiers

    • numPeople and NumPeople are different

  • Convention (not enforced by the compiler, but expected)

    • Variables and methods: camelCase (e.g. numPeople, firstName)

    • Classes: PascalCase (e.g. Scanner, String)

    • Constants: ALL_CAPS (e.g. Math.PI)

Example from the notes:

numPeople += 5;  // valid identifier, camelCase

Core Content: Type Casting and Conversions

  • Widening (implicit, automatic)

    • Going from a smaller type to a larger type: byte → short → int → long → float → double

    • No data is lost. Java does this for you automatically.

    • Example: int x = 5; double y = x; // y is 5.0

  • Narrowing (explicit, requires a cast)

    • Going from a larger type to a smaller type: double → int

    • You must write the cast operator: (targetType) value

    • The decimal portion is truncated (chopped off), not rounded

    • Example from the notes:

double number = 5.6;
int newNumber = (int)(number);  // newNumber is 5, not 6
  • Integer division trap

    • When both operands are integers, Java performs integer division and truncates the result

    • 7 / 2 gives 3, not 3.5

    • To get 3.5, cast one operand: (double) 7 / 2

  • Useful casting patterns from the notes

    • Extracting digits: thousandsDigit = (userValue / 1000) % 10

    • Getting last 4 digits of a credit card: x % 10000

    • Getting first N digits: (x / 10) % 10 (for the tens digit of 693, gives 9)

    • Converting metres to feet: int foot = (int)(meter * 3.28); double inches = (meter * 39.37) - (foot * 12);

  • Math.ceil() with casting

    • int cans = (int) Math.ceil(gallons); rounds up and then casts to int


Common Misconceptions

  • Students often think casting a double to an int will round the number. It does not. It truncates (chops off the decimal). (int)(5.9) gives 5, not 6.

  • Students often think String is a primitive type because it is used so frequently. It is not. String is a reference type (an object). This matters for comparison: use .equals(), not ==.

  • Students often think identifiers can start with a digit. They cannot. 2ndPlace is invalid; secondPlace is valid.

  • Students often confuse = (assignment) with == (comparison). boolean b = Name1.equals(Name2) is correct for String comparison, not Name1 == Name2.


Why It Matters / Exam Flags

  • ⚠️ Know all eight primitive types and their sizes. Expect a question asking you to pick the correct type for a given scenario.

  • ⚠️ Type casting with truncation is a classic exam question. Be ready for "what is the output?" with a double-to-int cast.

  • ⚠️ Identifier rules are tested directly: "which of the following is a valid identifier?" Memorise the rules for the first character.

  • ⚠️ Integer division (7 / 2 = 3, not 3.5) appears frequently in "what is the output?" questions.

  • ⚠️ The modulo operator (%) for digit extraction is a recurring exam pattern.


Quick Self-Test

  1. True or False: char is 8 bits wide in Java.

  1. Fill in the blank: An identifier must begin with a ______, ______, or ______.

  1. True or False: (int)(9.99) evaluates to 10.

  1. Fill in the blank: The result of 15 / 4 in Java (both ints) is ______.

  1. True or False: String is a primitive data type in Java.


Practice Q&A

Q: What is the output of the following code?

double number = 5.6;
int newNumber = (int)(number);
System.out.println(newNumber);

A: 5. The cast truncates the decimal; it does not round.

Q: Which of the following is NOT a valid Java identifier: myVar, 2ndValue, _count, $amount?

A: 2ndValue. Identifiers cannot begin with a digit.

Q: What is the value of (userValue / 1000) % 10 when userValue is 4825?

A: 4. Integer division 4825 / 1000 gives 4, then 4 % 10 gives 4. This extracts the thousands digit.

Q: What is the difference between int and double in terms of how Java handles division?

A: When both operands are int, Java performs integer division and discards the remainder (7 / 2 = 3). When at least one operand is double, Java performs floating-point division (7.0 / 2 = 3.5).

Q: Write a line of code that casts a double variable called meter to an int variable called foot using the conversion factor 3.28.

A: int foot = (int)(meter * 3.28);


Connections to Other Topics

This connects to Input/Output (the Scanner class returns specific data types like nextInt(), nextDouble(), nextLine()). Understanding types is essential for method signatures, where you declare the return type and parameter types. Type casting reappears when working with collections and generics later in the course.


Related Terms / Search Tags

Java primitive types, data types in Java, int vs double, type casting Java, narrowing conversion, widening conversion, implicit cast, explicit cast, Java identifiers, reserved words Java, variable naming rules, integer division Java, modulo operator, digit extraction, byte short int long, float double, char boolean, String vs primitive, CSE 101 ASU, Java fundamentals