Skip to content

Exploring the basics of Java programming language

Exploring the basics of Java programming language
Bookmark

No account yet? Register

Java programming language allows programmers to write computer instructions using English-based commands. The language is known as a high-level language because it is readable by humans first, then get translated later into numeric codes for machine understanding.

The designing of the first version of Java started in 1991 by James Gosling and his team at Sun Microsystems –  a company which later on get acquired by Oracle. Gosling and his team created the technology for programming home appliances that would be controlled by a variety of computer processors. Then, in 1994, Java got connected to the Internet after Gosling realized that the language would be ideal for use in web development, mobile devices and so on. From then onward, as they say, the rest is history, Java has gone up to become one of the leading programming languages in the world.

As one of the most popular programming languages in the world, Java has become a force to be reckoned with. Whether you want to be a back-end-web developer, a desktop developer, mobile applications developer, you name it, Java is undoubtedly a language you should experiment with. Therefore, because of its importance in programming ecosystem today, I would like to explore its core concepts that an aspiring Java programmer should know.

So, as a beginner programmer, how do you start? Just learn anything that comes your way, or is there a particular way to approach the journey? Well, in almost everything you do in this world, there may be a better way to approach it if you pay attention and are willing to put in the effort to learn and unlearn. Going by this, I can say, Java is not an exception.

To be a good java programmer, you should, first of all, know that you need more than just java programming language. There are a lot of other tools that you need to master or at least be familiar with to be able to have programs under your belt.

Also, you can read more about the tools you need other than the Java programming language in order to become a java developer.

Now that we have known the background of Java and how important it is, let us see the best way one could learn the language. According to so many experts in this field, it’s very important to learn and understand what the core Java concepts are, why and how they are being used and when to implement them in a program before actually jumping into other things. Then after this, practice as much as possible – that’s the only way one can master Java or anything else for that matter. Let’s see the core java concepts, shall we?

The core java concepts

Programming languages are categorized based on many approaches; procedural and object-oriented programming (OOP) are some of the most prominent of them. Java is a language that follows object-orientated programming, otherwise known as OOPs. In Oops approach, programs are seen or viewed as a collection of objects rather than procedures.

The introductory part of Java

Before diving into OOPs, first thing first, the moments you can write and print your first line of code, usually “Hello world”, next is to understand how variables work. Then, you must understand the relationship between the variables and the data types, the identifiers, the keywords, the literals and so on. Usually, these concepts are the introductory part of any programming language.

READ ALSO:  Data structure and algorithm: A brief explanation of binary search and arrays

Other important java concepts to learn includes data structure and algorithms like arrays, ArrayList, LinkedList, Maps, and so on. To know more about them, check this.

How to print the first code: The “Hello world.”

public class Example{


    public static void main(String[] args){
        //to print your first line of code
        System.out.println("Hello World!");
    }
}

The output is: Hello world

The variables and the data types

As mentioned above, variables are one of the basic things that one should know as one gets introduced to Java. They are just like containers that hold data or information. Those containers are set based on the data type (kind of information) a programmer intends to put in there. For example, a variable that will store a bunch of text goes with ‘string’ data type. But for a whole number, you need ‘integer’ data type. Numbers that have decimal points go with ‘double’ data type. Likewise, a variable that can only take true and false statements uses ‘Boolean’ data type. Those are just a few of the data types that we have in Java. You can read more about them here.

Below is an example of how variables and data types are used.

public class Example{


    public static void main(String[] args){
        //variables and data types
        String name = "Ali";
        int age = 9;
        double height = 4.7;
        boolean isGood = true;
    }
}

In the above, we have seen how variables (name, age, height and isGood) and data types (string, int, double and boolean) are being used in real code.

Class

The main idea behind OOPS is the concept of class. A class is like a blueprint of program functionality. The information found and the method that will be used in a program are all defined in a class. Remember, a class is not the actual program – it’s just a blueprint where programs are being defined. The program will be implemented using what is called an object. For example, we can have just a single blueprint (class) for employees so that we can define the attributes and behaviours of an average employee in a company; then subsequently, every employee will have his/her object with different values or information that are unique to this employee in question.   

Check out the example below:

public class Employees{
    
    //variables in the class Employees
    String name;
    int id;
    int salary;
    
    public Employees(){
        
    }
}

In the above example, we created a class called employees with some fields or variables that every employee will have, but every employee is going to have his or her unique name, ID and perhaps different salary. To represent their uniqueness, an object must be created. Talking about an object, that brings us to our next concept in OOPs.

Object

As discussed above, an object is directly related to a class. You cannot have an object without a class because an object is an instance of a class. Going by our previous example in the class section, we mentioned that we have a class called employees as a blueprint that was set up for employees of a company. Now, in order to use this blueprint, you must create what we called an object. The number of objects could be as many as you need, let’s say in that company, you have several employees and each of them has a portfolio, example, there is a manager, a supervisor, a cashier and so on. So, here, you can have an object for each of them using the class blueprint that we created. Though they are all employees who have similar properties, there’s specific role each plays independently of another.

READ ALSO:  Data structure and algorithm: An introduction to ArrayList and LinkedList

Here is an example below:

public static void main(String[] args) {

    // cashier object is created from Employees class
    Employees cashier = new Employees();
    cashier.name = "Ali";
    cashier.id = 123445;
    cashier.salary = 50;

    System.out.println(cashier.name + "'s salary is " + cashier.salary * 30);

    // manager object is created from Employees class
    Employees manager = new Employees();
    manager.name = "Aisha";
    manager.id = 23874;
    manager.salary = 70;

    System.out.println(manager.name + "'s salary is " + manager. Salary * 30);


}  

Output1: Ali’s salary is 1500

Output2: Aisha’s salary is 2100

Above, we created two objects from the employees’ class. Although both used the same properties from the employees’ class, they all have unique values that are independent of the others.

Encapsulation

Encapsulation is a technique used in OOP programming languages to bind object methods and variables together in order to make programs simpler and more organized. By doing this, data such as variables, methods or objects can be hidden from other classes so that the only way to access them is through a special method in their class called, method getters and setters. In essence, encapsulation is used in order to prevent easy access to data. To achieve this, data access modifiers are set as private in their class. Check out for more information about encapsulation in Java.

See example below:

public class Example{

    //All these variables are only accessible in this class
    private  String name;
    private  int id;
    private int salary;

//getters and setters methods to access salary

    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary){
        this.salary = salary;
    }

In the above case, we have three variables that are set as private (name, id and salary). Because they are set as private, these variables cannot be accessed by any class other than their present class. To access the “salary” variable, we created special methods called, “getSalary” and “setSalary”. This way we can access “salary” variable. In order to access the remaining variables; therefore, we can as well follow the same procedure as in the case of the “salary”.

Inheritance

Inheritance, also, is a term used in OOPs that does literarily what the word “inheritance” means. It allows a class to inherit another class together with its fields. When a class inherits another class, it will get all the class’s methods and variables. In inheritance, there is what is called parent-child relationship or an is-A relationship where the main class act like a parent and any subsequent class that inherit it, will be a child class.

The parent class can also be called main class while the child class is also called sub class. When inheriting a class, a child class must use the Java keyword “extends” before the class name.

Very important to note: Inheritance reduces redundancy in code, thereby making code lesser, cleaner and easier to comprehend.

Example of inheritance below:


//parent class
public class Employees{
    String name;
    int age;
    public void Method(){

    }
    //child class to inherits Employee class

    class  Manager extends Employees{

    }
}

In the above instance, we have two classes; one parent, “Employees” and the other a child, “Manager”. The child inherited all the fields of its parent once “extends” keyword is used

Abstraction

This is a technique in OOPs whereby a class is created with an abstract method. Abstract methods are methods that have no implementation in them; they are empty methods that can not be called in another class or classes but can only be inherited from parent class. When you have an abstract method in a class, that class must be declared abstract using the java keyword “abstract” as well.

When a class is declared abstract, it means that the class cannot be instantiated. That is, an object cannot be created from the class because an abstract class is not a complete class in its self. Therefore, to use the abstract class, other class (s) must “extend” it, so that the abstract method can be overridden, and implementation will be applied to this new method (the overridden method).

READ ALSO:  Graphical User Interface: An overview of Java Swing interface

The main reasons why we have abstraction is to hide away unnecessary details. Only relevant details or mechanisms will be disclosed to users. Example, inside a car, a driver can only see the steering wheel, the pedals and few other things to be able to drive, but the internal complexities that allow the car to start and work are hidden away from the exterior. For details on this, check out this video.

Check out abstraction example below:

public abstract class Employees{

    String name;

    public abstract  void  abstractMethod();


    //a class that extends Employees class.
    class Manager extends Employees{

        @Override //this overrides the method in the abstract class.
        public void abstractMethod(){


            //below implements the method
            System.out.println("");
        }
    }
}

Polymorphism

Polymorphism is a word that is derived from two words; poly, which means ‘many’, and morph, which means ‘form’. In OOPs, the term refers to the ability of a single name to be used in several methods, either in the same class or in the inherited class. We have two main types of polymorphisms in java; method overriding, otherwise known as runtime polymorphism and method overloading, also known as compile time polymorphism.

Method overriding allows you to have the same name in the methods in main and subs classes but with different implementations. Which method gets called, depends upon the object type and that is determined during runtime. Here, the name, data type and parameters are the same.

While on the other hand, method overloading has the same name but different data type or parameters.

Below is an example of polymorphism: Method overriding:

public class Employees{

    public void MethodOverriding(){ //method in the parent class

        System.out.println("implementation in the parent class");

    }

    class Manager extends Employees{

        public void MethodOverriding() { //same name in the child class
            System.out.println("implementation in the child class");
        }
    }

    class Cashier extends  Employees{
        public void MethodOverriding() {//another method with same name
            System.out.println("implementation in another child's class");
        }
    }

Example of method overloading below:

public class Employees{


    //method without parameter
    public void MethodOverloading(){

        System.out.println("anything here");

    }
    //same name but has a parameter
    public void MethodOverloading(int a){

        System.out.println("integer a = "+ a);

    }
    //same name but has 2 parameters
    public void MethodOverloading(double b, int c) {

        System.out.println("double b is = " + b + "integer c is = " + c);

    }
}

In the examples above, we have seen how method overriding and method overloading are being used in practice. In the overriding case, we have three methods called “methodOverriding” in three different classes; one parent and two inherited. All of them will work based upon the object that is called.

While on the other hand, in the case of method overloading, we have three methods called “methodOveloading” in a single class. The one to be implemented depends upon the number of parameters involved.

To conclude

That’s all about the core Java concepts that every Java programmer should be comfortable with. Hopefully, any beginner programmer or a person that wants to get into the world of programming especially java programming will find this article as a sort of guide that will help them through their journey.

And lastly, if you find any important java concept that you feel like should have been included here, please let me know in the comment section below.

Rahmatu Lawan

Rahmatu Lawan

A wife and a mother who is always striving to improve.I am always excited to connect with new people and learn from each other.View Author posts

Drop a Comment

Your email address will not be published. Required fields are marked *

×