1 of 155

Primitive data types, variables, and printing

Arjun Chandrasekhar — CSC 54-184

Material adapted from Dr. Niema Moshiri

2 of 155

Today’s Goals

3 of 155

Primitive data types

4 of 155

Primitive Data Types

  • Primitive Data Types are the lowest-level ways we can represent data in Java

Data Type

Size

Description

byte

1 byte

Stores whole numbers from -2⁷ to 2⁷-1

short

2 bytes

Stores whole numbers from -2¹⁵ to 2¹⁵-1

int

4 bytes

Stores whole numbers from -2³¹ to 2³¹-1

long

8 bytes

Stores whole numbers from -2⁶³ to 2⁶³-1

float

4 bytes

Stores fractional numbers up to 6-7 decimal digits

double

8 bytes

Stores fractional numbers up to 15 decimal digits

boolean

1 bit (depends)

Stores true or false values

char

2 bytes

Stores a single character/letter or ASCII values

Integer Numbers

Fractional Numbers

5 of 155

Which Primitive Data Type should I use?

  • Think about all the possible values your problem can hold
    • If I’m storing human ages, a byte would be sufficient (oldest verified person was 122 years old)
    • If i’m storing the number of CS 1 students, a short would be sufficient (~150 students)
    • If I’m storing the number of Southwestern alumni, an int would be sufficient (not more than 2 billion)
    • If I’m storing the number of bases in the human genome, a long would be sufficient (~4 billion)
    • For float vs. double, how much precision do you need?
  • Or, just use int for integers and double for decimals like most people do 😑

6 of 155

Primitive data types

Match the data with the most appropriate data type

  1. The number of students in the class
  2. The number of atoms in the world
  3. My middle initial
  4. Whether or not today is a weekday
  5. My grade point average
  • double
  • int
  • boolean
  • long
  • char

7 of 155

Primitive data types

Match the data with the most appropriate data type

  • The number of students in the class
  • The number of atoms in the world
  • My middle initial
  • Whether or not today is a weekday
  • My grade point average
  • double
  • int
  • boolean
  • long
  • char

8 of 155

Strings

9 of 155

Strings

10 of 155

Strings

  • A String is a sequence of characters

11 of 155

Strings

  • A String is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on

12 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String

13 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String

“Hello!”.length() → 6

14 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)

15 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)

“Hello!”.charAt(4) → ‘o’

16 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)

“Hello!”.charAt(6) → IndexOutOfBoundsError

17 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String

18 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String

“Hello!”.replace(‘l’, ‘x’) → “Hexxo!”

19 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)

20 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)

“Hello!”.substring(1, 4) → “ell”

21 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)

“Hello!”.substring(0, 2) → “He”

22 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)

“Hello!”.substring(3, 6) → “lo!”

23 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)

“Hello!”.substring(3, 6) → “lo!”

24 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)

“Hello!”.substring(3, 7) → IndexOutOfBoundsException

25 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

26 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

“Hello!”.indexOf(“H”) → 0

27 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

“Hello!”.indexOf(“l”) → 2

28 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

“Hello!”.indexOf(“lo!”) → 3

29 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

“Hello!”.indexOf(“ello”) → 1

30 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

“Hello!”.indexOf(“Hi”) → -1

31 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

“Hello!”.indexOf(“h”) → -1

32 of 155

Strings

  • A string is a sequence of characters
  • Strings are not primitive, but they are useful to discuss early on
    • length() returns the number of characters in the String
    • charAt(i) returns the char at index i of the String (0-based indexing)
    • replace(a, b) replaces all instances of char a with char b in the String
    • substring(a, b) obtains the characters between position a and b (exclusive)
    • indexOf(a) obtains the index of the first occurrence of a (or -1 if it doesn’t exist in the String)

Check the Java String documentation for more!

33 of 155

Java String methods

What is “Star Wars”.length()?

  1. 8. There are 9 characters, but indexing starts at 0
  2. 8. There are only 8 characters because the space doesn’t count
  3. 9. There are 9 characters (including the space)
  4. 11, including the quotation marks
  5. Something else

34 of 155

Java String methods

What is “Star Wars”.length()?

  • 8. There are 9 characters, but indexing starts at 0
  • 8. There are only 8 characters because the space doesn’t count
  • 9. There are 9 characters (including the space)
  • 11, including the quotation marks
  • Something else

35 of 155

Java String methods

What is “Star Wars”.charAt(3)?

  • ‘a’
  • ‘r’
  • ‘ ’ (i.e. a single space)
  • ‘W’
  • Something else

36 of 155

Java String methods

What is “Star Wars”.charAt(3)?

  • ‘a’
  • ‘r’
  • ‘ ’ (i.e. a single space)
  • ‘W’
  • Something else

012345678

37 of 155

Java String methods

What is “Star Wars”.charAt(9)?

  • ‘S’
  • ‘s’
  • ‘r’
  • Something else
  • Error

38 of 155

Java String methods

What is “Star Wars”.charAt(9)?

  • ‘S’
  • ‘s’
  • ‘r’
  • Something else
  • Error

012345678

Out of bounds!

39 of 155

Java String methods

What is “Star Wars”.substring(1, 6)?

40 of 155

Java String methods

What is “Star Wars”.substring(1, 6)?

0123456789

41 of 155

Java String methods

How would we use substring to obtain the last 4 characters of “Star Wars”?

42 of 155

Java String methods

How would we use subtring to obtain the last 4 characters of “Star Wars”?

“Star Wars”.substring(5, 9)

0123456789

43 of 155

Java String methods

What is “Star Wars”.indexOf(“Wars”)?

44 of 155

Java String methods

What is “Star Wars”.indexOf(“Wars”)?

0123456789

45 of 155

Java String methods

What is “Star Wars”.indexOf(“Star”)?

46 of 155

Java String methods

What is Star Wars”.indexOf(“Star”)?

0123456789

47 of 155

Java String methods

What is “Star Wars”.indexOf(“Vader”)?

48 of 155

Java String methods

What is Star Wars”.indexOf(“Vader”)?

No match, returns -1

49 of 155

Printing

50 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line

51 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line

System.out.print(“Hello”);

System.out.print(“Newman”);

Code

Console

52 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line

System.out.print(“Hello”);

System.out.print(“Newman”);

Code

Hello

Console

53 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line

System.out.print(“Hello”);

System.out.print(“Newman”);

Code

HelloNewman

Console

54 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line

System.out.print(“Hello”);

System.out.print(“Newman”);

Code

HelloNewman

Console

55 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line

56 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line

System.out.println(“Hello”);

System.out.println(“Newman”);

Code

Console

57 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line

System.out.println(“Hello”);

System.out.println(“Newman”);

Code

Hello

Console

58 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line

System.out.println(“Hello”);

System.out.println(“Newman”);

Code

Hello

Console

Insert a line break

59 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line

System.out.println(“Hello”);

System.out.println(“Newman”);

Code

Hello

Newman

Console

60 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line

System.out.println(“Hello”);

System.out.println(“Newman”);

Code

Hello

Newman

Console

Insert a line break

61 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line

System.out.println(“Hello”);

System.out.println(“Newman”);

Code

Hello

Newman

Console

62 of 155

Printing to the Console

  • In Java (and most languages), code runs silently
  • When we want to actually display something to the user, we can print it
  • We typically use one of two Java functions:
    • System.out.print(myVar) prints the value of myVar to the user’s console
      • However, it won’t end the line, so any other print statements will occur on the same line
    • System.out.println(myVar) prints the value of myVar and goes to the next line
  • Usually, you want to use System.out.println(...)

63 of 155

How many lines will be printed?

System.out.print(“One”);

System.out.print(“Two”);

System.out.println(“Three”);

System.out.print(“Four”);

64 of 155

How many lines will be printed?

One

Console

System.out.print(“One”);

System.out.print(“Two”);

System.out.println(“Three”);

System.out.print(“Four”);

65 of 155

How many lines will be printed?

OneTwo

Console

System.out.print(“One”);

System.out.print(“Two”);

System.out.println(“Three”);

System.out.print(“Four”);

66 of 155

How many lines will be printed?

OneTwoThree

Console

System.out.print(“One”);

System.out.print(“Two”);

System.out.println(“Three”);

System.out.print(“Four”);

67 of 155

How many lines will be printed?

OneTwoThree

Console

System.out.print(“One”);

System.out.print(“Two”);

System.out.println(“Three”);

System.out.print(“Four”);

Insert a line break

68 of 155

How many lines will be printed?

OneTwoThree

Four

Console

System.out.print(“One”);

System.out.print(“Two”);

System.out.println(“Three”);

System.out.print(“Four”);

69 of 155

How many lines will be printed?

OneTwoThree

Four

Console

System.out.print(“One”);

System.out.print(“Two”);

System.out.println(“Three”);

System.out.print(“Four”);

2 lines

70 of 155

What will be the second line of printed output?

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

71 of 155

What will be the second line of printed output?

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

72 of 155

What will be the second line of printed output?

Eject

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

73 of 155

What will be the second line of printed output?

Ejecto

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

74 of 155

What will be the second line of printed output?

Ejecto

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

Insert a line break

75 of 155

What will be the second line of printed output?

Ejecto

Seato

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

76 of 155

What will be the second line of printed output?

Ejecto

Seato

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

Insert a line break

77 of 155

What will be the second line of printed output?

Ejecto

Seato

Cuz

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

78 of 155

What will be the second line of printed output?

Ejecto

Seato

Cuz

Console

System.out.print(“Eject”);

System.out.println(“o”);

System.out.println(“Seato”);

System.out.print(“Cuz”);

79 of 155

Escape sequences

80 of 155

Escape characters

  • What if I want to print “Hello, Newman!” (including the quotes)
  • System.out.println(““Hello, Newman!””);
    • The compiler won’t like this
    • System.out.println(“”Hello, Newman!“”);
  • So how do we print this?
    • Escape characters to the rescue!

81 of 155

Some common escape characters

  • \n - newline
  • \’ - single quotation mark within a string
  • \” - double quotation mark within a string
  • \t - tab
  • \\ - backslash (i.e. insert a literal backslash into a string)

82 of 155

Escape characters

  • What if I want to print “Hello, Newman!” (including the quotes)
  • System.out.println(“\“Hello, Newman!\””);

83 of 155

Some notes about printing

  • When you use print or println, you will generally want to to put a String value inside the parentheses
  • However, you can print any kind of data
    • Primitive data will automatically be converted into a String
    • System.out.println(2);
    • System.out.println(‘c’);
    • System.out.println(true);
  • Later we will see how to combine Strings and primitive data for printing and other purposes!

84 of 155

Variables

85 of 155

What’s wrong with this code?

System.out.println(“The area of a circle with radius 1 is:”);

System.out.println(3 * 1 * 1);

System.out.println(“The area of a circle with radius 10 is:”);

System.out.println(3 * 10 * 10);

System.out.println(“The area of a circle with radius 50 is:”);

System.out.println(3 * 50 * 50);

86 of 155

What’s wrong with this code?

System.out.println(“The area of a circle with radius 1 is:”);

System.out.println(3 * 1 * 1);

System.out.println(“The area of a circle with radius 10 is:”);

System.out.println(3 * 10 * 10);

System.out.println(“The area of a circle with radius 50 is:”);

System.out.println(3 * 50 * 50);

What does the value 3 refer to?

Pi should be 3.14!

87 of 155

What’s wrong with this code?

System.out.println(“The area of a circle with radius 1 is:”);

System.out.println(3 3.14 * 1 * 1);

System.out.println(“The area of a circle with radius 10 is:”);

System.out.println(3 3.14 * 10 * 10);

System.out.println(“The area of a circle with radius 50 is:”);

System.out.println(3 3.14 * 50 * 50);

What does the value 3 refer to?

Pi should be 3.14!

I have to change a lot of lines of code

88 of 155

D.R.Y. Coding

Don’t

Repeat

Yourself

89 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)

int meaningOfLife;

90 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)

int meaningOfLife;

91 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)

int meaningOfLife;

92 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)
  • Variables are then initialized (assigns the value)

int meaningOfLife;

meaningOfLife = 42;

93 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)
  • Variables are then initialized (assigns the value)

int meaningOfLife;

meaningOfLife = 42;

94 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)
  • Variables are then initialized (assigns the value)
  • We can declare and initialize a variable simultaneously

int meaningOfLife = 42;

95 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)
  • Variables are then initialized (assigns the value)
  • We can declare and initialize a variable simultaneously

int meaningOfLife = 42;

96 of 155

Variables

  • Variable: A location in memory that stores data
    • It has an associated name
    • In Java (and some other languages), it has a type
  • Variables are first declared (specifies the name and type)
  • Variables are then initialized (assigns the value)
  • We can declare and initialize a variable simultaneously

int meaningOfLife = 42;

97 of 155

int meaningOfLife = 42;

98 of 155

int meaningOfLife = 42;

int

99 of 155

int meaningOfLife = 42;

meaningOfLife:

int

100 of 155

int meaningOfLife = 42;

42

meaningOfLife:

int

101 of 155

int meaningOfLife = 42;

42

meaningOfLife:

int

102 of 155

Variable components

Match up terms

int meaningOfLife = 42;

  1. Variable name
  2. Variable type
  3. Variable value
  4. Variable size
  • int
  • meaningOfLife
  • 4 bytes
  • 42

103 of 155

Variable components

Match up terms

int meaningOfLife = 42;

  • Variable name
  • Variable type
  • Variable value
  • Variable size
  • int
  • meaningOfLife
  • 4 bytes
  • 42

104 of 155

Variables

Why use variables?

  • Avoid changing the same value in multiple code locations
  • Make code more readable
  • Variables can be a placeholder for values that will be determined later

double pi = 3;

System.out.println(“The area of a circle with radius 1 is:”);

System.out.println(pi * 1 * 1);

System.out.println(“The area of a circle with radius 10 is:”);

System.out.println(pi * 10 * 10);

System.out.println(“The area of a circle with radius 50 is:”);

System.out.println(pi * 50 * 50);

105 of 155

Variables

Why use variables?

  • Avoid changing the same value in multiple code locations
  • Make code more readable
  • Variables can be a placeholder for values that will be determined later

double pi = 3 3.14;

System.out.println(“The area of a circle with radius 1 is:”);

System.out.println(pi * 1 * 1);

System.out.println(“The area of a circle with radius 10 is:”);

System.out.println(pi * 10 * 10);

System.out.println(“The area of a circle with radius 50 is:”);

System.out.println(pi * 50 * 50);

106 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

1

numStarWarsMovies:

int

107 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

1 2

numStarWarsMovies:

int

108 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

numStarWarsMovies = 3; // 1982

2 3

numStarWarsMovies:

int

109 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

numStarWarsMovies = 3; // 1982

numStarWarsMovies = 4; // 1999

3 4

numStarWarsMovies:

int

110 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

numStarWarsMovies = 3; // 1982

numStarWarsMovies = 4; // 1999

numStarWarsMovies = 5; // 2002

4 5

numStarWarsMovies:

int

111 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

numStarWarsMovies = 3; // 1982

numStarWarsMovies = 4; // 1999

numStarWarsMovies = 5; // 2002

numStarWarsMovies = 6; // 2005

5 6

numStarWarsMovies:

int

112 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

numStarWarsMovies = 3; // 1982

numStarWarsMovies = 4; // 1999

numStarWarsMovies = 5; // 2002

numStarWarsMovies = 6; // 2005

numStarWarsMovies = 7; // 2015

6 7

numStarWarsMovies:

int

113 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

numStarWarsMovies = 3; // 1982

numStarWarsMovies = 4; // 1999

numStarWarsMovies = 5; // 2002

numStarWarsMovies = 6; // 2005

numStarWarsMovies = 7; // 2015

numStarWarsMovies = 8; // 2017

7 8

numStarWarsMovies:

int

114 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want

int numStarWarsMovies = 1; // 1977

numStarWarsMovies = 2; // 1980

numStarWarsMovies = 3; // 1982

numStarWarsMovies = 4; // 1999

numStarWarsMovies = 5; // 2002

numStarWarsMovies = 6; // 2005

numStarWarsMovies = 7; // 2015

numStarWarsMovies = 8; // 2017

numStarWarsMovies = 9; // 2019

8 9

numStarWarsMovies:

int

115 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want
  • However, we can never change the type of a variable after we declare it

116 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want
  • However, we can never change the type of a variable after we declare it

int numStarWarsMovies = 1; // 1978

numStarWarsMovies = 2; // 1980

2

numStarWarsMovies:

int

117 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want
  • However, we can never change the type of a variable after we declare it

int numStarWarsMovies = 1; // 1978

numStarWarsMovies = 2; // 1980

double numStarWarsMovies = 3.0;

118 of 155

Types are Forever

  • We can reassign the value of a variable as many times as we want
  • However, we can never change the type of a variable after we declare it

int numStarWarsMovies = 1; // 1978

numStarWarsMovies = 2; // 1980

double numStarWarsMovies = 3.0;

Compile Error!!!

119 of 155

Assigning a Variable from Another Variable

120 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant

121 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant

int meaningOfLife = 42;

122 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead

123 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

124 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

125 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

126 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

int

127 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

first:

int

128 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

129 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

130 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

131 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

int

132 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

second:

int

133 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

second:

int

134 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

second:

int

135 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

second:

int

136 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

42

second:

int

137 of 155

Assigning a Variable from Another Variable

  • Thus far, the value we used to assign a variable was a constant
  • However, we could have used another variable instead
  • When we do this, we copy the value of the other variable into our variable

int first = 42;

int second = first;

42

first:

int

42

second:

int

138 of 155

What is the value of var1 after this code runs?

  1. 0
  2. 42
  3. 100
  4. 420
  5. Depends on what order things happen

int var1 = 42;

int var2 = 420;

var1 = var2;

var2 = 100;

139 of 155

What is the value of var1 after this code runs?

  • 0
  • 42
  • 100
  • 420
  • Depends on what order things happen

int var1 = 42;

int var2 = 420;

var1 = var2;

var2 = 100;

42

var1:

int

140 of 155

What is the value of var1 after this code runs?

  • 0
  • 42
  • 100
  • 420
  • Depends on what order things happen

int var1 = 42;

int var2 = 420;

var1 = var2;

var2 = 100;

42

var1:

int

420

var2:

int

141 of 155

What is the value of var1 after this code runs?

  • 0
  • 42
  • 100
  • 420
  • Depends on what order things happen

int var1 = 42;

int var2 = 420;

var1 = var2;

var2 = 100;

42 420

var1:

int

420

var2:

int

142 of 155

What is the value of var1 after this code runs?

  • 0
  • 42
  • 100
  • 420
  • Depends on what order things happen

int var1 = 42;

int var2 = 420;

var1 = var2;

var2 = 100;

420

var1:

int

420 100

var2:

int

143 of 155

What is the value of var1 after this code runs?

  • 0
  • 42
  • 100
  • 420
  • Depends on what order things happen

int var1 = 42;

int var2 = 420;

var1 = var2;

var2 = 100;

420

var1:

int

100

var2:

int

144 of 155

What is the value of var2 after this code runs?

  • 31
  • 75
  • Depends on what order things happen
  • Something else
  • Error

int var1 = 75;

int var2 = 31;

var1 = var2;

var2 = var1;

145 of 155

What is the value of var2 after this code runs?

  • 31
  • 75
  • Depends on what order things happen
  • Something else
  • Error

int var1 = 75;

int var2 = 31;

var1 = var2;

var2 = var1;

75

var1:

int

146 of 155

What is the value of var2 after this code runs?

  • 31
  • 75
  • Depends on what order things happen
  • Something else
  • Error

int var1 = 75;

int var2 = 31;

var1 = var2;

var2 = var1;

75

var1:

int

31

var2:

int

147 of 155

What is the value of var2 after this code runs?

  • 31
  • 75
  • Depends on what order things happen
  • Something else
  • Error

int var1 = 75;

int var2 = 31;

var1 = var2;

var2 = var1;

75 31

var1:

int

31

var2:

int

148 of 155

What is the value of var2 after this code runs?

  • 31
  • 75
  • Depends on what order things happen
  • Something else
  • Error

int var1 = 75;

int var2 = 31;

var1 = var2;

var2 = var1;

31

var1:

int

31 31

var2:

int

149 of 155

What is the value of var2 after this code runs?

  • 31
  • 75
  • Depends on what order things happen
  • Something else
  • Error

int var1 = 75;

int var2 = 31;

var1 = var2;

var2 = var1;

31

var1:

int

31

var2:

int

150 of 155

What is the value of ch after this code runs?

  • ‘r’
  • ‘j’
  • ‘u’
  • Depends on what order things happen
  • Error: charAt requires a number, not a variable
  • Some other error
  • Something else

String str = “Arjun”;

int pos = 2;

char ch = str.charAt(pos);

pos = 3;

151 of 155

What is the value of ch after this code runs?

  • ‘r’
  • ‘j’
  • ‘u’
  • Depends on what order things happen
  • Error: charAt requires a number, not a variable
  • Some other error
  • Something else

String str = “Arjun”;

int pos = 2;

char ch = str.charAt(pos);

pos = 3;

“Arjun”

str:

String

152 of 155

What is the value of ch after this code runs?

  • ‘r’
  • ‘j’
  • ‘u’
  • Depends on what order things happen
  • Error: charAt requires a number, not a variable
  • Some other error
  • Something else

String str = “Arjun”;

int pos = 2;

char ch = str.charAt(pos);

pos = 3;

“Arjun”

str:

String

2

pos:

int

153 of 155

What is the value of ch after this code runs?

  • ‘r’
  • ‘j’
  • ‘u’
  • Depends on what order things happen
  • Error: charAt requires a number, not a variable
  • Some other error
  • Something else

String str = “Arjun”;

int pos = 2;

char ch = str.charAt(pos);

pos = 3;

“Arjun”

str:

String

2

pos:

int

‘j’

ch:

char

0-based indexing!

A r j u n

0 1 2 3 4

154 of 155

What is the value of ch after this code runs?

  • ‘r’
  • ‘j’
  • ‘u’
  • Depends on what order things happen
  • Error: charAt requires a number, not a variable
  • Some other error
  • Something else

String str = “Arjun”;

int pos = 2;

char ch = str.charAt(pos);

pos = 3;

“Arjun”

str:

String

2 3

pos:

int

‘j’

ch:

char

pos gets modified

ch does not!

155 of 155

What is the value of ch after this code runs?

  • ‘r’
  • ‘j’
  • ‘u’
  • Depends on what order things happen
  • Error: charAt requires a number, not a variable
  • Some other error
  • Something else

String str = “Arjun”;

int pos = 2;

char ch = str.charAt(pos);

pos = 3;

“Arjun”

str:

String

3

pos:

int

‘j’

ch:

char