What you'll learn
- Create and run a Java program in the Eclipse IDE using a class with a public static void main method
- Explain how classes, main methods, and packages organize a Java project and prevent naming collisions
- Apply Java syntax rules such as semicolon line endings and case sensitivity for System and String
- Declare variables in Java and use System.out.println to print results to the console
- Name Java packages using reverse-domain segments such as com.example.hello, from general to specific
- Compile and run Java files from a terminal and text editor without a Java-specific IDE
Free 48-minute video lesson with a full transcript and knowledge checks. No account needed to watch.
Overview
Join Shaun Wassell as he walks you through the fundamental concepts of the incredibly popular Java programming language and shows you how to write and run several Java programs both with and without an IDE.
Recommended Experience
- 1 to 2 years of software development experience
Related Job Functions
- Software Developer
- Software Engineer
- Software Development Engineer (SDE)
A former full-stack developer, Shaun Wassell brings a wealth of web development and programming experience to his training.
Introduction
Let's get started with a little background on what we'll be covering in this section.
Creating and Running a Java with Eclipse
And now, let's jump right in and see how to install the Eclipse IDE and use it to write and run a simple Java program.
Knowledge Check
In order to add new files to a Java project, what will we most often tell the IDE that we want?
Basic Structure of a Java Program
At first glance, Java projects may seem like a jumble of files and code, but they actually follow a pretty logical structure. In this video, we take a look at this structure and talk about the purpose of each part.
Knowledge Check
The entry point for a Java program is a method called which of the following?
Java Syntax Rules
Ignore Java syntax rules at your peril! Well actually, if you ignore them, you just won't be able to compile or run your programs. In this video, we learn about the most important Java syntax rules and see them in action.
Knowledge Check
What do single-line comments in Java begin with?
Declaring Variables in Java
Variables form the life-blood of most programs, and Java programs are no exception. In this video, we see how to declare basic variables in Java, and also discuss some of the quirks of Java's type system.
Knowledge Check
Which type of languages generally include the data type of a variable in the variable's declaration statement?
Writing and Running Java without an IDE
While most Java developers use an IDE for their day-to-day development needs, it's entirely possible to write and run Java without fancy IDEs like Eclipse. In this video, we see how it's possible to write and run Java code using only a text editor and the command line.
Knowledge Check
Which command compiles a .java file into a .class file?
Conclusion
I hope this has been informative for you and I would like to thank you for consuming.
View Transcript
Introduction
0:00[AUDIO LOGO] Hi, everyone. Shaun Wassell here. And welcome to this course where we're going to be learning all about the basics of Java development. In this section and the sections that follow, we're going to be covering a pretty wide variety of different Java-related topics, all the way from the basic syntax and structure of Java programs, through some of the more advanced topics, such as object-oriented programming, and the many built-in data structures that Java provides us with.
0:32So in this section, specifically, we're going to be getting started by seeing how to set up and run the Eclipse IDE, and use that to write Java programs. But we're also going to see how to run Java programs without using an IDE, just in case that's something you ever want to do. And additionally, we'll also be covering things such as how to declare variables and just the basic syntax of Java program. So without further ado, let's jump right into the world of Java.
Creating and Running a Java with Eclipse
0:00[AUDIO LOGO] All right, so to get started here, let's just jump right in and see how to create and run our first Java program. Now, the first thing that you might be wondering about, if you've done any kind of programming before, is, which IDE should we use for Java development? Well, the good news, and sort of the bad news here too, is that there are actually a lot of different options for Java development.
0:32And depending on what company you work with, depending on who you talk to, people tend to prefer different IDEs. But the most common one, and the one that we're going to be seeing how to use here, is an IDE called Eclipse. And there are others. There's another one called NetBeans. There's another one called IntelliJ IDEA. And then of course, there's always the option of using an IDE that isn't specifically made for Java but that includes different plug-ins you can use for Java development.
1:05And that would be something like Visual Studio Code, which is very commonly used for a pretty wide variety of programming languages. But as I said, we're going to start off by seeing how to use the Eclipse IDE to just write and run a simple Java program. So what you're going to want to do is go to the Eclipse Foundation's website. And currently, that's eclipse.org. I don't see that changing at any point in the near future. But of course, you never do see those changes coming. So anyway, just find the Eclipse Foundation's website. And once you're there, you're going to want to find the Download link.
1:40In my case, that was just a button up in the top corner. And you're going to want to find the link for downloading the Eclipse IDE for whatever operating system you're currently on. I'm on macOS. So I'm just going to click Download this x86 version here. And once you've downloaded that, you should see this "Thank you for your download" and just a thing asking you to donate to the Eclipse community. If you want to do that, go ahead. But I'm just going to skip over that for now. And what you're going to want to do is run the Eclipse Installer, which-- let me just drag that back over to here.
2:14What you just downloaded there is going to have this Eclipse Installer thing inside of it. You can just click on that. In my case, since I'm on Mac, it's going to ask me if I really want to open this, because it's something that I downloaded from the internet. And then you should see the Eclipse Installer run. And then the next thing that you should see is this menu asking which Eclipse IDE you want to install. There are actually several different options, depending on what programming language you're using. And you'll even see that there's things like Eclipse Modeling Tools and Eclipse IDE for Scientific Computing.
2:46There's lots of options here. But what we're interested in is the most common one, and that's going to be the Eclipse IDE for Java Developers. So just click that. And the next thing that it's going to ask you is where you want the Java Virtual Machine. That's something that we'll talk about in a little bit more detail later. But for now, just leave the default option. And it's also going to ask you where you want to install the Eclipse IDE. I'm just going to leave that as the default option as well. But if there's another place you want that, feel free to change it. So at this point, you should be able to click Install.
3:17And that will run for you and install the Eclipse IDE. Since I already have it installed on my computer, I'm just going to leave it and exit out of the installer. And I'm just going to get rid of the installer here as well. All right, so now that you have the eclipse IDE installed, you're going to want to open that up. And the first thing it's going to ask you here is where you want your workspace to be. Now, what this is asking you is basically just where you want all of your Eclipse projects to be saved. So I've created this folder called eclipse-projects inside my home folder.
3:49And that's where I'm going to do it. But you can really set whatever path you want for that. Cool. So once you know where you want to store your Eclipse projects, you're just going to want to click Launch. And that will open the Eclipse IDE for you. So once you have the IDE open, the next thing that we're going to want to do is create a new Java project. And this will allow us to write and run our Java code. All right, so you should be able to create a new Java project in several ways. One is over here in the top left-hand corner, you should see Create a Java project.
4:22Or what you can do is just go up to the Menu and do File, New, and Java Project. But however you get to it right, however you tell Eclipse that you want to create a new Java project, the next thing that it's going to ask you is what you want to name the project. All right, now, this can really be whatever you want. But I'm going to call this project HelloWorld because we are going to be doing the famous Hello World example here just because this is going to give us a great foundation to start talking about some of the more fundamental concepts of Java programming.
4:53And once you've done that, you should just be able to click Finish. And leave the defaults and everything else too. And over here on the left-hand side, you should see this HelloWorld little folder thing up here. And if you expand that-- and it is pretty teeny tiny here. Hopefully, you can see OK. You should see that it has this JRE System Library thing there. We're just going to ignore that for now. And under that, we have source. And this is where we're going to write our code. So at this point, we've created a Java project. But the next thing that we have to do is in order to write some Java code, we need to create our first Java class.
5:30All right, so this is an interesting place where Java can start to diverge from a lot of other languages, especially scripting languages like JavaScript and Python is that in Java, pretty much everything, all of the logic that you're going to write in a program is contained somehow inside a class. So as you'll see, even when we want to write a simple Hello World program, we're still required to write a class. And inside that class is where we'll specify the actual logic for the program.
6:01All right, so here's what this is going to look like. What you're going to want to do is right-click on this source folder here. And then we're going to go to New. And you're going to want to find Class in this dropdown menu. So let's just click on that. And then what it's going to ask you is what you want to name this class. Now, since we're just creating a simple Hello World example, we're going to call this HelloWorld in Pascal case, starting with a capital letter and with every new word in that name starting with a capital letter as well, which is almost always how Java classes are named.
6:35All right, and we're also going to want to specify a package for this class. And packages are something that we'll discuss a little bit later as well. They just help us keep all of our code organized and prevent things like naming collisions in our projects. So what we're going to do is just say com.example.hello. And that will be the package name that we'll use for this simple class. Again, we'll discuss this in more detail as we go along. So now that we have our name and package specified, there's one more thing that we're going to do here.
7:06And that is-- you see this little option that says public static void main, blah, blah, blah? Well, we're going to check that. And that's something that I'll discuss in more detail in just a minute here as well. But you're going to want to select that. And then we're going to click Finish. And this will create our new class for us and open that up in our editor. So in order to write a simple Hello World example in Java, what we're going to do-- and we will talk more about the rest of the code here shortly. But what we're going to do is delete any comments that are inside this public static void main method, which, again, we'll talk about in more detail shortly.
7:45And inside there, what we're going to do is we're going to say System.out.println. All lowercase there with the println. And then inside here, what we're going to do is we're going to say double quotes "Hello world." And then after that, we're going to put a semicolon because in Java, all lines have to end in a semicolon. This isn't like Python where a newline indicates the end of a statement. And it's definitely not like JavaScript, where you have the option of adding a semicolon or not.
8:17In Java, you don't have the choice. You have to add a semicolon. So what we just wrote here is really the simplest possible Java program. And you might notice that it's a little bit more complex than, let's say, the simplest program in Python or the simplest program in JavaScript. And that's because of all this extra stuff here-- the public static void main, public class HelloWorld, the package name, et cetera. But before we discuss that in more detail, let's see how to run the program. The good news here is that as long as you're using Eclipse, this is pretty easy.
8:50You can just go up here to this little green Play button with nothing else beside. There are other options here, as you can see. And you're just going to click on that. And you can see, if you hover over it, it says Run HelloWorld. So let's just click on that. And it might ask you to save a certain number of files. If there's anything that the code you're running relies on that hasn't been saved, that's going to show up here. And we're just going to click OK. And that will save the file. And down here at the bottom of our Eclipse window, you'll see that, sure enough, it prints out "Hello world" to the console.
9:24So that is really the simplest possible Java program. Congratulations on writing and running your first program in Eclipse. So I hope this has been informative for you, and I'd like to thank you for viewing.
Basic Structure of a Java Program
0:00All right. So now that we've seen how to set up and run a simple Java program, what I want to do next is go back and actually take a look at some of the finer details that we encountered in this process. So the first thing that I want to take a look at here is this system.out.print. line that actually printed out Hello World to the console.
0:30Now system.out.print.line in Java is the main way that we print things out to the console. And it's very frequently used for things like debugging. And even in production, it's still used to store just little pieces of information in the logs. So especially in these initial stages as we're learning Java, we'll be using this system.out.print.line function quite a bit in order to see the results of the programs that we write. All right. And even in more complex command line applications, right, if you want to write some kind of, let's say file manager that the user can use in a terminal, then most of the time you 'll still stick to using system.out.print.line.
1:12All right. So I want to point out here too that you can easily change this string that's inside this function. So right now we've specified the string. And by the way, in Java, strings are just data that represents some sort of text, right? In our case, we have the sentence Hello World. And another thing to notice here is that we've specified this string with double quotes. Now, unlike in other languages, such as JavaScript and Python, where you have the option to use either double quotes or single quotes, in Java, strings can only be specified with double quotes, right?
1:47You can see this if you try and switch the double quotes to single quotes, you 'll get some kind of error saying invalid character constant. And the reason you see that error is because in Java, single quotes are only used to specify characters, which are basically strings that contain only a single letter if you want to think about them that way. All right. So let's change those back to double quotes. And let's change the string that we're printing out here. All right. So we'll change this string to something like Java is awesome. Okay.
2:17Well, if we save that and then go and click this little run button again, sure enough, we'll see that instead of Hello World, Java is awesome is printed out to the console. Cool. So hopefully you understand at least this line now. So let's move on out to this public static void main line. And while we're at it, let's also talk about public class Hello World. As I said before, in Java programs, pretty much everything is grouped into these things called classes that has some sort of name that helps us identify what the class is meant to do.
2:50Now at some point, we'll go through a proper examination of object oriented programming in Java and, you know, how it works, how it can help us, et cetera. But for now, all you really need to know about classes is that they represent sort of categories of objects that all behave in a similar way, right? So you could have something like a car class. You could have something like a database class. And the idea is that from these classes, you can create what are called instances, which are specific objects that behave in a certain way.
3:23All right, don't worry too much about that. If that all sounds new to you, and if you're already familiar with object oriented programming, well, that's great as well. All right. So what we've done here is we've created a Hello World class. And by itself, this class doesn't really have any behavior, right? But what it does have is this public static void main method, and by the way, functions that we define inside classes are called methods. And what this public static void main method does is it tells our Java project where to start execution, right?
3:58So in other words, in a Java program, we could have lots and lots of different classes. And so when we run our program, Eclipse has to know where to start running that program from. As it happens, this public static void main method is what it goes around and looks for. All right. Now it is possible to have multiple main methods in a project. And that would be the case if your project needs multiple entry points, such as a client side program and a server side program, or, you know, two separate programs that need to talk to each other, but operate independently.
4:35That's something that we'll take a look at much later. So don't worry too much about that right now. All right. So that's just a brief explanation of classes and the main method in Java programs. The last thing that I want to discuss here is this package, calm.example.hello thing up at the top. Now as I mentioned very briefly, a few minutes ago, packages in Java help us organize our projects more effectively, right? Now you can probably imagine that if we had only 10 or even 20 classes in our project, that the need for organizing all of those classes into different packages wouldn't be as great, right?
5:12We could probably keep track of that project without separating them into packages. But as our Java programs get bigger and bigger and bigger, and it's not uncommon to have Java projects that have thousands or probably even tens of thousands or hundreds of thousands of different classes in extreme cases. But in those situations, you definitely want to have some sort of organization to all of your code. And that's what packages in Java allow us to do. You can think of packages in Java as sort of like directories and sub directories in our project, but that have an actual meaning and that influence how the program operate, right?
5:50So just to give you a little demonstration of that here, if we were to try and create a new class inside this calm.example.hello package that we just created, right? Let's try and create another class called hello world. Well, we're going to see that we get this error here saying type already exists , right? And that's because we already have a hello world class inside this package. However, what you'll notice is that if you change the package here to something like calm.example.greet, right, we won't get that same error, right?
6:21It'll allow us to create this new class. And if you click finish, then you'll see that we have this other hello world class in a different package, right? So in other words, Java forces us to keep our class names unique, but only inside a specific package, right? So what this means is that it's perfectly fine to have multiple classes with the same name, but they have to be separated into different packages. And the idea there is that the package gives a little bit of context to what that class is for.
6:52All right, so that's the basic idea of packages in Java. Hopefully this brief discussion of classes and main methods and packages has helped you to understand a little better how Java projects are organized. And obviously we're going to be seeing this in much more detail as we go on. So I hope this has been informative for you, and I'd like to thank you for viewing. [BLANK_AUDIO]
Java Syntax Rules
0:00All right, so now that we've discussed a little bit of the structure of Java programs, the next thing that I want to do is talk a little bit more about the basic Java syntax. And we're also going to see how to declare variables in Java, which obviously in any programming language is a pretty important thing to know how to do. So first things first, in no particular order, I'm going to cover a few of the most important syntax rules in Java. All right, and it's important to know that if you break one of these syntax rules, right? So for example, if you leave a semicolon off the end of a line, then the program won't actually run, right?
0:47And this is a little bit different from some of the other programming languages out there, such as JavaScript, which will happily execute your code up until it finds something wrong with your syntax, at which point it will give you an error. So you can see this , by the way, if you just remove the semicolon, save the file and try and run the code, what you'll get is instead of any kind of output down here, it'll just say errors exist blah, blah , blah. And you can tell it to proceed, but you're going to get an error, right? It'll say error in thread main.
1:19And it'll tell you that there's a syntax error of some sort, right? So if you see this syntax error string, well, as you can imagine, that means you have a syntax error and depending on your ID, it may even tell you where that syntax error is, right? In our case, it tells it that it's in Hello World main and line six. So it leads us right to it. And sure enough, if we add the semicolon back and try and run our program again, we'll see that our program runs without a hitch. Cool. So now that we're clear on how important it is to have correct syntax in your Java programs, let's discuss some of the most important syntax rules in Java. The first one is that Java is case sensitive, right? So what that means is if you were to try and say system dot out dot print ln, and perhaps you even made this mistake earlier, if you were following along, right, if you write system with a lowercase s instead of the correct uppercase s, that's going to be an error, right?
2:17It's going to tell you it doesn't know what system is and we can see this if we save it and try and run it and tell it to proceed. Sure enough, we'll see that Java's telling us that it has no idea what system with a lowercase s is. But if we change that back to an uppercase s , we'll see that that works without a problem. Okay, so that's just something that's important to remember. And it can get a little bit tricky, especially. And this is problem that still occasionally trips me up sometimes is this string type. Right, so we'll talk about the string type in much more detail when we talk about Java's data types. But in Java, the string type has a capital s and what most people will do is write that with a lowercase s, which is not actually a thing.
3:01Okay, cool. So Java's case sensitive. And we've already seen that Java requires us to end all statements with a semicolon. And besides that we've also talked about the fact that Java programs are made up of a series of classes. And these classes, as I mentioned earlier, are actually required to start with a capital letter. Right, and this usually means that they're in Pascal case, which is just like camel case, except it starts with a capital letter. And as we've also mentioned , these classes have a series of methods such as this main method here. And in Java, the names of these methods are required to be lowercase, right? So in other words, they're required to start anyway, with a lower case character. So you couldn't have, for example, a method called main with a capital M. All right, and Java just does that to help differentiate between things that are classes and things that are methods and variables, etc. All right, now besides those things, really, the only other piece of syntax that I wanted to discuss before we talk about how to declare variables in Java is how to do comments in Java programs. All right, now depending on which programming languages you already know, this may or may not look very familiar, in particular, if you're familiar with
4:18JavaScript, this is going to look very familiar. In Java, we can do comments, which are basically just text in our code files that the compiler will ignore. And you can write a comment by putting two forward slashes. And then after that, you can write whatever you want, right? So we can say something like, hello, everyone. And the compiler will just ignore that, right? In general, nothing that we put after a comment will directly affect the execution of the program. Now, depending on your ID settings, and whether or not you're automatically generating documentation from comments, that's another story. But in general, comments allow you to just tell other programmers or yourself later on, what a given line was for, right? So we could say something like, this is how you print to the console in Java. Okay, now the two forward slashes will create a single line comment. But in many cases, we want to create a multi line comment. And in Java, the way that we do this is by using a forward slash and an asterisk. And this is closed off by an asterisk and a forward slash. All right, now my ID is automatically adding these asterisks in between as well, which I
5:34think looks nice, but that's actually not necessary, right? You can just say, hello, this is a multi line comment. And all of that will be ignored by the Java compiler. All right, so anyway, that's how to do comments in Java. So the next thing that we're going to take a look at is how to start declaring variables in our Java program. So I hope this has been informative for you. And I'd like to thank you for viewing. [BLANK_AUDIO]
Declaring Variables in Java
0:00All right, so now that we've discussed a few of the basic syntax rules in Java, the next thing that I want to talk about is how to declare variables. Now declaring variables is obviously a very important part of any programming language, and the good news is that Java does it in a pretty similar way to most programming languages that you'll find. So in order to demonstrate this, what we're going to do is we're going to create a new class in our project here.
0:37And instead of the hello world example, what we're going to do is again, we'll right click on the source folder and say that we want to create a new class. And then what we'll do is we'll call this class something like calculator. All right, you'll see why we call it that in just a minute here. And we're going to put it in a different package from our hello world class. So what we'll do is we'll do calm.example.calculator. And by the way, I don't think I've mentioned yet why exactly package names are the way they are, right?
1:08In other words, you might be wondering why they start with calm and have all these different segments in them. Well, basically the convention with Java package names since Java packages can be shared with other developers and other organizations is to name your packages the reverse of your domain name. So in other words, let's say that you were working for XYZ corporation and your corporations domain name was XYZ Corp.com. Your package names would generally be calm.xyz Corp.blah blah blah, right?
1:42And you could have as many segments as you want after this as you get more and more specific, right? So if you wanted to do calm.xyz Corp.math.calculator, right? That would sort of provide a path down from more general things, such as your entire company and then down through specific sections and specific features of your app. And another way that you could picture this is as just a series of nested directories, right? So, you know, in computer directories, you generally go from more general directories, such as your user directory to more specific directories, such as documents or downloads.
2:24And then those could even have more specific directories inside of them. And that's pretty much what Java package names do as well. All right. So now that we've touched on why package names are the way they are, let's create this new calculator class here. And what we're going to do is inside here, we'll give this calculator class that was just created the same main method as we had in our Hello world class. And you can even just copy and paste this if you want. Although personally, I've found that actually typing things out manually can really help you remember what things are and speak about them a little bit more intellig ently.
3:00So what we'll do here is we'll just type this out. We'll say public static void main. And then the argument that this main method takes is a string array called ARGS . All right. Now that's something that we'll discuss in a bit more detail shortly. Basically, what this argument does is it gives us access to any other command line arguments that the user has entered when running our program, right? So in other words, you're allowed to run JavaScript programs with arguments just like you're allowed to say call functions or methods with arguments.
3:34Again, that's something we'll take a look at in more detail shortly. But anyway, what we're going to do is inside this calculator function, for now we'll start off by defining a few variables and adding them together. And really the point of all of this is to show us how to declare variables in Java. All right. So let's start off here by defining two numbers called X and Y. And the way that we do that in Java is by saying int, right? So I and T X equals, and then we'll just give X a value.
4:06We'll say 10. And of course, we'll close that off with a semicolon because all statements in Java have to end with semicolons. And then we'll do the same thing for Y. So we'll say int Y equals, and let's just make this something like 30. Cool. So what you see here is two examples of how to declare variables in Java. It's really not that much different from a lot of other programming languages, but the main difference to note, which is again, something that we'll discuss in much more detail later on, is that in Java, when you declare variables, the variable declaration includes the type of the variable.
4:42So in other words, since we're defining two integer numbers here that we'll want to add together, the type of these variables is int, right? Int in Java is just an integer number without any kind of decimal portion. And yes, there are other types of numbers in Java. That's something that once again, we will discuss a little bit later. So right away, what you can see in Java is that because variable declarations include the type of the variable, Java is what's referred to as a statically typed language.
5:15All right. So Java's typing system is static, which means that the type of the variables is determined at compile time instead of at runtime. And this is in contrast to languages like JavaScript, which is part of a group of programming languages that are referred to as dynamically typed languages, right? So Java is a statically typed language, which means that the variable declarations have the type of the variable in them. And it also means that for the rest of the life of these variables, they can only ever hold that type, right?
5:50So just to give you an idea here of what some other types of variables might look like in Java, if we wanted to declare a string variable, which is just a piece of text that we want to manipulate in our program, then that would look like this, right? We could say string name equals, and then in double quotes, Sean. All right. So that would be how you declare a string variable that contains a value that looks something like this. It could really be anything much like what we passed to our system dot out dot print LN method previously.
6:25So the point here is that since we've declared X and Y with a specific type, as well as name with a different type, from now on in our program, we would never be able to sort of cross these two types over and say something like X equals Sean, right? That would immediately tell us that something's wrong. And if you hover over this, you'd see that we have an error that says type mismatch cannot convert from string to int, right? So in other words, Java is pretty strict about mixing types.
6:55So if you define something as an int, it has to stay an int. If you define something as a string, it has to stay a string. There are a few instances where Java says, okay, okay, I'll allow you to sort of mix types a little bit. But those are very specific cases. And that's not something I would rely on. Okay. And, you know, just like we weren't able to say X equals Sean, we also can't say name equals and then specify a number, right? Because even though it would be pretty easy for Java to just assume that you meant to say the string 10 instead of the number 10, Java is pretty strict about that just to avoid confusion in variable types.
7:34Cool. So anyway, getting back to our main thread here. Now that we have these X and Y variables, let's just do something like print out the sum of those two variables. And that'll look like this. We'll say system dot out dot print l n. And then what we'll do is we'll say the sum of X and Y is, and then we're going to add those two variables together and put it onto the end of the string. Now what we could do is just create a new variable called something like sum, right?
8:09You could call it total as well, whatever you want, basically, I'm going to call it sum. And then we can say X plus Y, right? And what that would do is it would take the two values of X and Y, add them together and store them in a new variable. And then we'd be able to take that sum variable. And this is one of those cases I was talking about earlier, where Java allows us to mix our variable types a little bit. We can actually say plus and then put some on the end there. Right.
8:39So in other words, Java is allowing us here to add a string and an int together . And the result is, of course, a string that now has that int at the end of it. All right. So that's one way to do it. You could also just cut out the extra variable and say plus X plus Y. But in that case, you'd want to wrap those in parentheses just to make sure that X and Y actually get added together before adding them on to the string. And just as a side note, in general, it's considered a good practice to just create new variables since that allows you to more easily understand what's going on here in most cases.
9:15All right. So let's just run this simple program that we've created to see if it works. And one thing to note is that if you go up here and hover over the run button, you'll see that it says run calculator dot Java. And at the same time, if we have hello world dot Java open, and let's just make sure we've saved it. If we hover over it now, it's going to say run hello world. And basically, that's just something that Eclipse does, right? It allows us to run the code inside a file as long as that class has a main method, right?
9:47Now you'll notice that if we comment this out, which you can do by just highlighting a piece of code and doing control forward slash or command forward slash depending on your operating system, if we save this, you'll see now that we no longer have the option to run calculator dot Java, right? Because this file has no entry point inside of it. You'll see that it just says run hello world. Okay. So let's uncomment that. And we're going to run our calculator file by just clicking this play button with calculator dot Java open.
10:20And sure enough, we see that it says the sum of X and Y is 40. And if we take a look at the math here, that is definitely correct. So anyway, hopefully you've learned a little bit here about declaring variables in Java as well as using them. I hope this has been informative for you and I'd like to thank you for viewing. [BLANK_AUDIO]
Writing and Running Java without an IDE
0:00All right, so so far in this section, we've discussed things like Java's basic syntax rules. We've seen how to declare variables. And we've also seen how we can have multiple files in the same project that have a main method and how this allows us to run these files individually. Now in this section, we've primarily been using the Eclipse IDE. And while this is a very common tool that developers use, what I want to do here is show you how it's possible to actually write and run JavaScript programs without an IDE or at least without an IDE that's specifically set up for Java. So what you're going to want to do here is just open up some IDE that doesn't naturally have support for Java. And what I 'm going to be doing here is using visual studio code, right visual studio code does have a lot of plugins you can use for Java development. But what it doesn't have you'll notice is a play button for easily running our Java code. All right, so what we're going to be doing here is seeing how we can write simple Java files and both compile them and run them
1:18using nothing but a simple text editor and a terminal. All right, so what we're going to do here is we're going to just create a new folder and I'll call this folder something like Java, no IDE. And then we'll open up that folder in visual studio code. And now that we've created that folder, what we're going to do is just open that up like this in our IDE. And then what we'll do is we'll just start off by creating a new folder and we'll call this folder something like calm. And then we'll create a folder inside of their called example. And finally inside there will create a new folder called hello. Okay, so what we're doing here is we're mirroring our package structure that we had in Eclipse. Now it's worth noting here that packages and Java are really behind the scenes just nested directories that Java treats a little bit differently than regular directories. So what we're going to do is inside this hello directory at the bottom, we're going to create a new file which we'll call something like hello again. Java. And then inside here, we're just going to want to write some simple Java code. So we'll start off by saying package and then we'll have the path to our package, which is conveniently listed right here above our code. So we'll say package,
2:38calm.example.hello. And of course, we'll put a semicolon at the end of that. And then we'll define a new class called hello again. And by the way, this is another rule in Java that the name of our class has to actually match the file name that the class is written in, right? So we can't name this something like my cool class, right? We have to name it the exact same string is was used for the file name. And then inside here, we're going to define our main method by saying public static void main. And then we'll have string with square brackets after it, which designates this as an array of strings, we'll discuss that later, of course. And then we'll say ours. And finally, inside of here, just to get ourselves going, we'll say system dot out dot print ln, and we'll print out a string that says hello again.
3:35This time, no IDE. Okay. And that is really all the code that we have to have for our Java program. So now that we want to run this program, we run into the rather interesting problem of not having that very convenient play button that Eclipse provided for us. Now in order to understand how we can run this program without that play button , we first need to very briefly discuss what that play button actually did any clips. Well , if you picture a Java project, most of what we interact with on a day to day basis as Java developers is source code, right? So these are all of the dot Java files that we can directly edit.
4:21And that are fairly human readable, right? You can at least recognize words and structure here in these programs. Okay. Well, unlike with languages such as JavaScript and Python, which are what are referred to as interpreted languages, Java is a compiled language. And what this means is that in order to run Java code that we write, we first have to compile it. Right now, this is basically where the compiler takes all of the human readable code that we've written and translates it into computer readable code that well, I'm sure some humans can read it, but I certainly can't. All right, so this code obviously looks quite a bit different. And it's generally in the form of a dot class file.
5:09Right. So if you see a dot class file, that's a compiled version of Java code. And the computer can then take this and run it. Right. So the computer knows how to actually take a class file and execute the commands that it contains. Now, this actually isn't 100% true, because in order for compiled Java code to be run, we actually have to use something called the Java virtual machine abbreviated JVM. But that's something that we'll discuss at some other time for now, just know that computers need this Java virtual machine thing to run a compiled Java file. So anyway, these two steps here compile and then run or execute or whatever you want to call it, are what the Eclipse IDE was doing for us behind the scenes, right, it was automatically compiling the changes to our code, and then running that behind the scenes. And if we want to recreate this functionality outside of an IDE, we're just going to need to execute those ourselves in the terminal. So here's what this is going to look like, what we're going to do is open up a terminal, and I'm using visual studio code. So I just have this integrated terminal here, you could always just open up your operating systems built in terminal if you want to. And the
6:27first thing that we need to do is check to see if we have a Java compiler installed, right, you might not because Eclipse actually includes a sort of prepackaged Java compiler with it. Now in order to check this, what you're going to need to do is just run Java C, right, that stands for Java compile or Java compiler. And then you're going to say dash dash version. And if you see the same or similar version to what I have here, you should be all good. If you get some kind of error saying that Java C was not understood, what you 're going to need to do is install Java. And you can do that by going to oracles website.
7:09And the best way that I've found to actually find what I'm looking for here is to use the search box and just say Java download. And that should take you to the corresponding page where you can download Java for your operating system. And as a side note here, in case you didn't notice, I'm currently using Java 17. At the time that you're watching this, there might be a later version. Don't worry too much about it because what I have here should work across pretty much any Java version, at least provided that they haven't made any huge changes since then. Okay, so anyway, once you've downloaded and installed Java, which should be a pretty straightforward process, you should be able to run this command here and see this output or something similar, of course. And this Java C command is what we use to actually compile Java code. So in order to compile our hello again.java file, all we need to do is say Java C, and then hello again.java. And if you hit enter, oops, it looks like we got an error.
8:13And that's just because I forgot to put in the full file path there, we need to actually say Java C, and then comm slash example slash hello, slash hello again.java. And what you'll see now is that this hello again.class file will show up next to our hello again.java file. And if you try and open this thing, your IDE is probably not going to want to show it to you, but I'm just going to click open anyway, and select text editor as the thing that we display this with. And sure enough, you'll see that this doesn't really contain anything human readable. There are a few things that are in here that you can make sense of, but overall, it's just gibberish. So the good news though is that this class file can be run on your computer from the terminal simply by saying Java, and then the path to the class file you want to run. Right, so we could say comm slash example slash hello, o ops, there we go slash hello slash hello again. And you actually don't want to put the dot class on the end because that will confuse the Java command. So let's hit enter. And sure enough, we see that it says hello again, this time no IDE, which is exactly the program that we wrote inside of here. Okay. And just as a side note, you can also run this
9:31by using dots instead of slashes, right? This would just be comm dot example dot hello dot hello again. And this is more the package name syntax for Java classes. And you can see that if you hit enter there, it does the exact same thing. Alright, so anyway, this has just been a very basic demonstration of the fact that it's possible to develop for Java without any IDE. But in general, most developers appreciate the extra help that I te's provide, right? Personally, I would never want to have to manually compile and run all of my Java code each and every time I want them to see the results of some kind of change I made to my program. But nevertheless, if you ever have to write some kind of command line script to compile Java code for you, that's always possible to do. And you would do that using the commands that we saw here as a foundation. So anyway, I hope this has been informative for you. And I'd like to thank you for viewing.
10:27[BLANK_AUDIO]
Frequently asked questions
What does the Introduction to the Java Language lesson cover?
The lesson walks a team member through creating and running a Java program in the Eclipse IDE, the basic structure of a Java program including classes, the main method, and packages, core Java syntax rules, declaring variables, and compiling and running Java without an IDE using a text editor and a terminal.
How long is the Introduction to the Java Language lesson?
The lesson runs 48 minutes. It opens the beginner-level Java course, which is a 12-hour, 6-minute per-learner investment, so a Training Manager can schedule it as the first step of a structured onboarding path.
Does a developer need prior Java or object-oriented programming experience to start this lesson?
No. The course is beginner-level, and the trainer introduces classes as categories of objects that behave in similar ways, noting that learners new to object-oriented programming should not worry because it is covered in more detail later in the course.
Which tools are used to write and run Java code in this lesson?
The lesson shows how to download Eclipse from the Eclipse Foundation website and run a program with its Run button. It then uses Visual Studio Code as a plain editor with a terminal to compile and run Java files manually, the foundation for writing command-line scripts that compile Java code.
What can a team member do after completing this Java lesson?
A team member can set up a simple Java project, print output to the console with System.out.println for debugging and logging, organize classes into packages to avoid naming collisions, avoid common syntax errors such as missing semicolons and incorrect capitalization of System and String, and declare variables. These skills give IT Directors a shared baseline before developers move on to control structures, OOP, and collections.
How can a Training Manager assign and track this Java course for a team?
CBT Nuggets Playlists can help Training Managers sequence this course with related development training. Team reporting provides per-learner progress and completion, cohort and group views, and completion exports for audit evidence, helping IT leaders spot learners who may need follow-up coaching.
Team training path
Turn this skill into assignable team training
This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.
Free training library
More free lessons in Programming
$59
seat / mo, billed annually
