Primitive Types and Strings - Fall 2024 CS 18000-GLD - Merge

CS18000: Problem Solving and Object-Oriented Programming

Primitive Types and Strings

Video 1: Data Types

Overview of Data Types

  • Primitive Types: Built into the language (boolean, byte, short, int, long, float, double, char)

  • Reference Types: User-defined, hold references to objects

Values, Variables, and Literals

  • Values are represented in programs by literals and stored in variables

  • Literals Examples:

    • Numbers: 3, -23, 4.5, 0.23, 3E8, 6.02e+23

    • Strings: "Hello there", 'A'

    • Boolean: true, false

  • Variables Examples:

    • Names like X, y, helloMessage, etc. begin with letters; contain letters, digits, and underscores

Types

  • Type: A formal definition, which includes a set of values and operations on those values

  • Example Types: int, double, String

Example: Java Type - int

  • Set of Values: Integers stored in 4 bytes (32 bits)

  • Range: -2,147,483,648 to 2,147,483,647

  • Operations: +, -, /, %, etc.

Type Categories

  • Primitive Types: Directly managed by the language

  • Reference Types: Point to objects, defined by the user

Example Reference Type: String

  • Set of Values: Sequences of characters (Length: 0 to 2,147,483,647)

  • Operations: concat(), toUpperCase(), length(), substring(), etc.

Wheel Class Example

public class Wheel {  
    double radius;  
    Wheel (double radius) {  
        this.radius = radius;  
    }  
    double getCircumference() {  
        return 2 * Math.PI * radius;  
    }  
    double getArea() {  
        return Math.PI * radius * radius;  
    }  
    double getRadius() {  
        return radius;  
    }  
}  
  • Operations: getArea(), getCircumference(), getRadius()

Variables and Literals

  • Variable: A memory location, contents can change

  • Literal: A fixed value that cannot change

Declarations

  • Variables must be declared and given a type

  • Compiler allocates space and ensures valid operations

Primitive Types Overview

Integer Types in Java

  • Represent subsets of integers:

    • byte (8 bits), short (16 bits), int (32 bits), long (64 bits)

  • Most common: int

Real Number Types

  • Float (32 bits), Double (64 bits), most common: double

Operations on Real Types

  • Usual mathematical: +, /

  • Math class offers additional functions: Math.pow(), Math.log10(), etc.

Declaring Variables

  • Examples:

    • int x;

    • int x = 5;

    • int x, y;

  • Best practices: Declare one variable per line with comments

Expressions

  • Built from variables and literals, using operators

  • Mathematical precedence: multiplication/division first; addition/subtraction second

Type Promotion

  • Mixing value types in expressions promotes values when no data is lost

  • Example: 3 + 5.0 results in 8.0

Casting

  • Converting values from one type to another using upcasting or downcasting

  • Upcasting is safe; downcasting can lead to loss of precision

Constructors and Fields

  • Constructor: A special method used to create an object

  • Fields: Member variables within class definitions

Primitive Type: char

  • Encoded as numbers (e.g., 'A' is 65)

  • Java uses 16 bits per character

char Operations

  • Treated as String for concatenation

  • Treated as integer for arithmetic

Useful Character Methods

  • Examples: Character.isDigit(), Character.toUpperCase(), etc.

Primitive Type: boolean

  • Two values: true or false

  • Operations: logical operations &&, ||, !, etc.

Reference Types Overview

  • Unlike primitive types, reference types allow user-defined objects

  • Declaring a reference type variable allocates space for the reference, not the object

Important Reference Type: String

  • Built-in String class, supports String literals

Operations on Strings

  • Concatenation, immutable operations

Comparing Strings

  • Use String.equals() instead of ==

Formatting Strings

  • Using escape sequences: %s, %d, %f

The final Keyword

  • Used to create constants: once declared, cannot be changed

Wrapper Classes and Useful Methods

  • Examples: Byte, Short, Integer, Long, Float, Double, Character, Boolean

String to Numeric Conversion

  • Integer.parseInt(), Double.parseDouble(), etc.

  • Be cautious of NumberFormatException