Java Demo Program | First Java Program

I hope you are walking the way I am, following the right way. That being said, before learning your first java program and doing the java demo program I would like you to read the first chapter of Java.

If you have already sailed on that ship, let’s move forward and see what’s Java has in store for us.

What’s What in Java: Using Java

I know it is confusing when you look at tons of codes and wonder if you will ever get there. A kind of gibberish that makes you instantly go averse to it. Just look at Java as French or Spanish, a language you know nothing about. Or even better try to relate to it as an alien language like that in the movie Arrival.

Now you don’t know anything about how to read, write or interpret, but it is there, and it is getting the job done. Aliens are talking to each other, all you need to do is learn to speak their language even though it takes you months or years to become really efficient at it. No problemo!

Java Funny Meme Speaking In Java

When I tell you this ‘&’ sign means ‘and’ why does your mind register it? Because you have learnt this early on, and now aware of it unconsciously. Similarly there are things in Java that are English that will make you instantly understand what’s what, and what is called what. There are just some terminologies that your mind needs to register. When you do, trust me you will begin talking to your Mom in Java. But please don’t.

I hate to mug things up because I believe learning can’t be forced. But with languages you are left with no options but to con, because that’s what you need to interact with computers. If you speak English and expect your computer to respond, it would just sit there wondering how dumb you are.

Process of Programming in Java

Programming in Java requires three cardinal processes to be followed.

  1. Creation: This is where we are going to write a program in a notepad file and save it into a java extension file format. Our files are gonna look like: myProgram.java
  2. Compilation: Whatever code you have written needs to be compiled to check for errors and see everything is good to go. Java automatically generates a “.class” file as you compile it using the ‘javac‘ command. It is also known as bytecode.
  3. Execution: The compiled .class file needs to be then executed. You need a JVM (Java Virtual Machine) for that. Don’t worry you have it with you already when you downloaded the Java kit.

It is here where the concept of Java being a cross platform language comes into picture. You can take that compiled .class file or the bytecode and run it on any Operating System without any issues. Isn’t that beautiful?

Just mail the bytecode to your Hobbit friend in New Jersey who uses Mac OS as the Operating System, and ask him if the program executes there. Sounds good?

We are going to delve into a tad deeper understanding of all the aforesaid as we begin writing programs.

Java Demo Program

You want to say Hi to the world? Let’s get that out of the way first. Getting your system to talk or here print is I guess one of the best feelings in the world. It means the system understands you, and more importantly you understand it. You are in perfect sync with it.

Our prime motive would be to print a “Hello World” gesture to announce to the world you are here.

To do that let’s just split the work into steps. Steps make things so easy, I know! You get to rest once in a while, get up for a beer, and then come back to start from where you left off. Here are those steps to write your first java program.

So without wasting any more time let’s start by writing your first java demo program in notepad. We will learn what’s what as we proceed.

Steps to Write Your First Java Demo Program

Did you make a separate folder for this? You didn’t? Please do. The last thing you would want is learning hotchpotch in your head.

Step 1: Right click on a blank area to create a new text document. Then open the notepad file. Type in the following code:

Hello World Java program first Java demo program

Step 2: Once you are past that you need to move on to the next step of compilation. To do that you need to save the file in a format Java gets. Save the file as “.java” extension. I have saved it as Chimera.java. Gotta stay cool, huh?

Saving the Java file as Chimera

Step 3: Open Command Prompt. You can do that using Windows + R key and typing cmd.

Run Dialog Box

 

Using MS-DOS

Step 4: Now if you are new to MS-DOS you might have to learn how to navigate inside and outside the folder. Or the best way to avoid that would be to simply copy the path of the folder where your file is actually located. Then proceed to Command Prompt, and type

cd "path"

where path denotes what you copied. For my case it is “D:\Java”

NOTE: You need to be in the same drive however. It wouldn’t work if your command prompt reflects a different alphabet than your actual file location.

So what I am going to do is type

D:

first to navigate to the drive where my file is located and press Enter.

navigating to a different drive using command prompt

Then immediately type cd followed by my path. So my command would look like:

cd D:\Java

That means I am in the folder named Java where my file Chimera.java sits.

Compilation

Step 5: Next step is compilation. To do that use the following command.

javac "file.java"

where file is the name of your file. Here in my precarious case it is Chimera. Once done press Enter.

how to compile in java

When you press Enter you will notice nothing happens, (provided you haven’t made any mistakes). But when you will navigate back to your folder you will notice that another file with the same name yet different extension has been created out of thin air.

class file extension in Java after compilation

It has an extension of “.class”. Yes it’s that java bytecode that I had been ranting about in the earlier section. You can literally use it anywhere. You just need a JVM to run it. Hence Java’s platform independent.

Now since you already have that installed we will go for the final process of Execution.

Execution

Step 6: Type java followed by the name of your file. Here the command would look something like this:

java "filename"

where filename is the name of that compiled file you got. Then press Enter.

Commands to compile and execute in Java

The moment you do you will have a black screen shouting “Hello World” at you. There! Your job’s done. Your first Java demo program is over just like that.

Breaking Down the First Java Demo Program

Well apparently whatever you wrote down felt like a jargon you don’t understand. To label it down and help you understand I will make sure that I cover them all in the upcoming chapters. The only thing you need to remember with the hello world program is that the following command:

System.out.println();

basically, prints out or displays anything you write within its brackets. Always use a semicolon to end a line, or you will get a compile-time error message. Make sure the first letter is in Capital and other such minute details to avoid errors.

Then there is that line that says:

public static void main (String []args) {

For starters, I would like to say just mug the syntax up, coz that’s how you get to make stuff work. You have to use that in every code. We will see what’s what in detail as we proceed with our further learning.

However, it is worth noting that “main” is the cardinal executor of your program. At least one class should have a method called main. Every Java program starts with the main method and ends with it.

  • The first line of the main method will be the starting point of your program (will be executed first)
  • The last line of the main method will be the exit point of your program (will be executed last)

Main() is also the first thing that is called by your JVM so if you have got something to say to your computer always directly or indirectly place it in main.

Access Modifiers

The keyword public in the above example is nothing but an access modifier. Access modifiers in Java are used to regulate access visibility meaning whether a class or its data members can be accessed by other classes or not. We can use the relevant access modifier to establish that. There are four types of Access Modifiers namely:

  1. public
  2. protected
  3. default
  4. private

Here’s a table to make you understand the accessibility of these modifiers:

access control modifiers in java

Remember while working with inheritance access control follows certain rules.

For instance, if you have declared a method as public in superclass, it must be public for all methods in all its subclasses. They can’t be less restrictive. Meaning methods declared as protected in a superclass has to be declared either protected or public in a subclasses.

Also, for default you don’t need to manually type default against classes or data members. It is an implicit built-in feautre of Java. If you don’t mention anything it will consider it as default.

Remaining Part of the Code

The second part of our code for our java demo program says static. To learn what is static you need to check this static chapter out.

Void is simply a return type. Return types can either be primitive or non-primitive depending upon your requirement.

String[] args is a parameter passed for main method. String is a return type and args[] is an array that holds command line arguments as an array of String objects.

Meaning if you pass a command on the console as:

java Chimera one two three four

The args[] array is going to store one in args[0], two in args[1], three in args[2] and four in args[3].

And then, of course, never forget to follow the primal rule of programming:

What opens, always closes. What has closed must have been opened.

Holds true here as well. It is just that here we have braces ‘{‘ and ‘}’ to deal with.

Bottom line

You have successfully learnt how to program in Java. All there is to it is coding, compilation and execution. But it doesn’t end there. The story has just begun my dear friend. We will get there one step at a time.

Revere in the glory of your first successful java demo program run. Go grab a beer!

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

6 Responses

  1. bagavathy says:

    how do i go to the next post of this blog?:)

    • dumbitdude says:

      I have created a separate widget in the left sidebar. All Java-related tutorials are under one section. You can navigate chapter wise now. 🙂

      – Scottshak

  2. dumbitdude says:

    Open Java category in a separate page, and keep navigating to the next page till you reach the very first post 🙂

  1. March 26, 2017

    […] do you do that? If you remember correctly from Chapter 2,  we used to […]

  2. July 3, 2017

    […] execute it.  If you have forgotten how to do that on cmd you can always flip back to check out that first Java Demo program we did in the very […]

  3. September 8, 2019

    […] Chapter 2 – Java Demo Program […]

Leave a Reply