Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
Now let's break down all of the different pieces you're gonna need
to write a Java program.
Here is our overall HelloWorld program. You should have seen this before in the previous section, all this does is print the words HelloWorld out to the console for your user to see them. But in here we have three very distinct pieces. The first piece is called the class. This is the overall structure of any Java file. It should be one of the very first lines in your file and it always starts the same way. Public, class and then the exact name of your file. That's why this is called capital H Hello, no space, capital W, World. Just like the file name. Then there's the main method. This is a line of code you're probably just gonna have to memorize, at least for now. And it always goes public static void main(String[] args) and then a curly brace. I know that's a big mouthful. But I promise, if you're using an IDE like Eclipse, it'll probably add that in for you. Now that we've got these two lines of code, there's a few characters you're gonna wanna pay close attention to. You'll notice that both the class and the main method after the line of code has this thing we call a curly brace. That curly brace opens up that section of code and then you put thing inside of it and then when you're done you close it. So if you notice the very bottom of the file there is a corresponding closing curly brace for each of the opening ones. To make it really easy to see we've indented the main method, one level of indentation in from when the class starts. This is to show you visually that the main method is inside the class and this is gonna become incredibly important, so you wanna make sure that you're following this convention early on. I promise it will save you a lot of heartache. The rule of thumb is every time you add an open curly brace you're gonna wanna increase your level of indentation by one level. Inside the main method is the println, this is the way that you print out to the console. And in fact, you can put a bunch of statements here in the main method. And there's obviously lots of different types of statements, not just printlns. The main method is where you gonna wanna put all of the code you actually want the computer to run. In fact, the main method is where the computer knows where look to find code that you wanna run. At the end of the statements inside of the main method, you can see that there is a semicolon at the end of the line. Every single line in Java needs to either end with a curly brace or a semicolon. Now chances are, if you haven't used a compile language before, you're very unfamiliar with compiler errors. So we're just gonna talk about a couple of them now. For example, it's very common to forget that semicolon at the end of your statement. But if you have a compiler, it's gonna give you a little bit of feedback of what it thinks the problem is. Now unfortunately the compiler isn't as smart as we are so sometimes it doesn't have the most helpful responses, but as you can see in this one, this actually makes a lot of sense. It's gonna tell you which line it thinks the error is on and then it has this little caret character to tell you, hey, I think you're missing a semicolon right here. Thanks Java. Another incredibly common error is to forget one of those curly braces at the end of your line. For example, let's get rid of the one after the main method header. Now this compiler error is a little less helpful, because the Java program can tell like, hey, something's missing, I wasn't quite ready for the next line. But if you see an error that says something like this, you should go check all of your curly braces, and all of your semicolons because it essentially means the computer wasn't ready for the line of code it got because you didn't finish the line before it. Sometimes, you can miss a curly brace, not at the beginning of an opening, but at the closing. So for example, if we forget that last curly brace that closes the class we might get an error that looks something like this where it tells us that it's reached the end of the file. That means that you didn't put the curly brace in so the computer is like hey, I expect more Java where is it? That's why you always gonna make sure that curly braces is there so you know hey, computer I'm done with my code. As you work with Java you'll learn to read these compiler errors like a second language and they're always gonna try to be really helpful. One thing that's always good to do is to turn on the line numbers. Because the compiler error is gonna tell you which line it thinks the problem happened at. The other thing that's really good, is you should always try to write small bits of code and then compile them. If you write a lot of code sometimes you get like a lot of compiler errors and it's very hard to find what went wrong. If this does happen however, always try to resolve the compiler error at the very top of the list first, because sometimes the same mistake generates multiple errors. So if you fix the thing at the top sometimes what looks like a very broken piece of code works pretty well. If you really are stuck and you don't know what your compiler error means, you can always copy it into a search engine. Because chances are there's somebody out there on the Internet that had the exact same error. And there's a lot of really helpful resources out there for you. So now that you know all of the different pieces of a Java program I encourage you to spend sometime, write your own and play around with all the different ways you can break it. That way when you see these errors later, they'll feel like helpful hints, not scary mistakes.
(Ebook) Communicating Linguistics: Language, Community and Public Engagement by Hazel Price, Dan McIntyre ISBN 9780367560119, 0367560119 - Download the ebook with all fully detailed chapters