java-programming

Java is one of the most popular and widely used programming languages in the world. Lets begin with a simple program before getting into any details:

Open your IDE or editor and type the following code:

public class FirstProgram {
    public static void main(String[] args) {
        System.out.println("First Java Program");
    }
}

Save this file as FirstProgram.java. All Java files are required to have the same names as their classes. Java is case-sensitive.

  • Class name is FirstProgram
  • Java functions are called methods</li>
  • public means that the class or method can be accessed by any Java class
  • static means that you don’t need to instantiate (create object) the class to use the method
  • void means the method doesn’t return anything
  • String[] args means a string array called args holds all parameters passed from command line
  • System.out.println() method print the string First Java Program to screen

Programs starts execution at the main() method. A class can have at most one main() method. A class is not required to have a main() method and it can be called from the main() method of another class.

Strictly typed Language

Java is a strictly type language. This means that you have to declare the type of every variable you create. Languages like Perl are able to deduce the type of a variable based on how it is used. There are benefits and disadvantages to being strictly typed. Just know that you have to specify the type of every variable or object you create.

Fully Object-Oriented Language

Java is a fully object-oriented language as opposed to functional or scripting language. What does that mean? Let’s briefly discuss object-oriented programming to help you understand this.

The main design goals of object-oriented design are:

  • robustness
  • adaptability
  • reusability

A robust software is capable of handling unexpected input. For example, anything divided by 0 is undefined. When an unexpected 0 is encountered, the software should be able to handle the problem without resulting in broken behavior.

An adaptable software is able to continue functioning with changing technologies. Portability is a subset of adaptability, referring to the ability of the software to work across different software platforms and hardware.

Reusability refers to the ability of the software or its components to be reused in other software systems.

Object-oriented design goals are difficult to achieve without the following design principles:

  • Abstraction
  • Encapsulation
  • Modularity

Abstraction involves simplifying a complex system down to its fundamental concepts and components i.e. identifying components, naming them, and describing their functionality. In object-oriented programming languages, abstraction is accomplished with classes.

Encapsulation means hiding the details for the code from other pieces of code using it. For example, a class calculates a numerical value used to move projectile in a game. This class will only interface its input and output with other classes using it. This allows a programmer to change the code within the class without impacting other classes using it as long as the input and output parameters remain unchanged. If the code is not encapsulated, other classes might use a function in ways it was not intended or be impacted when that code is modified.

Object-oriented Principles facilitate achievement of object-oriented design goals. Design patterns are canned solutions to software design problems. Design problems and solutions are not programming language specific. They are design specific. Design patterns can be implemented in any object-oriented language. You will learn about design patterns when you reach an advanced stage.

Object-Oriented code in Java

Every program has at least one class. Classes are instantiated to create objects.

To provide a more tangible analogy, think of an automobile assembly line. Cars are created in factories. A group of engineers and others designed every detail of the car i.e. created a blueprint for the car. All cars are produced to the exact specifications with the possibility of customizations such as color, leather interior, sound system, sunroof, etc. The factory is the Java compiler. The blueprint is the class. Each individual car coming off the assembly line is an object. A class can be used to create infinite number of objects. Depending on the code of the class, objects can have different properties such as the color of the car. The act of creating an object is called instantiation.

A class is declared with the keyword class.

class MyClass {
    // some code
}

Objects are instantiated with the new keyword.

MyClass mc = new MyClass();

Time for an example. We will be creating a calculator:

public class Calc {
    int operand1;
    int operand2;
    int result;

    Calc() {
        this.operand1 = 1;
        this.operand2 = 2;
        this.result = 0;
    }

    void calculate() {
        this.result = this.operand1 + this.operand2;
    }

    int getResult() {
        return this.result;
    }
}

Save this as Calc.java. The name of the file must be the same as the name of the class.

public class testCalc {
    public static void main(String[] args) {
        Calc c = new Calc();
        c.calculate();       
        System.out.println(c.getResult());
    }
}

Save this as testCalc.java. Compile and run both. The output should be the number 3.

Following articles different topics in Java.

By master