How to execute a java program without using an IDE

How to execute a java program without using an IDE

To use the terminal, execute the following commands into the terminal. First of all, let’s make sure that our terminal is in the right location(where we need to execute the program)

The first instruction on the terminal would be –

javac ProgramName.java

In the above example, make sure that the program is just a placeholder here. Instead, we are going to put our program name there. For example, javac SyntaxExample.java

After the above instruction is executed, then we have to execute another instruction, and here it is –

java ProgramName

Again, in the above instruction, the ProgramName is just a placeholder. Instead, we have to put our program name there. For example, java SyntaxExample.

After this, you would be able to see the output of the program –

So, as an output from the program, we get the message printed on the terminal as –

Hello… from GyaniPandit!

So, this is how we have written our very first java program.

Now, if you are wondering that’s ok we have written our first java program, but we only understood the System.out.println thing. What about public, class, static, main, etc? So, we are going to discuss these terms now –

So, first of all, we need to understand that there is something called keywords(which we are going to see a bit later). But for now just understand that these are some words that have some special meaning, and they cannot be used anywhere else in the program for other uses.

This is like there are different doctors for treating different diseases like we have some general physicians, an ophthalmologist(for eyes), a dentist( for teeth), Cardiologists (related to the heart), and so on.

The basic idea is that if you have some problem related to the heart, you will not visit a dentist right? Because when it comes to a dentist, he/she is an expert in treating the problems related to teeth, gums, etc. Just like this, the keywords also are some words that are used for some specific purposes, and nowhere else.

The above example may not relate directly to what keywords are, but still, it becomes easy to understand that a cardiologist can be thought of when you have some problems related to the heart, and not skin. Now let’s come back to those keywords which we have been seeing for a while, so we need to explore them. We are also going to discuss what are keywords a little bit later.

So, public, class, static, and void are all keywords, while main is the name of the method we are going to write inside our class. Let’s understand these ones by one.

public → This is an access specifier(there are two more… protected and private, which we are going to see quite later in the course, but it is used here, so we are going to have a brief explanation of the public keyword). So, by access modifiers, we mean that we can modify what can be accessed inside a program. It’s just like in public places, anyone can visit, but the same is not with the private places, right? Later in the course, when we will study the concept of access modifiers, you will get familiar with the usage. So, let’s keep it here for now.

class → class is another keyword, used at the time of the creation of the class. We are going to study the concept of classes and objects under the concept of object-oriented programming. But for now, just consider that class is like a declaration of something. Like if you have a class Bottle, then it declares how a bottle should be.

We have methods and attributes inside of a class. Methods are nothing but some sort of behavior, like a bottle can be filled with water, emptied, opened, closed, or something like that. This was a quite abstract example of what a class is. We are going to go into greater detail about all of these while we study Object-Oriented Programming.

static → Static is another keyword, which can be used with variables, methods, some blocks, or nested classes. In this case, it is being used with method main. We are going to discuss the static keyword separately, but for now, let’s see what the static keyword means here.

Well, as said, it is being used with the main method, so, if we want to call the method, we do not need to create an object of that class.

Instead, we can directly call the method. If you are not familiar with the concept, just for now know that if you are creating some class, let’s say Bottle, then every bottle out in this world is an object of that Bottle class, and follows whatever there is inside the Bottle class. Usually, inside a class, we have some methods (behavior) and attributes(values), which every object has to follow.

So, every bottle can be filled with water or some other drink, and it can also be emptied, we can drink water from bottles, etc. I think now you can visualize it a little bit. Don’t worry if you did not get the full concept. It is not expected to.

Let’s come back to the static keyword. I was a bit lost in explaining the concept of OOP again. The concept in itself is very interesting as you will find out later in this course.

So, simply the thing is that there are methods inside a class, and the objects of that particular class are going to call this method(well, it depends on other parameters as well). But if we use static keywords while we create the method, then we need not create an object of the class to call this method. We can call the static method using the class name. We are going to see this later so don’t worry.

void → This is the keyword which is specifying that this method returns nothing. Usually, methods are written to do something. So, they may or may not return something.

We have different return types as well some methods can return an integer, or floating-point number, a character, or a boolean value(These are some datatypes that we are going to discuss very soon ahead). But this method main that we have here is returning nothing. So if some method is going to return nothing, then we write void there. You will get used to it when we start writing programs.

main → You can consider that the main method is the entry point of the program. We are writing all our code inside the class, and the execution starts with the execution of the main method. So, we are always going to write the main method, because it is the first thing that executes.

So, make sure to have this main method in your programs. The String[] args written inside the brackets are just some arguments. With arguments, we mean that we are passing some values to the function when we call it. We are going to study methods later. This thing will make sense afterward.

So, with this, we have understood some terms here used in the program, like a public, static, class, void, etc.

You will surely get to see these terms in every program that we are going to see.