Other primitives and Strings

Introduction to Java

Jim White

Java Developer

Primitives continued

char and boolean data types

Introduction to Java

booleans

boolean values are true and false

Introduction to Java

booleans - how they work?

Is the item on sale? Yes = true, No = false

Introduction to Java

booleans - naming convention

Boolean variable names should start with is, such as isOnSale

Introduction to Java

char

  • Stores single character
  • Wrapped in single quotes
// Examples
char initial = 'J';

char size = 'M';

char ampersand = '&';
Introduction to Java

char versus String

char

  • Single quotes

    char initial = 'J';
    
  • Primitive data type

  • Stores one character

String

  • Double quotes

    String name = "Jim";
    
  • Not a primitive, collection of characters

  • Stores text
Introduction to Java

Strings

  • Collection of characters inside double quotes
  • Any number of characters
    • Single character in double quotes is String
    • Single character in single quotes is char
  • Not primitives
    • Come with built-in functionality

Primitives are like ingredients, while a String is a meal made from those ingredients

Introduction to Java

Storing Strings

String variableName = "Text to store";
//Example
String myName = "Jim White";

Remember to use double quotes

Introduction to Java

Summary of primitives

Table of primitive types

Introduction to Java

Summary of Strings

Strings are a collection of characters using double quotes

Introduction to Java

Let's practice!

Introduction to Java

Preparing Video For Download...