Skip to content
CBT Nuggets
DemoBook a Demo

Introduction to the Java Language

This skill, led by Shaun Wassell, provides a comprehensive introduction to the Java programming language. It covers fundamental concepts such as basic syntax, variable declaration, and object-oriented programming principles. Learners will also gain practical experience by writing and running Java programs using the Eclipse IDE and the command line. The course is designed for individuals with 1 to 2 years of software development experience and aims to equip them with essential Java development skills.

Full skill from Java. Preview the IT training 23,000+ organizations trust.

48m

Skill 1 of 14 in Java

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]

0:05Hi, everyone.

0:06Shaun Wassell here.

0:07And welcome to this course where we're

0:08going to be learning all about the basics of Java development.

0:12In this section and the sections that follow,

0:14we're going to be covering a pretty wide variety

0:16of different Java-related topics,

0:18all the way from the basic syntax

0:21and structure of Java programs, through some

0:23of the more advanced topics, such as object-oriented

0:26programming, and the many built-in data structures

0:30that Java provides us with.

0:32So in this section, specifically, we're

0:34going to be getting started by seeing

0:36how to set up and run the Eclipse IDE,

0:39and use that to write Java programs.

0:41But we're also going to see how to run Java programs

0:44without using an IDE, just in case that's

0:47something you ever want to do.

0:49And additionally, we'll also be covering things

0:51such as how to declare variables and just

0:54the basic syntax of Java program.

0:57So without further ado, let's jump

0:59right into the world of Java.

Creating and Running a Java with Eclipse

0:00[AUDIO LOGO]

0:09All right, so to get started here,

0:10let's just jump right in and see how to create and run

0:13our first Java program.

0:14Now, the first thing that you might be wondering about,

0:18if you've done any kind of programming before,

0:20is, which IDE should we use for Java development?

0:25Well, the good news, and sort of the bad news here too,

0:28is that there are actually a lot of different options for Java

0:32development.

0:32And depending on what company you work with,

0:35depending on who you talk to, people

0:38tend to prefer different IDEs.

0:40But the most common one, and the one

0:41that we're going to be seeing how to use here,

0:43is an IDE called Eclipse.

0:45And there are others.

0:47There's another one called NetBeans.

0:50There's another one called IntelliJ IDEA.

0:56And then of course, there's always

0:57the option of using an IDE that isn't specifically made

1:01for Java but that includes different plug-ins you

1:03can use for Java development.

1:05And that would be something like Visual Studio Code, which

1:09is very commonly used for a pretty wide variety

1:12of programming languages.

1:13But as I said, we're going to start off

1:14by seeing how to use the Eclipse IDE to just write and run

1:19a simple Java program.

1:20So what you're going to want to do

1:21is go to the Eclipse Foundation's website.

1:25And currently, that's eclipse.org.

1:27I don't see that changing at any point in the near future.

1:30But of course, you never do see those changes coming.

1:32So anyway, just find the Eclipse Foundation's website.

1:35And once you're there, you're going

1:36to want to find the Download link.

1:40In my case, that was just a button up in the top corner.

1:42And you're going to want to find the link for downloading

1:45the Eclipse IDE for whatever operating system

1:48you're currently on.

1:49I'm on macOS.

1:50So I'm just going to click Download this x86 version here.

1:55And once you've downloaded that, you

1:57should see this "Thank you for your download" and just a thing

2:01asking you to donate to the Eclipse community.

2:03If you want to do that, go ahead.

2:05But I'm just going to skip over that for now.

2:07And what you're going to want to do

2:09is run the Eclipse Installer, which--

2:11let me just drag that back over to here.

2:14What you just downloaded there is

2:15going to have this Eclipse Installer thing inside of it.

2:18You can just click on that.

2:20In my case, since I'm on Mac, it's

2:21going to ask me if I really want to open this,

2:23because it's something that I downloaded from the internet.

2:26And then you should see the Eclipse Installer run.

2:29And then the next thing that you should see

2:31is this menu asking which Eclipse IDE

2:34you want to install.

2:36There are actually several different options,

2:38depending on what programming language you're using.

2:40And you'll even see that there's things

2:42like Eclipse Modeling Tools and Eclipse

2:44IDE for Scientific Computing.

2:46There's lots of options here.

2:47But what we're interested in is the most common one,

2:50and that's going to be the Eclipse

2:51IDE for Java Developers.

2:53So just click that.

2:53And the next thing that it's going to ask you

2:55is where you want the Java Virtual Machine.

2:58That's something that we'll talk about in a little bit

3:00more detail later.

3:02But for now, just leave the default option.

3:04And it's also going to ask you where you want

3:07to install the Eclipse IDE.

3:09I'm just going to leave that as the default option as well.

3:11But if there's another place you want that,

3:14feel free to change it.

3:15So at this point, you should be able to click Install.

3:17And that will run for you and install the Eclipse IDE.

3:20Since I already have it installed on my computer,

3:22I'm just going to leave it and exit out of the installer.

3:25And I'm just going to get rid of the installer here as well.

3:29All right, so now that you have the eclipse IDE installed,

3:31you're going to want to open that up.

3:33And the first thing it's going to ask you here

3:35is where you want your workspace to be.

3:38Now, what this is asking you is basically just

3:40where you want all of your Eclipse projects to be saved.

3:44So I've created this folder called eclipse-projects

3:47inside my home folder.

3:49And that's where I'm going to do it.

3:51But you can really set whatever path you want for that.

3:54Cool.

3:54So once you know where you want to store your Eclipse projects,

3:57you're just going to want to click Launch.

3:59And that will open the Eclipse IDE for you.

4:04So once you have the IDE open, the next thing

4:06that we're going to want to do is create a new Java project.

4:10And this will allow us to write and run our Java code.

4:13All right, so you should be able to create a new Java

4:16project in several ways.

4:18One is over here in the top left-hand corner,

4:20you should see Create a Java project.

4:22Or what you can do is just go up to the Menu

4:24and do File, New, and Java Project.

4:28But however you get to it right, however you tell Eclipse

4:31that you want to create a new Java

4:32project, the next thing that it's going to ask you is what

4:35you want to name the project.

4:37All right, now, this can really be whatever you want.

4:39But I'm going to call this project HelloWorld because we

4:42are going to be doing the famous Hello World example here

4:45just because this is going to give us a great foundation

4:48to start talking about some of the more fundamental concepts

4:51of Java programming.

4:53And once you've done that, you should just

4:55be able to click Finish.

4:56And leave the defaults and everything else too.

4:59And over here on the left-hand side,

5:01you should see this HelloWorld little folder thing up here.

5:04And if you expand that-- and it is pretty teeny tiny here.

5:07Hopefully, you can see OK.

5:09You should see that it has this JRE System Library thing there.

5:12We're just going to ignore that for now.

5:14And under that, we have source.

5:16And this is where we're going to write our code.

5:19So at this point, we've created a Java project.

5:22But the next thing that we have to do

5:24is in order to write some Java code,

5:26we need to create our first Java class.

5:30All right, so this is an interesting place where

5:32Java can start to diverge from a lot of other languages,

5:36especially scripting languages like JavaScript and Python

5:40is that in Java, pretty much everything,

5:43all of the logic that you're going to write in a program

5:45is contained somehow inside a class.

5:49So as you'll see, even when we want

5:51to write a simple Hello World program,

5:53we're still required to write a class.

5:56And inside that class is where we'll

5:58specify the actual logic for the program.

6:01All right, so here's what this is going to look like.

6:03What you're going to want to do is right-click on this source

6:06folder here.

6:08And then we're going to go to New.

6:10And you're going to want to find Class in this dropdown menu.

6:14So let's just click on that.

6:15And then what it's going to ask you

6:17is what you want to name this class.

6:19Now, since we're just creating a simple Hello World example,

6:22we're going to call this HelloWorld in Pascal case,

6:25starting with a capital letter and with every new word

6:29in that name starting with a capital letter

6:31as well, which is almost always how Java classes are named.

6:35All right, and we're also going to want to specify

6:37a package for this class.

6:40And packages are something that we'll discuss

6:41a little bit later as well.

6:43They just help us keep all of our code organized

6:46and prevent things like naming collisions in our projects.

6:49So what we're going to do is just say com.example.hello.

6:54And that will be the package name

6:57that we'll use for this simple class.

6:58Again, we'll discuss this in more detail as we go along.

7:01So now that we have our name and package specified,

7:04there's one more thing that we're going to do here.

7:06And that is-- you see this little option

7:08that says public static void main, blah, blah, blah?

7:11Well, we're going to check that.

7:13And that's something that I'll discuss in more detail in just

7:16a minute here as well.

7:17But you're going to want to select that.

7:19And then we're going to click Finish.

7:21And this will create our new class for us

7:24and open that up in our editor.

7:27So in order to write a simple Hello World example in Java,

7:31what we're going to do-- and we will

7:32talk more about the rest of the code here shortly.

7:35But what we're going to do is delete

7:37any comments that are inside this public static void

7:40main method, which, again, we'll talk about in more detail

7:44shortly.

7:45And inside there, what we're going to do

7:46is we're going to say System.out.println.

7:52All lowercase there with the println.

7:54And then inside here, what we're going to do

7:56is we're going to say double quotes "Hello world."

8:01And then after that, we're going to put a semicolon

8:04because in Java, all lines have to end in a semicolon.

8:08This isn't like Python where a newline indicates

8:11the end of a statement.

8:12And it's definitely not like JavaScript,

8:14where you have the option of adding a semicolon or not.

8:17In Java, you don't have the choice.

8:19You have to add a semicolon.

8:20So what we just wrote here is really the simplest

8:23possible Java program.

8:24And you might notice that it's a little bit more complex than,

8:27let's say, the simplest program in Python

8:30or the simplest program in JavaScript.

8:34And that's because of all this extra stuff here--

8:36the public static void main, public class HelloWorld,

8:39the package name, et cetera.

8:41But before we discuss that in more detail,

8:44let's see how to run the program.

8:46The good news here is that as long as you're using Eclipse,

8:48this is pretty easy.

8:50You can just go up here to this little green Play button

8:53with nothing else beside.

8:54There are other options here, as you can see.

8:57And you're just going to click on that.

8:58And you can see, if you hover over it,

9:00it says Run HelloWorld.

9:02So let's just click on that.

9:03And it might ask you to save a certain number of files.

9:07If there's anything that the code you're running

9:09relies on that hasn't been saved,

9:11that's going to show up here.

9:12And we're just going to click OK.

9:14And that will save the file.

9:16And down here at the bottom of our Eclipse window,

9:19you'll see that, sure enough, it prints out

9:21"Hello world" to the console.

9:24So that is really the simplest possible Java program.

9:27Congratulations on writing and running your first program

9:30in Eclipse.

9:31So I hope this has been informative for you,

9:33and I'd like to thank you for viewing.

Basic Structure of a Java Program

0:00All right.

0:09So now that we've seen how to set up and run a simple Java program, what I want

0:13to do next

0:14is go back and actually take a look at some of the finer details that we

0:17encountered in

0:18this process.

0:19So the first thing that I want to take a look at here is this system.out.print.

0:25line that

0:26actually 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

0:35the console.

0:36And it's very frequently used for things like debugging.

0:40And even in production, it's still used to store just little pieces of

0:44information in

0:45the logs.

0:46So especially in these initial stages as we're learning Java, we'll be using

0:50this system.out.print.line

0:52function quite a bit in order to see the results of the programs that we write.

0:58All right.

0:59And even in more complex command line applications, right, if you want to write

1:03some kind of, let's

1:04say file manager that the user can use in a terminal, then most of the time you

1:09'll still

1:10stick to using system.out.print.line.

1:12All right.

1:13So I want to point out here too that you can easily change this string that's

1:18inside

1:19this function.

1:20So right now we've specified the string.

1:22And by the way, in Java, strings are just data that represents some sort of

1:27text, right?

1:28In our case, we have the sentence Hello World.

1:31And another thing to notice here is that we've specified this string with

1:34double quotes.

1:35Now, unlike in other languages, such as JavaScript and Python, where you have

1:40the option to use

1:41either double quotes or single quotes, in Java, strings can only be specified

1:45with double

1:46quotes, right?

1:47You can see this if you try and switch the double quotes to single quotes, you

1:51'll get

1:51some kind of error saying invalid character constant.

1:55And the reason you see that error is because in Java, single quotes are only

1:59used to specify

2:00characters, which are basically strings that contain only a single letter if

2:05you want to

2:05think about them that way.

2:07All right.

2:08So let's change those back to double quotes.

2:10And let's change the string that we're printing out here.

2:12All right.

2:13So we'll change this string to something like Java is awesome.

2:16Okay.

2:17Well, if we save that and then go and click this little run button again, sure

2:21enough,

2:22we'll see that instead of Hello World, Java is awesome is printed out to the

2:26console.

2:27Cool.

2:28So hopefully you understand at least this line now.

2:30So let's move on out to this public static void main line.

2:34And while we're at it, let's also talk about public class Hello World.

2:38As I said before, in Java programs, pretty much everything is grouped into

2:42these things

2:43called classes that has some sort of name that helps us identify what the class

2:48is meant

2:49to do.

2:50Now at some point, we'll go through a proper examination of object oriented

2:54programming

2:55in Java and, you know, how it works, how it can help us, et cetera.

2:59But for now, all you really need to know about classes is that they represent

3:03sort

3:03of categories of objects that all behave in a similar way, right?

3:08So you could have something like a car class.

3:10You could have something like a database class.

3:14And the idea is that from these classes, you can create what are called

3:18instances, which

3:19are specific objects that behave in a certain way.

3:23All right, don't worry too much about that.

3:25If that all sounds new to you, and if you're already familiar with object

3:28oriented programming,

3:29well, that's great as well.

3:31All right.

3:32So what we've done here is we've created a Hello World class.

3:35And by itself, this class doesn't really have any behavior, right?

3:40But what it does have is this public static void main method, and by the way,

3:46functions

3:46that we define inside classes are called methods.

3:51And what this public static void main method does is it tells our Java project

3:56where to

3:56start execution, right?

3:58So in other words, in a Java program, we could have lots and lots of different

4:02classes.

4:03And so when we run our program, Eclipse has to know where to start running that

4:07program

4:08from.

4:09As it happens, this public static void main method is what it goes around and

4:14looks for.

4:15All right.

4:16Now it is possible to have multiple main methods in a project.

4:20And that would be the case if your project needs multiple entry points, such as

4:25a client

4:25side program and a server side program, or, you know, two separate programs

4:30that need

4:31to talk to each other, but operate independently.

4:35That's something that we'll take a look at much later.

4:37So don't worry too much about that right now.

4:39All right.

4:40So that's just a brief explanation of classes and the main method in Java

4:44programs.

4:45The last thing that I want to discuss here is this package, calm.example.hello

4:50thing

4:51up at the top.

4:52Now as I mentioned very briefly, a few minutes ago, packages in Java help us

4:57organize our

4:58projects more effectively, right?

5:00Now you can probably imagine that if we had only 10 or even 20 classes in our

5:06project,

5:07that the need for organizing all of those classes into different packages

5:11wouldn't be

5:11as great, right?

5:12We could probably keep track of that project without separating them into

5:17packages.

5:17But as our Java programs get bigger and bigger and bigger, and it's not

5:21uncommon to have

5:22Java projects that have thousands or probably even tens of thousands or

5:26hundreds of thousands

5:28of different classes in extreme cases.

5:31But in those situations, you definitely want to have some sort of organization

5:35to all

5:35of your code.

5:36And that's what packages in Java allow us to do.

5:38You can think of packages in Java as sort of like directories and sub

5:42directories in

5:43our project, but that have an actual meaning and that influence how the program

5:48operate,

5:49right?

5:50So just to give you a little demonstration of that here, if we were to try and

5:53create

5:53a new class inside this calm.example.hello package that we just created, right?

5:59Let's try and create another class called hello world.

6:01Well, we're going to see that we get this error here saying type already exists

6:06, right?

6:06And that's because we already have a hello world class inside this package.

6:11However, what you'll notice is that if you change the package here to something

6:15like

6:15calm.example.greet, right, we won't get that same error, right?

6:21It'll allow us to create this new class.

6:23And if you click finish, then you'll see that we have this other hello world

6:27class in a

6:27different package, right?

6:29So in other words, Java forces us to keep our class names unique, but only

6:34inside a specific

6:36package, right?

6:37So what this means is that it's perfectly fine to have multiple classes with

6:41the same

6:41name, but they have to be separated into different packages.

6:46And the idea there is that the package gives a little bit of context to what

6:50that class

6:51is for.

6:52All right, so that's the basic idea of packages in Java.

6:56Hopefully this brief discussion of classes and main methods and packages has

7:00helped you

7:00to understand a little better how Java projects are organized.

7:04And obviously we're going to be seeing this in much more detail as we go on.

7:08So I hope this has been informative for you, and I'd like to thank you for

7:11viewing.

7:11[BLANK_AUDIO]

Java Syntax Rules

0:00All right, so now that we've discussed a little bit of the structure of Java

0:13programs, the

0:13next thing that I want to do is talk a little bit more about the basic Java

0:19syntax. And

0:20we're also going to see how to declare variables in Java, which obviously in

0:24any programming

0:25language is a pretty important thing to know how to do. So first things first,

0:30in no particular

0:31order, I'm going to cover a few of the most important syntax rules in Java. All

0:36right, and

0:36it's important to know that if you break one of these syntax rules, right? So

0:40for example,

0:41if you leave a semicolon off the end of a line, then the program won't actually

0:46run, right?

0:47And this is a little bit different from some of the other programming languages

0:51out there,

0:52such as JavaScript, which will happily execute your code up until it finds

0:56something wrong

0:58with your syntax, at which point it will give you an error. So you can see this

1:02, by the way,

1:03if you just remove the semicolon, save the file and try and run the code, what

1:08you'll get is

1:09instead of any kind of output down here, it'll just say errors exist blah, blah

1:13, blah. And you

1:14can tell it to proceed, but you're going to get an error, right? It'll say

1:18error in thread main.

1:19And it'll tell you that there's a syntax error of some sort, right? So if you

1:23see this syntax

1:24error string, well, as you can imagine, that means you have a syntax error and

1:28depending on your ID,

1:30it may even tell you where that syntax error is, right? In our case, it tells

1:34it that it's in

1:35Hello World main and line six. So it leads us right to it. And sure enough, if

1:41we add the semicolon

1:42back and try and run our program again, we'll see that our program runs without

1:46a hitch. Cool.

1:48So now that we're clear on how important it is to have correct syntax in your

1:52Java programs,

1:53let's discuss some of the most important syntax rules in Java. The first one is

1:58that Java is case

2:00sensitive, right? So what that means is if you were to try and say system dot

2:05out dot print ln,

2:07and perhaps you even made this mistake earlier, if you were following along,

2:10right, if you write

2:11system with a lowercase s instead of the correct uppercase s, that's going to

2:16be an error, right?

2:17It's going to tell you it doesn't know what system is and we can see this if we

2:21save it and try and

2:23run it and tell it to proceed. Sure enough, we'll see that Java's telling us

2:26that it has no idea

2:28what system with a lowercase s is. But if we change that back to an uppercase s

2:33, we'll see that

2:34that works without a problem. Okay, so that's just something that's important

2:38to remember. And it

2:39can get a little bit tricky, especially. And this is problem that still

2:42occasionally trips me up

2:44sometimes is this string type. Right, so we'll talk about the string type in

2:48much more detail

2:49when we talk about Java's data types. But in Java, the string type has a

2:55capital s and what most

2:57people will do is write that with a lowercase s, which is not actually a thing.

3:01Okay, cool. So

3:02Java's case sensitive. And we've already seen that Java requires us to end all

3:07statements with a

3:08semicolon. And besides that we've also talked about the fact that Java programs

3:13are made up of a

3:15series of classes. And these classes, as I mentioned earlier, are actually

3:19required to start with a

3:21capital letter. Right, and this usually means that they're in Pascal case,

3:25which is just like

3:26camel case, except it starts with a capital letter. And as we've also mentioned

3:30, these classes have a

3:32series of methods such as this main method here. And in Java, the names of

3:36these methods are

3:37required to be lowercase, right? So in other words, they're required to start

3:42anyway, with a lower

3:44case character. So you couldn't have, for example, a method called main with a

3:49capital M. All right,

3:50and Java just does that to help differentiate between things that are classes

3:54and things that are

3:55methods and variables, etc. All right, now besides those things, really, the

3:59only other piece of

4:00syntax that I wanted to discuss before we talk about how to declare variables

4:05in Java is how to

4:07do comments in Java programs. All right, now depending on which programming

4:11languages you already know,

4:13this may or may not look very familiar, in particular, if you're familiar with

4:18JavaScript,

4:18this is going to look very familiar. In Java, we can do comments, which are

4:23basically just text

4:25in our code files that the compiler will ignore. And you can write a comment by

4:29putting two forward

4:31slashes. And then after that, you can write whatever you want, right? So we can

4:35say something like,

4:36hello, everyone. And the compiler will just ignore that, right? In general,

4:42nothing that we put after

4:44a comment will directly affect the execution of the program. Now, depending on

4:49your ID settings,

4:50and whether or not you're automatically generating documentation from comments,

4:54that's another

4:55story. But in general, comments allow you to just tell other programmers or

4:59yourself later on,

5:00what a given line was for, right? So we could say something like, this is how

5:05you print to the

5:07console in Java. Okay, now the two forward slashes will create a single line

5:14comment. But in many

5:16cases, we want to create a multi line comment. And in Java, the way that we do

5:20this is by using a

5:21forward slash and an asterisk. And this is closed off by an asterisk and a

5:28forward slash. All right,

5:30now my ID is automatically adding these asterisks in between as well, which I

5:34think looks nice,

5:35but that's actually not necessary, right? You can just say, hello, this is a

5:43multi line comment.

5:46And all of that will be ignored by the Java compiler. All right, so anyway,

5:51that's how to do comments

5:52in Java. So the next thing that we're going to take a look at is how to start

5:56declaring variables

5:58in our Java program. So I hope this has been informative for you. And I'd like

6:02to thank you for

6:02viewing.

6:03[BLANK_AUDIO]

Declaring Variables in Java

0:00All right, so now that we've discussed a few of the basic syntax rules in Java,

0:13the next

0:13thing that I want to talk about is how to declare variables.

0:16Now declaring variables is obviously a very important part of any programming

0:20language,

0:21and the good news is that Java does it in a pretty similar way to most

0:25programming languages

0:27that you'll find.

0:28So in order to demonstrate this, what we're going to do is we're going to

0:33create a new

0:34class in our project here.

0:37And instead of the hello world example, what we're going to do is again, we'll

0:40right click

0:41on the source folder and say that we want to create a new class.

0:46And then what we'll do is we'll call this class something like calculator.

0:49All right, you'll see why we call it that in just a minute here.

0:53And we're going to put it in a different package from our hello world class.

0:57So what we'll do is we'll do calm.example.calculator.

1:02And by the way, I don't think I've mentioned yet why exactly package names are

1:06the way

1:06they are, right?

1:08In other words, you might be wondering why they start with calm and have all

1:11these different

1:12segments in them.

1:13Well, basically the convention with Java package names since Java packages can

1:18be shared with

1:19other developers and other organizations is to name your packages the reverse

1:25of your domain

1:27name.

1:28So in other words, let's say that you were working for XYZ corporation and your

1:33corporations

1:34domain name was XYZ Corp.com.

1:37Your 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

1:46more specific,

1:47right?

1:48So if you wanted to do calm.xyz Corp.math.calculator, right?

1:56That would sort of provide a path down from more general things, such as your

2:00entire company

2:02and then down through specific sections and specific features of your app.

2:06And another way that you could picture this is as just a series of nested

2:10directories,

2:11right?

2:12So, you know, in computer directories, you generally go from more general

2:16directories,

2:17such as your user directory to more specific directories, such as documents or

2:23downloads.

2:24And then those could even have more specific directories inside of them.

2:27And that's pretty much what Java package names do as well.

2:30All right.

2:31So now that we've touched on why package names are the way they are, let's

2:34create this new

2:35calculator class here.

2:38And what we're going to do is inside here, we'll give this calculator class

2:41that was

2:42just created the same main method as we had in our Hello world class.

2:48And you can even just copy and paste this if you want.

2:50Although personally, I've found that actually typing things out manually can

2:55really help

2:56you remember what things are and speak about them a little bit more intellig

3:00ently.

3:00So what we'll do here is we'll just type this out.

3:03We'll say public static void main.

3:07And then the argument that this main method takes is a string array called ARGS

3:12.

3:12All right.

3:13Now that's something that we'll discuss in a bit more detail shortly.

3:16Basically, what this argument does is it gives us access to any other command

3:20line arguments

3:21that the user has entered when running our program, right?

3:25So in other words, you're allowed to run JavaScript programs with arguments

3:30just like

3:30you'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.

3:37But anyway, what we're going to do is inside this calculator function, for now

3:42we'll start

3:43off by defining a few variables and adding them together.

3:47And really the point of all of this is to show us how to declare variables in

3:51Java.

3:52All right.

3:53So let's start off here by defining two numbers called X and Y. And the way

3:57that we do that

3:58in Java is by saying int, right?

4:00So I and T X equals, and then we'll just give X a value.

4:06We'll say 10.

4:07And of course, we'll close that off with a semicolon because all statements in

4:11Java have

4:11to end with semicolons.

4:13And then we'll do the same thing for Y. So we'll say int Y equals, and let's

4:18just make

4:19this something like 30.

4:20Cool.

4:21So what you see here is two examples of how to declare variables in Java.

4:26It's really not that much different from a lot of other programming languages,

4:29but the

4:29main difference to note, which is again, something that we'll discuss in much

4:33more detail later

4:34on, is that in Java, when you declare variables, the variable declaration

4:40includes the type

4:41of the variable.

4:42So in other words, since we're defining two integer numbers here that we'll

4:47want to add

4:47together, the type of these variables is int, right?

4:52Int in Java is just an integer number without any kind of decimal portion.

4:57And yes, there are other types of numbers in Java.

4:59That's something that once again, we will discuss a little bit later.

5:03So right away, what you can see in Java is that because variable declarations

5:08include

5:08the type of the variable, Java is what's referred to as a statically typed

5:14language.

5:15All right.

5:16So Java's typing system is static, which means that the type of the variables

5:21is determined

5:22at compile time instead of at runtime.

5:25And this is in contrast to languages like JavaScript, which is part of a group

5:29of programming

5:30languages that are referred to as dynamically typed languages, right?

5:36So Java is a statically typed language, which means that the variable

5:40declarations have

5:41the type of the variable in them.

5:44And it also means that for the rest of the life of these variables, they can

5:48only ever

5:49hold that type, right?

5:50So just to give you an idea here of what some other types of variables might

5:55look like in

5:56Java, if we wanted to declare a string variable, which is just a piece of text

6:01that we want

6:02to manipulate in our program, then that would look like this, right?

6:06We could say string name equals, and then in double quotes, Sean.

6:10All right.

6:11So that would be how you declare a string variable that contains a value that

6:16looks something

6:17like this.

6:18It could really be anything much like what we passed to our system dot out dot

6:22print

6:22LN method previously.

6:25So the point here is that since we've declared X and Y with a specific type, as

6:29well as

6:30name with a different type, from now on in our program, we would never be able

6:34to sort

6:35of cross these two types over and say something like X equals Sean, right?

6:41That would immediately tell us that something's wrong.

6:43And if you hover over this, you'd see that we have an error that says type

6:47mismatch cannot

6:47convert from string to int, right?

6:49So 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.

6:57If you define something as a string, it has to stay a string.

7:00There are a few instances where Java says, okay, okay, I'll allow you to sort

7:06of mix

7:06types a little bit.

7:08But those are very specific cases.

7:09And that's not something I would rely on.

7:12Okay.

7:13And, you know, just like we weren't able to say X equals Sean, we also can't

7:17say name

7:17equals and then specify a number, right?

7:20Because even though it would be pretty easy for Java to just assume that you

7:23meant to

7:24say the string 10 instead of the number 10, Java is pretty strict about that

7:30just to avoid

7:32confusion in variable types.

7:34Cool.

7:35So anyway, getting back to our main thread here.

7:39Now that we have these X and Y variables, let's just do something like print

7:43out the

7:43sum of those two variables.

7:46And that'll look like this.

7:47We'll say system dot out dot print l n.

7:52And then what we'll do is we'll say the sum of X and Y is, and then we're going

7:58to add

7:59those two variables together and put it onto the end of the string.

8:03Now what we could do is just create a new variable called something like sum,

8:08right?

8:09You could call it total as well, whatever you want, basically, I'm going to

8:12call it

8:12sum.

8:13And then we can say X plus Y, right?

8:16And what that would do is it would take the two values of X and Y, add them

8:19together

8:20and store them in a new variable.

8:22And then we'd be able to take that sum variable.

8:26And this is one of those cases I was talking about earlier, where Java allows

8:31us to mix

8:31our variable types a little bit.

8:34We can actually say plus and then put some on the end there.

8:38Right.

8:39So in other words, Java is allowing us here to add a string and an int together

8:44.

8:45And the result is, of course, a string that now has that int at the end of it.

8:50All right.

8:51So that's one way to do it.

8:52You could also just cut out the extra variable and say plus X plus Y.

8:56But in that case, you'd want to wrap those in parentheses just to make sure

8:59that X and

9:00Y actually get added together before adding them on to the string.

9:06And just as a side note, in general, it's considered a good practice to just

9:09create new

9:10variables since that allows you to more easily understand what's going on here

9:14in most cases.

9:15All right.

9:16So let's just run this simple program that we've created to see if it works.

9:20And one thing to note is that if you go up here and hover over the run button,

9:24you'll

9:25see that it says run calculator dot Java.

9:29And at the same time, if we have hello world dot Java open, and let's just make

9:33sure we've

9:33saved it.

9:34If we hover over it now, it's going to say run hello world.

9:38And basically, that's just something that Eclipse does, right?

9:41It allows us to run the code inside a file as long as that class has a main

9:46method, right?

9:47Now you'll notice that if we comment this out, which you can do by just

9:51highlighting

9:52a piece of code and doing control forward slash or command forward slash

9:57depending on

9:58your operating system, if we save this, you'll see now that we no longer have

10:03the option

10:04to run calculator dot Java, right?

10:06Because this file has no entry point inside of it.

10:09You'll see that it just says run hello world.

10:12Okay.

10:13So let's uncomment that.

10:14And we're going to run our calculator file by just clicking this play button

10:18with calculator

10:18dot Java open.

10:20And sure enough, we see that it says the sum of X and Y is 40.

10:25And if we take a look at the math here, that is definitely correct.

10:29So anyway, hopefully you've learned a little bit here about declaring variables

10:32in Java

10:32as well as using them.

10:35I hope this has been informative for you and I'd like to thank you for viewing.

10:38[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

0:13syntax

0:13rules. We've seen how to declare variables. And we've also seen how we can have

0:18multiple

0:19files in the same project that have a main method and how this allows us to run

0:24these

0:25files individually. Now in this section, we've primarily been using the Eclipse

0:30IDE. And while

0:31this is a very common tool that developers use, what I want to do here is show

0:36you how

0:37it's possible to actually write and run JavaScript programs without an IDE or

0:43at least without

0:44an IDE that's specifically set up for Java. So what you're going to want to do

0:49here is

0:49just open up some IDE that doesn't naturally have support for Java. And what I

0:54'm going to

0:55be doing here is using visual studio code, right visual studio code does have a

1:01lot of

1:01plugins you can use for Java development. But what it doesn't have you'll

1:05notice is a play

1:06button for easily running our Java code. All right, so what we're going to be

1:12doing here

1:12is seeing how we can write simple Java files and both compile them and run them

1:18using nothing

1:19but a simple text editor and a terminal. All right, so what we're going to do

1:23here is

1:24we're going to just create a new folder and I'll call this folder something

1:29like Java,

1:30no IDE. And then we'll open up that folder in visual studio code. And now that

1:35we've created

1:36that folder, what we're going to do is just open that up like this in our IDE.

1:41And then

1:42what we'll do is we'll just start off by creating a new folder and we'll call

1:46this folder

1:48something like calm. And then we'll create a folder inside of their called

1:53example. And

1:54finally inside there will create a new folder called hello. Okay, so what we're

1:58doing here

2:00is we're mirroring our package structure that we had in Eclipse. Now it's worth

2:05noting here

2:05that packages and Java are really behind the scenes just nested directories

2:10that Java treats

2:11a little bit differently than regular directories. So what we're going to do is

2:15inside this

2:16hello directory at the bottom, we're going to create a new file which we'll

2:20call something

2:21like hello again. Java. And then inside here, we're just going to want to write

2:27some simple

2:27Java code. So we'll start off by saying package and then we'll have the path to

2:32our package,

2:33which is conveniently listed right here above our code. So we'll say package,

2:38calm.example.hello.

2:41And of course, we'll put a semicolon at the end of that. And then we'll define

2:45a new

2:45class called hello again. And by the way, this is another rule in Java that the

2:50name

2:51of our class has to actually match the file name that the class is written in,

2:55right?

2:56So we can't name this something like my cool class, right? We have to name it

3:04the exact

3:04same string is was used for the file name. And then inside here, we're going to

3:09define

3:09our main method by saying public static void main. And then we'll have string

3:15with square

3:16brackets after it, which designates this as an array of strings, we'll discuss

3:20that later,

3:21of course. And then we'll say ours. And finally, inside of here, just to get

3:26ourselves going,

3:27we'll say system dot out dot print ln, and we'll print out a string that says

3:33hello again.

3:35This time, no IDE. Okay. And that is really all the code that we have to have

3:43for our

3:43Java program. So now that we want to run this program, we run into the rather

3:48interesting

3:49problem of not having that very convenient play button that Eclipse provided

3:55for us.

3:56Now in order to understand how we can run this program without that play button

3:59, we first

4:00need to very briefly discuss what that play button actually did any clips. Well

4:06, if you

4:06picture a Java project, most of what we interact with on a day to day basis as

4:11Java developers

4:12is source code, right? So these are all of the dot Java files that we can

4:19directly edit.

4:21And that are fairly human readable, right? You can at least recognize words and

4:26structure

4:27here in these programs. Okay. Well, unlike with languages such as JavaScript

4:33and Python,

4:34which are what are referred to as interpreted languages, Java is a compiled

4:39language. And

4:40what this means is that in order to run Java code that we write, we first have

4:45to compile

4:46it. Right now, this is basically where the compiler takes all of the human

4:52readable code

4:53that we've written and translates it into computer readable code that well, I'm

4:59sure

4:59some humans can read it, but I certainly can't. All right, so this code

5:03obviously looks

5:03quite a bit different. And it's generally in the form of a dot class file.

5:09Right. So

5:10if you see a dot class file, that's a compiled version of Java code. And the

5:16computer can

5:17then take this and run it. Right. So the computer knows how to actually take a

5:21class

5:22file and execute the commands that it contains. Now, this actually isn't 100%

5:27true, because

5:28in order for compiled Java code to be run, we actually have to use something

5:32called the

5:34Java virtual machine abbreviated JVM. But that's something that we'll discuss

5:40at some

5:41other time for now, just know that computers need this Java virtual machine

5:45thing to run

5:46a compiled Java file. So anyway, these two steps here compile and then run or

5:52execute

5:53or whatever you want to call it, are what the Eclipse IDE was doing for us

5:57behind the

5:58scenes, right, it was automatically compiling the changes to our code, and then

6:03running

6:03that behind the scenes. And if we want to recreate this functionality outside

6:07of an

6:08IDE, we're just going to need to execute those ourselves in the terminal. So

6:13here's

6:14what this is going to look like, what we're going to do is open up a terminal,

6:17and I'm

6:17using visual studio code. So I just have this integrated terminal here, you

6:21could always

6:22just open up your operating systems built in terminal if you want to. And the

6:27first

6:28thing that we need to do is check to see if we have a Java compiler installed,

6:32right,

6:33you might not because Eclipse actually includes a sort of prepackaged Java

6:39compiler with it.

6:41Now in order to check this, what you're going to need to do is just run Java C,

6:44right, that

6:45stands for Java compile or Java compiler. And then you're going to say dash

6:50dash version.

6:52And if you see the same or similar version to what I have here, you should be

6:56all good.

6:57If you get some kind of error saying that Java C was not understood, what you

7:02're going

7:02to need to do is install Java. And you can do that by going to oracles website.

7:09And the

7:10best way that I've found to actually find what I'm looking for here is to use

7:14the search

7:14box and just say Java download. And that should take you to the corresponding

7:21page where you

7:21can download Java for your operating system. And as a side note here, in case

7:26you didn't

7:26notice, I'm currently using Java 17. At the time that you're watching this,

7:31there might

7:32be a later version. Don't worry too much about it because what I have here

7:35should work across

7:37pretty much any Java version, at least provided that they haven't made any huge

7:42changes since

7:43then. Okay, so anyway, once you've downloaded and installed Java, which should

7:47be a pretty

7:47straightforward process, you should be able to run this command here and see

7:52this output

7:53or something similar, of course. And this Java C command is what we use to

7:58actually compile

8:00Java code. So in order to compile our hello again.java file, all we need to do

8:06is say

8:06Java C, and then hello again.java. And if you hit enter, oops, it looks like we

8:12got an error.

8:13And that's just because I forgot to put in the full file path there, we need to

8:16actually

8:17say Java C, and then comm slash example slash hello, slash hello again.java.

8:24And what you'll

8:25see now is that this hello again.class file will show up next to our hello

8:30again.java file.

8:32And if you try and open this thing, your IDE is probably not going to want to

8:36show it

8:37to you, but I'm just going to click open anyway, and select text editor as the

8:42thing

8:42that we display this with. And sure enough, you'll see that this doesn't really

8:45contain

8:46anything human readable. There are a few things that are in here that you can

8:50make sense of,

8:51but overall, it's just gibberish. So the good news though is that this class

8:55file can be

8:56run on your computer from the terminal simply by saying Java, and then the path

9:03to the class

9:04file you want to run. Right, so we could say comm slash example slash hello, o

9:09ops, there

9:10we go slash hello slash hello again. And you actually don't want to put the dot

9:15class

9:15on the end because that will confuse the Java command. So let's hit enter. And

9:20sure enough,

9:21we see that it says hello again, this time no IDE, which is exactly the program

9:26that

9:26we wrote inside of here. Okay. And just as a side note, you can also run this

9:31by using

9:31dots instead of slashes, right? This would just be comm dot example dot hello

9:36dot hello

9:36again. And this is more the package name syntax for Java classes. And you can

9:43see that if you

9:43hit enter there, it does the exact same thing. Alright, so anyway, this has

9:47just been a very

9:47basic demonstration of the fact that it's possible to develop for Java without

9:53any IDE.

9:54But in general, most developers appreciate the extra help that I te's provide,

9:58right?

9:59Personally, I would never want to have to manually compile and run all of my

10:04Java code

10:05each and every time I want them to see the results of some kind of change I

10:08made to my

10:09program. But nevertheless, if you ever have to write some kind of command line

10:13script

10:14to compile Java code for you, that's always possible to do. And you would do

10:19that using

10:20the commands that we saw here as a foundation. So anyway, I hope this has been

10:24informative

10:25for you. And I'd like to thank you for viewing.

10:27[BLANK_AUDIO]

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.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Book a Demo
Just need Java?

Learning on your own? Browse individual plans ($49/month, billed annually)

Not ready to buy?
with no purchase required. Already have an account?
Book a Demo