;
Java Tutorial For Beginners – Java Programming Made Easy!

Related Courses
Java Lessons for Beginners!

Java concepts are a must for all programmers. Usually, we learn C, C++ for starting, And the next language we learn is Java. In this article, we are going to put in front of you some of the fundamental concepts of Java. And before we start, we need to learn what it is, what are the features of Java and how we can install it. In this blog we are going to cover the installation process of Java, how we write the Hello World program, what member variables are, what are data types and operators, and what are control statements. Then we will go through the classes and objects, and the structure of a program. Finally, we will look into Input and Output handling of the file in Java, and the arrays in Java. And we will finish off the article discussing the OOPS concept and Exception Handling. So, let’s begin the article. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training.

Installation of Java

You can download Java from here. However, make sure you have an Oracle Account, and you will require to accept the terms and conditions. You need to download the developer version from here. Download Java's latest version. However, remember that JRE has been deprecated from Java 10 onward. Hence now you need not include it in the URL after JDK plus there is no JRE now. This will show when you will be installing the Netbean. Before, when you used to download, both the JDK and the JRE used to be present in the zipped file. However, now you will find only JDK. Download hence and follow all the instructions, and go on clicking ok. Once you install Java successfully, you need to update the Environment variable. You will witness here as well. If you are downloading a version post-Java 10, you need to add only the jdk bin URL in the environment variable path section. Or else you need to add till bin the URL for JRE as well.

Remember we are installing on Windows 10. Go to Start and search for System. And then search for the Advanced System Settings. Now click on Environment variable, and double click on Path. Now go to C drive, and under Program files find Java, and then inside that bin. Now click on the arrow in the Address bar, and copy the URL from there. In case, you have the Java version less than 10, you need to repeat this process for the JRE as well. Now add both of these URLs to the path, which you get after double-clicking on Path in the Environment Variable. Click ok and you are done. Now open the CMD, and enter Java -version. The Java version you downloaded will be shown. Congratulations, you have successfully installed Java on your Windows 10 machine.

Now you need to install the NetBeans or the Eclipse IDE. We are mentioning both so that you can install both of them or anyone. Download NetBeans full version from the internet, and also the Eclipse. Follow the installation process, as it shows. The JDK is required for both, which we have already installed. And the IDE will pick the JDK URL automatically. On successful installation, you need to add the server. And we will show later how to add the Apache Tomcat 9.0 server to the Netbeans. Remember, the web application will run on this Apache Tomcat 9.0 server. Let’s now see how we can curate a Hello world program, and that is our next point of discussion. 

However, before that, let's have a look at some of the salient features of Java. The salient features are as below:

Open Source

Java has always been an open-source product, and all have public access. As a programmer, you can post the entire source code, and it's downloadable by anyone. No license is required, and you get a GPL license or the General public License that we get through the open-source software.

High Performance

It’s an interpreted language, and hence it is not as fast as C or C++. However, it enables the best performance through the just-in-time compiler.

Multi-threading

Through Java, you can perform several tasks simultaneously as it is multithreaded. The advantage is you make use of the same memory and various other resources for executing numerous threads at one time, such as during typing, grammatical errors get checked.

Secure

Java is the best in security. Through its security features, we can develop virus and temper-free systems. And it always run-in runtime environment that has no interaction with the system OS, and therefore secure.

Platform independent

The C and C++ are compiled in platform-specific machines. Whereas Java is writing once, run anywhere programming setup. Java gets compiled into bytecode. And this is platform-independent, and you can run it on any of the machines. It also ensures security. And if the JRE is present on a machine, it can run the Java program.

Portability

The Java code is quite portable, and highly in fact. You can write the code in the windows environment and execute it in the Linux environment.

Object-Oriented

You will find all Java is an object which has data and behavior. And it can be easily extended as it is Object model-based.

Robust

Java to remove error-prone code put weights on runtime and compile-time checking. However, the main improvement in Java is in mishandled exception and memory management through the inclusion of exception handling and the Garbage collector.

Let’s now write the first Java program, which is the Hello world program

Hello World Program

Our first Java program is as below. We have declared a class called HelloWorld and printed the “Hello World” message inside it. The code is as below:

public class HelloWorld { 

      public static void main(String[] args){ 

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

      } 

}

Member variables in Java
Member Variable

It is used to store the data value. There are three ways of declaring the data variable in a class. they are Local variables, instance, and Class/static variables.

Local: It’s the variable declared within a class method shown as below:

public class Student {

      public void PrintStudent(string name){  

           string name1=name;  // This is a local variable

           System.out.println("Student Name is:" +name1);

     }

}

Above we have a local variable name1, declared with the method PrintStudent with parameter “name.”

The Instance variable

public class Student {

      public String name;     // instance variable

      Student(String n){

            name=n;

      }

      public void PrintStudentName() {  // Method 

            System.out.println("The name of the student is:"+name);

      }

public static void main (String args[]){

           Student obj=new Student("NareshIt");

                  obj.PrintStudentName();

      }

}

Above “name” is the instance variable and it gets the value “NareshIt in the main function.

Class Variable or the Static Variable

These variables have one copy stored by all objects in a class. Let’s see the below code.

public class Student {

      public static int HeadBoy;   // This is class or static variable

      public static void main(String args[]){

           HeadBoy="NareshIT";

           System.out.println("HeadBoy is"+HeadBoy);

      }

}

Above “HeadBoy” Is the class variable or the static variable.  And its value remains the same everywhere in the class.

Data types and operators

We make use of the Data type to store various values in a variable. The main data types are Integer, character, float, and Boolean. Various data types under the Integer is the byte, Long, Short, and Int. Various under Float are float and double. Various character types are char, and various Boolean types are Bool. We also have the string data type. And we have used that in the code that we have written till now in this blog post on various instances. 

Now let's have a look at the Operators.

We have mainly four types of operators which are respectively Arithmetic Operators, Unary operators, Logical operators, and relational operators. Let’s discuss each of them one by one.

Arithmetic Operator: The four arithmetic operators are addition, subtract, multiply, divide, and modulus.

Unary Operators: The unary operators are like increment and decrement operators. ++ is the increment and – is the decrement.

Relational Operator: It sets up the relation between the two entities. Like <,>,>=,<=,!=,==

Logical Operator: They are applied to Boolean values.

Before understanding anything else, let's first discuss the oops concept. And then we will continue with the basics of JAVA.

OOPS concepts like Inheritance, Polymorphism, Encapsulation, and Abstraction

Inheritance

We already know what this is. However, let's repeat that it’s the process where one class inherits the other class properties. There are two classes, the child class, and the parent class. It is the child class that inherits the properties of the parent class. it is also the derived class or we also know it as the subclass. And the class from which the child class inherits the properties is the parent class. And we also know it as the base class.

Let’s have one example to understand it more elaborately.

Suppose, there are “Class 9” and “Class 9 passed Students” and “Class 9 failed students”, which are the respective classes. The two of the last classes are the child class. And the first class is the parent class or the base class. You can clearly understand that both passed and failed students of class 9 will have the same list of subjects. Hence, it's not a must that the child class inherits everything from the base class. We do have the scope specifiers which explain what is inherited and what is not.

The inheritance puts aside the code redundancy. There is a long list of inheritance types, and we will discuss that deep in the forthcoming blog post, please be assured. In that post, we will cover Object-oriented programming elaborately.

Encapsulation

We have the data and the code wrapped In one unit and that is the Encapsulation. Or you can experience that methods and variables are bound together in one class. And that is what we know as encapsulation. We can use the class variables only through the class method, and that ensures security. These variables are hidden from other classes but can be inherited through inheritance.

Polymorphism

Through this, we can have multiple forms of the variables, functions, and objects. And the most appropriate example of polymorphism is when various child classes implement the parent class differently. Also, it is implemented while we do the function overloading. The entire concept we will cover in a forthcoming blog, so please do not worry about these for now. One more example, however, is the Teacher asks the students to draw a shape. And the students draw the circles, and some draw the rectangle. This is what we know as Polymorphism.

Let's, however, brief the function overloading here. Suppose we have the three definitions of the same function as below:

Void calculatearea(int a, int b);

Void calculatearea(int radius);

Void calculatearea(float side);

Based on the parameters passed the above same method will calculate the area of the rectangle, circle, or Square. And this is polymorphism. If you still didn’t get it please do not worry. We will have an elaborate post on OOPS soon.

Abstraction:

When talk about calculating an area, we can sometimes use the function, pass the parameters, and get the results. We are only concerned with the results and don’t care about the code and the calculation. Like Data Scientists use the Python methods and they don’t care about the code involved, whereas the data engineer deals with the code. And this is what we know as an abstraction. It makes the task simpler for the layman and puts the burden of coding and the calculation of one other person like above we have the data engineers. 

And that completes the OOPS part for now. If you are finding difficulty in understanding the above, then you need not worry as we will soon cover the OOPS in a separate blog and detail.

Exception Handling

An exception is some issue that is not expected and arises at runtime. It hinders the sequential and normal flow of the program. Hence, we need to resolve it for avoiding the problems. Some of the exceptions are as below.

  • Invalid format for function calling parameter missing
  • Arithmetic out of bound exception
  • Arithmetic divide by zero exception.
  • And so on.

Let’s have a look at one through below code:

public class Student {

    public static void main(String args[]) {

          try {

                 float marks[] = new marks[20];

                 System.out.println("Printing 21st element:" + marks[21]);

          } 

          catch (ArrayIndexOutOfBoundsException e) {

                 System.out.println("The Error encountered is :" + e);

          }

          System.out.println("Out of bound");

    }

}

The output will be “Out of bound” 

Now let’s hit back at the Java Core, and discuss the Control statements.