Primitive data types, variables, and printing
Arjun Chandrasekhar — CSC 54-184
Material adapted from Dr. Niema Moshiri
Today’s Goals
Primitive data types
Primitive Data Types
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
Which Primitive Data Type should I use?
Primitive data types
Match the data with the most appropriate data type
Primitive data types
Match the data with the most appropriate data type
Strings
Strings
Strings
Strings
Strings
Strings
“Hello!”.length() → 6
Strings
Strings
“Hello!”.charAt(4) → ‘o’
Strings
“Hello!”.charAt(6) → IndexOutOfBoundsError
Strings
Strings
“Hello!”.replace(‘l’, ‘x’) → “Hexxo!”
Strings
Strings
“Hello!”.substring(1, 4) → “ell”
Strings
“Hello!”.substring(0, 2) → “He”
Strings
“Hello!”.substring(3, 6) → “lo!”
Strings
“Hello!”.substring(3, 6) → “lo!”
Strings
“Hello!”.substring(3, 7) → IndexOutOfBoundsException
Strings
Strings
“Hello!”.indexOf(“H”) → 0
Strings
“Hello!”.indexOf(“l”) → 2
Strings
“Hello!”.indexOf(“lo!”) → 3
Strings
“Hello!”.indexOf(“ello”) → 1
Strings
“Hello!”.indexOf(“Hi”) → -1
Strings
“Hello!”.indexOf(“h”) → -1
Strings
Check the Java String documentation for more!
Java String methods
What is “Star Wars”.length()?
Java String methods
What is “Star Wars”.length()?
Java String methods
What is “Star Wars”.charAt(3)?
Java String methods
What is “Star Wars”.charAt(3)?
012345678
Java String methods
What is “Star Wars”.charAt(9)?
Java String methods
What is “Star Wars”.charAt(9)?
012345678
Out of bounds!
Java String methods
What is “Star Wars”.substring(1, 6)?
Java String methods
What is “Star Wars”.substring(1, 6)?
0123456789
Java String methods
How would we use substring to obtain the last 4 characters of “Star Wars”?
Java String methods
How would we use subtring to obtain the last 4 characters of “Star Wars”?
“Star Wars”.substring(5, 9)
0123456789
Java String methods
What is “Star Wars”.indexOf(“Wars”)?
Java String methods
What is “Star Wars”.indexOf(“Wars”)?
0123456789
Java String methods
What is “Star Wars”.indexOf(“Star”)?
Java String methods
What is “Star Wars”.indexOf(“Star”)?
0123456789
Java String methods
What is “Star Wars”.indexOf(“Vader”)?
Java String methods
What is “Star Wars”.indexOf(“Vader”)?
No match, returns -1
Printing
Printing to the Console
Printing to the Console
System.out.print(“Hello”);
System.out.print(“Newman”);
Code
Console
Printing to the Console
System.out.print(“Hello”);
System.out.print(“Newman”);
Code
Hello
Console
Printing to the Console
System.out.print(“Hello”);
System.out.print(“Newman”);
Code
HelloNewman
Console
Printing to the Console
System.out.print(“Hello”);
System.out.print(“Newman”);
Code
HelloNewman
Console
Printing to the Console
Printing to the Console
System.out.println(“Hello”);
System.out.println(“Newman”);
Code
Console
Printing to the Console
System.out.println(“Hello”);
System.out.println(“Newman”);
Code
Hello
Console
Printing to the Console
System.out.println(“Hello”);
System.out.println(“Newman”);
Code
Hello
Console
Insert a line break
Printing to the Console
System.out.println(“Hello”);
System.out.println(“Newman”);
Code
Hello
Newman
Console
Printing to the Console
System.out.println(“Hello”);
System.out.println(“Newman”);
Code
Hello
Newman
Console
Insert a line break
Printing to the Console
System.out.println(“Hello”);
System.out.println(“Newman”);
Code
Hello
Newman
Console
Printing to the Console
How many lines will be printed?
System.out.print(“One”);
System.out.print(“Two”);
System.out.println(“Three”);
System.out.print(“Four”);
How many lines will be printed?
One
Console
System.out.print(“One”);
System.out.print(“Two”);
System.out.println(“Three”);
System.out.print(“Four”);
How many lines will be printed?
OneTwo
Console
System.out.print(“One”);
System.out.print(“Two”);
System.out.println(“Three”);
System.out.print(“Four”);
How many lines will be printed?
OneTwoThree
Console
System.out.print(“One”);
System.out.print(“Two”);
System.out.println(“Three”);
System.out.print(“Four”);
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
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”);
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
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”);
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”);
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”);
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”);
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
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”);
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
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”);
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”);
Escape sequences
Escape characters
Some common escape characters
Escape characters
Some notes about printing
Variables
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’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!
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
D.R.Y. Coding
Don’t
Repeat
Yourself
Variables
int meaningOfLife;
Variables
int meaningOfLife;
Variables
int meaningOfLife;
Variables
int meaningOfLife;
meaningOfLife = 42;
Variables
int meaningOfLife;
meaningOfLife = 42;
Variables
int meaningOfLife = 42;
Variables
int meaningOfLife = 42;
Variables
int meaningOfLife = 42;
int meaningOfLife = 42;
int meaningOfLife = 42;
int
int meaningOfLife = 42;
meaningOfLife:
int
int meaningOfLife = 42;
42
meaningOfLife:
int
int meaningOfLife = 42;
42
meaningOfLife:
int
Variable components
Match up terms
int meaningOfLife = 42;
Variable components
Match up terms
int meaningOfLife = 42;
Variables
Why use variables?
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);
Variables
Why use variables?
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);
Types are Forever
int numStarWarsMovies = 1; // 1977
1
numStarWarsMovies:
int
Types are Forever
int numStarWarsMovies = 1; // 1977
numStarWarsMovies = 2; // 1980
1 2
numStarWarsMovies:
int
Types are Forever
int numStarWarsMovies = 1; // 1977
numStarWarsMovies = 2; // 1980
numStarWarsMovies = 3; // 1982
2 3
numStarWarsMovies:
int
Types are Forever
int numStarWarsMovies = 1; // 1977
numStarWarsMovies = 2; // 1980
numStarWarsMovies = 3; // 1982
numStarWarsMovies = 4; // 1999
3 4
numStarWarsMovies:
int
Types are Forever
int numStarWarsMovies = 1; // 1977
numStarWarsMovies = 2; // 1980
numStarWarsMovies = 3; // 1982
numStarWarsMovies = 4; // 1999
numStarWarsMovies = 5; // 2002
4 5
numStarWarsMovies:
int
Types are Forever
int numStarWarsMovies = 1; // 1977
numStarWarsMovies = 2; // 1980
numStarWarsMovies = 3; // 1982
numStarWarsMovies = 4; // 1999
numStarWarsMovies = 5; // 2002
numStarWarsMovies = 6; // 2005
5 6
numStarWarsMovies:
int
Types are Forever
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
Types are Forever
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
Types are Forever
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
Types are Forever
Types are Forever
int numStarWarsMovies = 1; // 1978
numStarWarsMovies = 2; // 1980
2
numStarWarsMovies:
int
Types are Forever
int numStarWarsMovies = 1; // 1978
numStarWarsMovies = 2; // 1980
double numStarWarsMovies = 3.0;
Types are Forever
int numStarWarsMovies = 1; // 1978
numStarWarsMovies = 2; // 1980
double numStarWarsMovies = 3.0;
Compile Error!!!
Assigning a Variable from Another Variable
Assigning a Variable from Another Variable
Assigning a Variable from Another Variable
int meaningOfLife = 42;
Assigning a Variable from Another Variable
Assigning a Variable from Another Variable
Assigning a Variable from Another Variable
int first = 42;
int second = first;
Assigning a Variable from Another Variable
int first = 42;
int second = first;
Assigning a Variable from Another Variable
int first = 42;
int second = first;
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
first:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
second:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
second:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
second:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
second:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
42
second:
int
Assigning a Variable from Another Variable
int first = 42;
int second = first;
42
first:
int
42
second:
int
What is the value of var1 after this code runs?
int var1 = 42;
int var2 = 420;
var1 = var2;
var2 = 100;
What is the value of var1 after this code runs?
int var1 = 42;
int var2 = 420;
var1 = var2;
var2 = 100;
42
var1:
int
What is the value of var1 after this code runs?
int var1 = 42;
int var2 = 420;
var1 = var2;
var2 = 100;
42
var1:
int
420
var2:
int
What is the value of var1 after this code runs?
int var1 = 42;
int var2 = 420;
var1 = var2;
var2 = 100;
42 420
var1:
int
420
var2:
int
What is the value of var1 after this code runs?
int var1 = 42;
int var2 = 420;
var1 = var2;
var2 = 100;
420
var1:
int
420 100
var2:
int
What is the value of var1 after this code runs?
int var1 = 42;
int var2 = 420;
var1 = var2;
var2 = 100;
420
var1:
int
100
var2:
int
What is the value of var2 after this code runs?
int var1 = 75;
int var2 = 31;
var1 = var2;
var2 = var1;
What is the value of var2 after this code runs?
int var1 = 75;
int var2 = 31;
var1 = var2;
var2 = var1;
75
var1:
int
What is the value of var2 after this code runs?
int var1 = 75;
int var2 = 31;
var1 = var2;
var2 = var1;
75
var1:
int
31
var2:
int
What is the value of var2 after this code runs?
int var1 = 75;
int var2 = 31;
var1 = var2;
var2 = var1;
75 31
var1:
int
31
var2:
int
What is the value of var2 after this code runs?
int var1 = 75;
int var2 = 31;
var1 = var2;
var2 = var1;
31
var1:
int
31 31
var2:
int
What is the value of var2 after this code runs?
int var1 = 75;
int var2 = 31;
var1 = var2;
var2 = var1;
31
var1:
int
31
var2:
int
What is the value of ch after this code runs?
String str = “Arjun”;
int pos = 2;
char ch = str.charAt(pos);
pos = 3;
What is the value of ch after this code runs?
String str = “Arjun”;
int pos = 2;
char ch = str.charAt(pos);
pos = 3;
“Arjun”
str:
String
What is the value of ch after this code runs?
String str = “Arjun”;
int pos = 2;
char ch = str.charAt(pos);
pos = 3;
“Arjun”
str:
String
2
pos:
int
What is the value of ch after this code runs?
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
What is the value of ch after this code runs?
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!
What is the value of ch after this code runs?
String str = “Arjun”;
int pos = 2;
char ch = str.charAt(pos);
pos = 3;
“Arjun”
str:
String
3
pos:
int
‘j’
ch:
char