Fanatah Java Academy

Fanatah Java Academy

Share

LEARN JAVA FROM SCRATCH.! No matter your background or age, it is not too late to learn computer programming.

Photos from Fanatah Java Academy's post 08/07/2020

JAVA BEGINNER

INTRODUCTION TO JAVA.

LESSON 1 : CHAPTER 2

By Fanatah

HOW TO RUN YOUR FIRST JAVA PROGRAM -

BY FANATAH | UNDER: LEARN JAVA FROM SCRATCH -BEGINNER A.

In this tutorial, we will learn how to write, compile and run a java program. We will also learn about java syntax, code conventions and several ways to run a java program.

Simple Java Program:

public class Welcome {

public static void main(String [ ] args) {

System.out.println("Hello.");

System.out.println("Welcome to programming in java.");

}//End of main

}//End of FirstJavaProgram Class

Output: Hallo.
Welcome to programming in java.

How to compile and run the above program

NB. Required: You need to have java installed on your machine. You can download get the java jdk 1.8 or laterfreely online. Or Netbeans version 8 or later.

There are 2 main methods of running a Java Program:
1. Using command prompt(cmd).
2. Opening and running on Netbeans IDE or Eclipse.

1. CMD Way:

Step 1: Open a text editor, like Notepad on your Windows Machine.
Copy the above program and paste it in the text editor.

Step 2: Save the file as Welcome.java. You may be wondering why we have named the file as Welcome.?
This is because of the rule that you should always make the file-name the same as the public class name. In our program, the public class name is Welcome, and that’s why our file name should be saved as Welcome.java.

Step 3: In this step, we will compile the program. For this, open command prompt or just type the words (cmd) on your start menu of you Windows-machine.

Before you compile your program create a folder in C/ drive manually by right clicking then selecting new folder and save it as MyJavaExamples and make sure that is where you will be saving all your java programs when u are in notepad.
Now compile your program, by typing the following commands and hit enter:

cd\

cd MyJavaExamples

javac Welcome.java

You may get this error when you try to compile the program: “javac’ is not recognized as an internal or external command, operable program or batch file“. This error occurs when the java path is not set in your system

If you get this error then you first need to set the path before compilation.

Set Path in Windows:
Open command prompt (cmd), go to the place where you have installed java on your system and locate the bin directory, copy the complete path and write it in the command like this.

set path=C:\Program Files\Java\jdk1.8.0_121\bin

Note: Your jdk version may be different. Since I have java version 1.8.0_121 installed on my system.

That’s it.

The steps above are for setting up the path temporary which means when you close the command prompt or terminal, the path settings will be lost and you will have to set the path again next time you use it. I will share the permanent path setup guide in the later tutorial.

Step 4: After compilation the .java file gets translated into the .class file(byte code). Now we can run the program. To run the program, type the following command and hit enter:

java Welcome

Note that you should not append the .java extension to the file name while running the program.

Closer look to the First Java Program

Now that we have understood how to run a java program, let us have a closer look at the program we have written above and try to break it down.

public class Welcome {

This is the first line of our java program. Every java application must have at least one class definition that consists of class keyword followed by class name. When we say keyword, we mean a word that should not be changed, we should use it as it is.
However the class name can be anything you choose to name it.

I have made the class public by using public access modifier, I will cover access modifiers in a separate tutorial, all you need to know for now is that a java file can have any number of classes but it can only have one public class and the file name you save should be name of the public class.

public static void main(String[ ] args) {

This is our next line in the program, lets break it down to understand it:

public:
This makes the main method public that means that we can call the method from outside the class.

static:
We do not need to create object for static methods to run. They can run itself.

void:
It does not return anything.

main:
It is the method name. This is the entry point method from which the JVM runs your program.

(String[] args):
Used for command line arguments that are passed as Strings. We will cover that in a separate lesson.

System.out.println("Hallo. Welcome to Java Programming.");

This method prints the contents inside the double quotes into the console (screen/monitor) and inserts a newline after.

Photos from Fanatah Java Academy's post 08/07/2020

GENERAL ELEMENTS OF A JAVA PROGRAM.

by Fanatah -

When we consider a Java program, it can be defined as a collection of objects that
communicate via invoking each other's methods. Let us now briefly look into what do class,
object, methods, and instance variables mean:

1.Object - Objects have states and behaviors. Example: A dog has states - color,
name, breed as well as behavior such as wagging their tail, barking, eating. An
object is an instance of a class.

2. Class - A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type supports.

3. Methods - A method is basically a behavior. A class can contain many methods.
It is in methods where the logics are written, data is manipulated and all the
actions are executed.

4. Instance Variables - Each object has its unique set of instance variables. An
object's state is created by the values assigned to these instance variables.

Example of a First Java Program:

Let us look at a simple code that will print the words Hello World.

public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}

08/07/2020

Java uses the 'this' keyword which in other languages such as C & C++ works as a pointer but in Java it is just a keyword that refers to the current object..!!

Want your business to be the top-listed Computer & Electronics Service in Harare?
Click here to claim your Sponsored Listing.

Telephone

Address


102 Twentydales, Hatfield
Harare

Opening Hours

Monday 09:00 - 17:00
Tuesday 09:00 - 17:00
Wednesday 09:00 - 17:00
Thursday 09:00 - 17:00
Friday 09:00 - 17:00
Saturday 09:00 - 13:00