Java Casting
int count = (int) 40.0; stores the whole number 40, and System.out.println(count) writes 40. The screen is one line. The point is not on that line, and the 0 after the point is not on that line.
The kind word does not match the digits
Open Mint.java. The file name stays Mint.java, because public class Mint is still the name of the file. This page does not rename the file, and it does not change public class Mint. Delete boolean ripe if that line is still there, including true and false. Delete String plant if it is still there. Delete plant.length, delete charAt, and delete every line that asks a question after a dot. Delete any import line, any reader line, and any nextLine line. Delete int count = 40; if that line has no point and no parentheses. The file you keep has no import, no Scanner, no nextLine, no plant, no ripe, and no word true. An unused import does not by itself make javac fail, and it does not write 40 either. The number is written only by the writing line that uses the name.
The numbers page already showed the mismatch. int count = 40.0; asks an int name to hold a number that is written with a point. javac writes an error. It does not stay quiet. It does not drop the point for you. There is no screen to read while that error is being written. This page does not leave the line like that. The kind word int does not match 40.0. You write that same kind word again, inside parentheses, between the = sign and the digits.
Inside main, the storing line is int count = (int) 40.0;. int, the first one, is the kind word for the name. It says count holds a whole number. count is the name, and it starts with a small c. The = sign stores. Then (int) is the kind word again, alone inside one pair of parentheses. There is no digit inside those parentheses. The value sits outside, after the closing parenthesis: 40.0. The 4 and the 0 before the point are the whole number. The point says there is a further digit. That further digit is 0. The semicolon ends the line. This line stores 40. It does not write the screen. Storing is not writing. The screen stays blank through this line. The parentheses are not a second number. They are not quotes. They are not a question after a name. There is no plant on this page, so the point in 40.0 is not a dot that asks length.
The writing line is the bare name: System.out.println(count);. No quotes, and no parentheses around count. Java reads the name count, writes the whole number that name holds, and ends the line. For the file you keep, that number is 40. The letters of the name are not on the screen. int is not on the screen. The parentheses are not on the screen. The point is not on the screen. The 0 that stood after the point is not on the screen. The screen is 40, one line. Save the file.
The file you keep
Save this as Mint.java. It is the file this page keeps. One name, count. One kind word for that name, int. One pair of parentheses, and inside them only int. One value, 40.0, outside the parentheses. One writing line, and that line uses the bare name.
public class Mint {
public static void main(String[] args) {
int count = (int) 40.0;
System.out.println(count);
}
}
Read it from the top. public class Mint names the file. It does not write the screen. public static void main(String[] args) is where Java starts. The word String on that main line is still not a storing line. There is no String plant line, and there is no boolean line. int count = (int) 40.0; stores 40. It writes nothing. System.out.println(count); writes 40 and ends the line. One line on the screen. No point. No second line.
What the screen shows
In the folder that contains this Mint.java, type javac Mint.java. When the file is fine, that command writes no line. The parentheses and the point do not make javac write a line, and they do not make javac fail. Then type java Mint. Do not type .java after java. The same two commands are what you type on Windows and on macOS. This page does not quote the shell prompt. The program writes 40. It does not wait for you to type. The digits were already in the file. If java Mint waits, nextLine is still in the file. Delete that line and keep int count = (int) 40.0; and the one writing line. The program finishes after the writing line.
The screen shows 40. One line. No point. If the screen shows 40.0, the writing line is printing a number that still has a point, or the parentheses are not on the storing line you ran. The file you keep prints count, and count holds a whole number. If the screen shows 40.9, you are printing the dotted number, not the whole number after the parentheses. If the screen shows 41, the file holds 41. These parentheses do not round 40.9 up to 41. If the screen shows true or false, the boolean file is still in place. Delete ripe. If the screen shows leaf, plant is still in the file. If the screen shows mint, you are still on an older file. If the screen shows 32, the line int count = 040; is still in place. If the screen shows count, the writing line has quotes around the name. You do not reinstall Java for a screen that shows the wrong line.
A screen that shows 40 does not, by itself, prove the parentheses. int count = 40; with no point and no parentheses also writes 40. The screen can look the same. The file does not. The file you keep has (int) and it has 40.0. When you judge this page, look for the parentheses and look for the point in the file. Do not judge the parentheses from the screen alone.
Without the parentheses, the mismatch is still an error. int count = 40.0; makes javac write an error. It does not write 40. It does not stay quiet. You do not type java Mint while javac is writing an error. You do not need the exact words of that error. Put (int) between = and 40.0 so the line is int count = (int) 40.0; again. The value does not go inside the parentheses. int count = (int 40.0); makes javac write an error. The kind word sits alone inside. The digits sit outside, after the closing parenthesis.
Quotes and the word true do not fit inside this pair of parentheses. int count = (int) "40"; asks the parentheses to turn letters into a whole number. javac writes an error. int count = (int) true; asks them to turn the word true into a whole number. javac writes an error. It does not write 1. boolean ripe = (boolean) 1; asks the other kind to turn a digit into true. javac writes an error. It does not write true. There is no screen to read for any of these, because you do not run java Mint while javac is writing an error. Delete the quotes, delete true, and delete the boolean line when you are repairing the file you keep.
The name has to exist before a line uses it. If System.out.println(count); is written above int count = (int) 40.0;, javac writes an error. Move the storing line above the writing line. Do not write int a second time for the same name. A second int count under a line that already created count is a different error. The second file on this page replaces the storing line. It does not add another int count under the first. int count = (int); with nothing after the parentheses makes javac write an error. The value has to sit there. A space inside the parentheses, int count = ( int ) 40.0;, still stores 40, and javac writes no line. The file you keep has no space inside the parentheses. No space after the closing parenthesis, int count = (int)40.0;, also stores 40. The file you keep has one space there. Neither extra space nor a missing space is a second number.
Digits after the point are dropped
40.9 is not stored as 41, and it is not stored as 40.9, when the parentheses hold int. int count = (int) 40.9; stores 40. The 9 is dropped. It is not rounded up. It is not a second number on the line. It is not a second line on the screen. The writing line is still System.out.println(count);. The screen shows 40. One line. No point. The name count is not on the screen.
This is the second file. It is not the file you keep. The file you keep uses 40.0, and the digit after the point is 0. Save this second file as Mint.java to see that a 9 is dropped, then put int count = (int) 40.0; back.
public class Mint {
public static void main(String[] args) {
int count = (int) 40.9;
System.out.println(count);
}
}
Type javac Mint.java. The file is fine, so javac writes no line. The 9 does not make javac fail. Then type java Mint. The screen shows 40. One line. If the screen shows 41, this file is not the file you ran. If the screen shows 40.9, the parentheses are missing, or the writing line is printing the dotted number itself. If the screen shows 40.0, you are still on the file you keep, which is the right file to put back, but it is not this check. After you have seen 40 from 40.9, put the kept file back: int count = (int) 40.0; and one System.out.println(count);.
The parentheses can sit in front of a name
A name can hold the dotted number first. double is the kind word that matches a point. double weight = 40.9; stores 40.9 in weight. That line has no parentheses, because the kind word double already matches a value written with a point. The next line is the one that does not match: int count = (int) weight;. The parentheses hold int, the kind count can hold. They sit in front of the name weight, not in front of fresh digits. weight already holds 40.9. The parentheses drop the 9, and count holds 40. The writing line is still System.out.println(count);. The screen shows 40. weight is not on the screen. double is not on the screen. 40.9 is not on the screen.
This is the third file. It is not the file you keep. The file you keep has no double and no weight. It writes the digits 40.0 on the storing line. The word inside the parentheses has to be a kind the name can hold. int count = (double) 40.0; puts double inside the parentheses and int on the name. Those two kind words do not match. javac writes an error. It does not write 40.0. It does not stay quiet.
public class Mint {
public static void main(String[] args) {
double weight = 40.9;
int count = (int) weight;
System.out.println(count);
}
}
Type javac Mint.java. The file is fine, so javac writes no line. double weight = 40.9; does not need parentheses, and it does not make javac fail. Then type java Mint. The screen shows 40. One line. If the screen shows 40.9, the writing line is System.out.println(weight);. Change it back to System.out.println(count);. If the screen shows weight, the writing line has quotes around that name. After you have seen 40, put the kept file back. Do not leave double in the file you keep.
You can now store a whole number from a value that was written with a point. int count = (int) 40.0; stores 40. System.out.println(count); writes 40 and ends the line. The point is not on the screen. int count = (int) 40.9; also writes 40. The 9 is dropped, not rounded. The same parentheses can sit in front of a name that already holds 40.9, and the screen is still 40. Without the parentheses, int count = 40.0; makes javac write an error. (int) true makes javac write an error. The next page leaves these parentheses. It looks at a name that holds no number you wrote and no word you wrote.
The screen is the whole number that fits
The file you keep writes 40. The name on the writing line is count, and count is not on the screen. The parentheses are in the file, and they are not on the screen. One println is one line. A 9 after the point is dropped. A name that holds 40.9 can stand inside the same parentheses, and the screen is still the whole number.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top