1/67
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is the difference between an int and a double?
int stores whole numbers: -3, 0, 42
double stores decimal/floating-point numbers: 3.14, -0.5, 42.0
What happens when you divide two ints in Java?
You get integer division: the decimal portion is discarded.
What happens when at least one operand in a division is a double?
double x = 7 / 2.0;
x is 3.5.
What is the result of 5 / 2?
2, because both operands are ints
What is the result of 5.0 / 2?
2.5
What does casting do?
Casting explicitly converts a value from one data type to another.
Ex:
double x = 7.8;
int y = (int)x;
y becomes 7.
Does casting a double to an int round the number?
No. Casting truncates the decimal portion.
(int) 3.99 // 3
(int) -3.99 // -3
Think cut off the decimal, not round.
How can you force integer values to produce decimal division?
int a = 5;
int b = 2;
double result = (double)a / b;
Result: 2.5. If you cast after the devision it would be too late.
Question: What is the difference between these?
(double)(5 / 2)
(double)5 / 2
First performs integer division then casts 2.0. The second converts 5 to a double then performs decimal devision, getting 2.5
What is a variable?
A named location that stores a value
What is the difference between a char and a String?
A char represents one character: char letter = ‘A’;
A String represents a sequence of characters: String word=”Apples”;
What is a parameter?
A variable listed in a method's header that receives a value when the method is called.
public static int square(int x)
x is a parameter here
What is an argument?
The actual value you pass into a method when calling it.
square(5);
5 is the argument, x is the parameter
What is a method's return type?
public static double half(int x)
The return type is double.
What does void mean as a return type?
The method does not return a value.
public static void printHello()
If a method has return type int, what must it ultimately do?
It must return an int.
public static int add(int a, int b) {
return a + b;
}
What is the difference between return and System.out.println()?
return sends a value back to the code that called the method.
return x * 2;
System.out.println() displays something on the screen.
System.out.println(x * 2);
What does Math.abs() do?
Returns the absolute value.
What does Math.pow() do?
Math.pow(2, 3)
equals 8
What does Math.sqrt() do?
Math.sqrt(25)
Equals 5
What does Math.max(a, b) do?
Returns the larger of the two values.
Math.max(7, 12)
Equals 12
What does Math.min(a, b) do?
Returns the smaller of the two values.
Math.min(7, 12)
Equals 7
What does Math.random() return?
A random double greater than or equal to 0.0 and less than 1.0.
Possible values include 0,0, 0,372, 0,9999, but never 1.0
How can you generate a random integer from 0 through 9?
(int)(Math.random() * 10)
Results are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
How can you generate a random integer from 1 through 10?
(int)(Math.random() * 10) + 1
What does String.length() return?
String word = "Hello";
word.length();
Equals 5
What index does the first character of a String have?
Index 0
String word = "Hello";
Character H e l l o
Index 0 1 2 3 4
What is the index of the last character of a String?
length() - 1
For Hello,
5 - 1 = 4
What does charAt() do?
Returns the character at a particular index.
String word = "Hello";
word.charAt(1);
Equals ‘e’
What happens if you use an invalid String index?
Java throws a StringIndexOutOfBoundsException.
word.charAt(5) is invalid because valid indexes are 0-4
What does substring() do?
Returns part of a String.
String word = "Hello";
word.substring(1, 4);
Equals “ell”
Is the ending index of substring(start, end) included?
No.
"Hello".substring(1, 4)
takes indexes
1, 2, 3
but not 4
What does substring(start) do?
Returns everything from start through the end of the String.
"Hello".substring(2)
→ "llo"
What does indexOf() return?
The index where a character or substring first occurs.
"Hello".indexOf("l")
→ 2
What does indexOf() return if it can't find what you're looking for?
-1
Example:
"Hello".indexOf("z")
→ -1
What is the difference between indexOf() and lastIndexOf()?
indexOf() finds the first occurrence.
lastIndexOf() finds the last occurrence.
"banana".indexOf("a") // 1
"banana".lastIndexOf("a") // 5How do you compare two Strings for equality in Java?
Use .equals().
name.equals("Bob")
Do not normally use:
name == "Bob"
for String-content comparison.
What does "Hello".equals("Hello") return?
true
What does "Hello".equals("hello") return?
false
String comparison with .equals() is case-sensitive.
How do you compare Strings without caring about capitalization?
Use:
.equalsIgnoreCase()
Example:
"Hello".equalsIgnoreCase("hello")
→ true
What does toUpperCase() do?
Returns a new String with letters converted to uppercase.
"hello".toUpperCase()
→ "HELLO"
What does toLowerCase() do?
"HELLO".toLowerCase()
→ "hello"
Are Java Strings mutable?
No. Strings are immutable.
Methods such as:
toUpperCase()
substring()
don't modify the original String. They return a new String.
What happens here?
String word = "hello";
word.toUpperCase();
System.out.println(word);
hello
The original String wasn't changed.
To save the result:
word = word.toUpperCase();What does the + operator do with Strings?
"Hello " + "world"
→ "Hello world"
What happens when a String is added to a number?
Java converts the number to a String and concatenates.
"Age: " + 17
→ "Age: 17"
What is the result of this?
System.out.println(2 + 3 + "hello");
5hello
Java evaluates from left to right:
2 + 3 → 5
5 + "hello" → "5hello"
What is the result of this?
System.out.println("hello" + 2 + 3);
hello23
Once Java encounters the String, the remaining + operations are concatenation.
What does the % operator do?
It gives the remainder after division.
7 % 3
→ 1
What is 10 % 2?
0
Because 10 divides evenly by 2.
How can % be used to determine whether a number is even?
number % 2 == 0
If true, the number is even.
What does 17 % 10 equal?
7
The remainder after dividing 17 by 10 is 7.
This is useful for extracting the last digit of a number.
What is the result of -7 / 2 using integer division?
-3
Java truncates the fractional part toward zero.
What is the result of (int) -3.9?
-3
Casting truncates toward zero; it does not round down to -4.
What is the difference between widening and narrowing conversions?
Widening: converting to a type that can represent a broader range, such as:
int → double
This generally happens automatically.
Narrowing: converting to a type with less range/precision, such as:
double → int
This generally requires an explicit cast.
What happens here?
int x = 5;
double y = x;This is valid.
x is automatically converted to:
5.0
because an int can be safely represented as a double.
What happens here?
double x = 5.7;
int y = x;This produces a compile-time error.
You need an explicit cast:
int y = (int)x;
Then y becomes 5.
What is the output?
int x = 9;
int y = 4;
double result = x / y;2.0
Why? x / y happens first using integer division → 2. Then 2 is converted to double → 2.0.
What is the output?
int x = 9;
int y = 4;
double result = (double)x / y;
2.25
Casting x to a double forces floating-point division.
What is the output?
System.out.println(10 / 4);
System.out.println(10 / 4.0);
2
2.5
Same numbers, different data types.
What is the output?
String s = "Computer";
System.out.println(s.substring(2, 6));
mput
Indexes 2, 3, 4, 5 are included; index 6 is excluded.
What is the output?
String s = "banana";
System.out.println(s.indexOf("na"));2
The first "na" starts at index 2.
What is the output?
String s = "banana";
System.out.println(s.indexOf("z"));
-1
The substring wasn't found.
What is the output?
String s = "Java";
System.out.println(s.charAt(s.length() - 1));a
length() is 4, so the last index is 4 - 1 = 3.
What is the output?
String s = "Java";
System.out.println(s.substring(1));ava
Starting at index 1 means everything from the second character to the end.
What is the output?
System.out.println((int)(Math.random() * 6));One of:
0, 1, 2, 3, 4, 5
Never 6.
What is the biggest conceptual thing to remember about this unit?
Always track the data type and the order of operations.
Especially watch for:
int / int
vs.
double / int
and:
(double)(a / b)
vs.
(double)a / bRemember:
String indexes start at 0.
substring's ending index is exclusive.
indexOf returns -1 when nothing is found.
Use .equals() for String-content comparison.
Strings are immutable.
Casting double → int truncates; it does not round.
Math.pow() and Math.sqrt() return double.
Math.random() is from 0.0 up to but not including 1.0.