Java String Methods
plant.length() counts the letters plant already holds. String plant = "leaf"; still stores leaf, and System.out.println(plant.length()) writes 4. The letters stay leaf. They do not turn into the number. The dot after the name is the question, and the writing line shows the answer.
The dot asks how many letters
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. The last page kept String plant = "leaf"; and one writing line that showed leaf. This page keeps that storing line. Delete an int line if one is still there from the page before the quotes. Delete any import line, any reader line, and any nextLine line. The file you keep has no import, no Scanner, and no nextLine. An unused import does not by itself make javac fail, and it does not write the count either. The count is written only by the writing line that asks the question.
Inside main, the storing line is still String plant = "leaf";. String is the kind word. plant is the name, and it starts with a small p. The quotes hold l, e, a, f, all small, with nothing else between them. That line stores the letters. It does not ask a question, and it does not write the screen. The question is a second line of work, after the letters are stored. After the name, write a dot, then the question word length, then a pair of parentheses with nothing between them. The whole question is plant.length(). The dot ties the question to the name. length is the question word for how many. The empty parentheses mark that this question needs no extra word inside them. There is no place number here. In the file you keep, that question sits inside the second writing line. It is not a line by itself. Asking is not the same as storing. The letters in the name stay leaf. The question does not take the quotes away, and it does not put the number 4 into the name plant.
The file you keep writes the letters first, then asks the question. The first writing line is the bare name: System.out.println(plant);. No quotes, and no dot. It writes leaf and ends the line, the same way the last page did. The second writing line is the question: System.out.println(plant.length());. No quotes around length. Java reads the name plant, counts the letters that name is holding, writes that count, and ends the line. For the file you keep, the count is 4. The quote marks are not on the screen. The word length is not on the screen. The parentheses are not on the screen. The letters of the name, p, l, a, n, t, are not on the screen. Save the file.
The file you keep
Save this as Mint.java. It is the file this page keeps. One storing line, String plant = "leaf";. One writing line for the letters. One writing line for the count. The count line is plant.length() inside println. Nothing else.
public class Mint {
public static void main(String[] args) {
String plant = "leaf";
System.out.println(plant);
System.out.println(plant.length());
}
}
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. The storing line is the next one, String plant = "leaf";. It stores four letters. It writes nothing. System.out.println(plant); writes leaf and ends the line. System.out.println(plant.length()); writes 4 and ends the line. Two lines on the screen. The first line is the letters. The second line is the count. The quotes stay in the file, around leaf, and they are on neither screen 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. A question after the dot does not by itself make javac write a line, and it does not make javac fail. Then type java Mint. Do not type .java after java. These are the same two commands you type on Windows and on macOS. This page does not quote the shell prompt. The program writes leaf, then 4 on the next line. It does not wait for you to type. The letters were already in the file, and the count is answered from those letters. If java Mint waits, nextLine is still in the file. Delete that line and keep the storing line and the two writing lines. The program finishes after the second writing line.
The screen shows leaf, then 4. Four small letters, then the count, with no quote marks on either line. If the screen shows plant, a writing line has quotes around the name: System.out.println("plant");. Those quotes mark the letters p, l, a, n, t, so the stored leaf is not written and the question is not asked. If the screen shows only leaf, the second writing line is missing. The kept file has both lines, letters first and count second. If the screen shows only 4, the first writing line is missing. You can still believe the letters are in the file, but this page's screen is the proof: leaf, then 4. If the screen shows 4 and then leaf, the two writing lines are swapped. Put println(plant) above println(plant.length()). If the screen shows 5, the count is wrong for these letters. The quotes are not letters the name holds, so they are not part of the 4. A fifth letter would have to sit between the quotes. The kept file has l, e, a, f only. If the screen shows mint, you are still on an older file. If the screen shows leaf and then leaf again, the second writing line is another println(plant), not the question. You do not reinstall Java for a screen that shows the wrong line.
A large letter between the quotes counts as one letter, the same as a small one. String plant = "Leaf"; would still make plant.length() write 4. The writing line for the letters would show Leaf, with a large L. The file you keep does not use that. It uses four small letters, and the count line writes 4. Judge the count by the letters that really sit between the quotes.
You can store the count under a second name. int count = plant.length(); takes the answer, and the kind word is int, because the answer is a whole number. System.out.println(count); then writes 4. That is allowed. It is not the file you keep. The kept file writes the question directly, so the screen is leaf and then 4, and you do not need a second name to see the count. The number 4 is the answer. It does not replace the letters inside plant.
A space counts as one
The letters between the quotes can change, and the count follows those letters. Under the first question, store a new set of letters in the same name, then ask the question again. The new storing line is plant = "leaf stem";. It does not start with String. plant already exists, so this line only replaces the letters the name holds. Between the quotes there are nine places: l, e, a, f, one space, s, t, e, m. The space is one of them. length counts it. The two words are still one stored value, not two names. This second file does not write the letters. It writes only the two counts, so the screen cannot be confused with the letters themselves.
This is the second file. It is not the file you keep. The file you keep has one storing line, the letters leaf, no space, and a screen of leaf then 4. Save this second file as Mint.java when you want to see the space counted, then put the kept file back.
public class Mint {
public static void main(String[] args) {
String plant = "leaf";
System.out.println(plant.length());
plant = "leaf stem";
System.out.println(plant.length());
}
}
Type javac Mint.java. The file is fine, so javac writes no line. The second storing line is allowed to omit String, and that omission does not make javac fail. Then type java Mint. The screen shows 4, and the next line is 9. Two lines. No quote marks. The letters leaf are not on the screen. The letters leaf stem are not on the screen either. Only the counts are there. The first count is from the letters the name held at that moment. The second count is from the letters the name holds after plant = "leaf stem";. If the second line is 8, the space is missing between the quotes, and the stored letters are leafstem. If both lines are 4, the second storing line is missing or still says "leaf". If both lines are 9, the first storing line already says "leaf stem". The screen follows the order of the lines in the file, and it follows the letters between the quotes, space included.
Do not write String a second time for the same name. String plant = "leaf stem";, placed under a line that already created plant, is a different error. That line tries to create plant again. javac writes an error and does not stay quiet. You do not need the exact words of that error. Do not type java Mint yet. Delete the second String and leave plant = "leaf stem";. The name is created once. Later lines only change the letters it holds, or ask a question about those letters. In the file you keep there is no second storing line. After you have seen 4 and then 9, put that file back: String plant = "leaf";, println(plant), and println(plant.length()).
Which letter sits at a place
length asks how many. A different question asks which letter sits at one place. The question word is charAt. The place goes inside the parentheses, because this question needs that extra word. The first place is 0, not 1. For leaf, place 0 holds l, place 1 holds e, place 2 holds a, and place 3 holds f. plant.charAt(0) answers with the single letter l. plant.charAt(1) answers with the single letter e. The answer is one letter, not the whole word leaf, and not the place number itself. The letters stored in plant stay leaf. Asking for one place does not remove the others.
This is the third file. It is not the file you keep. The file you keep asks length, and its screen is leaf then 4. Save this third file as Mint.java when you want to see the places, then put the kept file back.
public class Mint {
public static void main(String[] args) {
String plant = "leaf";
System.out.println(plant.charAt(0));
System.out.println(plant.charAt(1));
}
}
Type javac Mint.java. The file is fine, so javac writes no line. A place number inside the parentheses does not make javac fail when that place is one of the letters. Then type java Mint. The screen shows l, and the next line is e. Two lines. No quote marks. The word leaf is not on the screen. The numbers 0 and 1 are not on the screen. If the first line is e, the first question asked for place 1, which is the second letter. The first letter is place 0. If the first line is leaf, the writing line is println(plant), not charAt. If the first line is 0, the writing line wrote the place number and did not ask charAt. If both lines are l, both questions say charAt(0). Place 2 would write a. Place 3 would write f. Those two places are real for leaf. This third file does not ask them. It asks 0 and then 1, so you can see that counting starts at 0.
A place that is not there
Place 4 is not one of the letters in leaf. The count is 4, and the places that exist are 0, 1, 2, and 3. Change the first question to plant.charAt(4) and leave the rest of the third file. Type javac Mint.java. javac writes no line. The line is a legal question, so a missing letter is not a javac failure. Then type java Mint. The program writes an error. It does not write a fifth letter, and it does not write 4. You do not need the exact words of that error. The useful fact is that the failure happens when the program runs, not when javac reads the file. Put charAt(0) and charAt(1) back if you still want those two lines, then put the kept file back when you are done. Do not leave charAt(4) in the file you keep.
An empty pair has a count of 0. String plant = ""; stores no letters. System.out.println(plant.length()); writes 0. javac writes no line, because an empty pair is allowed. java Mint writes 0 and finishes. It does not wait. A blank line was what println(plant) wrote on the last page. The question is different: the screen shows the digit 0, which is the count, not a blank line. plant.charAt(0) on that empty pair is a place that is not there. javac still writes no line. java Mint writes an error, because place 0 needs a letter and the count is 0. After you have seen 0, make the storing line String plant = "leaf"; again.
The parentheses are part of the question. System.out.println(plant.length); has the dot and the word length, and it has no parentheses. javac writes an error. It does not stay quiet. There is no screen to read, because you do not type java Mint while javac writes an error. You do not need the exact words. Put the empty pair of parentheses back, so the line is System.out.println(plant.length());. charAt cannot be empty. System.out.println(plant.charAt()); writes an error at javac, because that question needs the place. Put a place that exists inside the parentheses, such as 0, when you are on the third file. The file you keep uses length(), with nothing between the parentheses.
The question has to follow the name. System.out.println(length()); treats length as a name this file does not have. javac writes an error. The question word belongs after plant and a dot. System.out.println(plantlength()); is also one name, plantlength, which this file does not have. The dot is what separates the name from the question. The name still has to exist before a line uses it. If System.out.println(plant.length()); is written above String plant = "leaf";, javac writes an error. The question names plant before plant exists. Move the storing line above the writing lines. This is the same order you already used for count and for reader.
The question is for letters, not for a whole number you stored as int. int count = 4; System.out.println(count.length()); asks a whole number how many letters it holds. javac writes an error. A whole number has no length question. The repair on this page is not to add parentheses only. The repair is to ask the question of the name that holds letters: plant.length(). The page before the quotes kept int and kept 4 with no question. The last page kept String and kept "leaf". This page keeps String, keeps "leaf", and asks length.
You can now ask how many letters a name already holds, and which letter sits at a place that starts at 0. String plant = "leaf"; still stores leaf. System.out.println(plant); writes leaf. System.out.println(plant.length()); writes 4, and the letters stay leaf. A later plant = "leaf stem"; makes the next count 9, because the space counts as one. plant.charAt(0) writes l, and plant.charAt(1) writes e. A place that is not there, such as 4 for leaf, lets javac stay quiet and makes java Mint write an error. length without parentheses makes javac write an error. The next page leaves these questions. It stays with a whole number like the 4 this count wrote, and the number is one you write in the file again.
The screen shows the answer, and the letters stay
The file you keep writes leaf and then 4. The name on the first writing line is plant, and plant is not on the screen. length is in the file, after the dot, and it is not on the screen. The quotes stay around leaf. They are not counted, and they are not on the screen. A space between the quotes is counted. The first place is 0.
🧠 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