Introduction to Java

Introduction to Java

The programming language that continues to stand out...

ยท

4 min read

Brief History

Java is a versatile and widely-used popular programming language. Java is known for being simple, portable, secure, and robust. It was first released in 1995 by Sun Microsystems and continues to be relevant and in demand for developing a wide range of applications, including desktop applications, web applications, mobile apps, enterprise applications, big data, IoT, etc.

Some of the key factors that contribute to Java's ongoing popularity include:

  1. Platform independence: Java code can run on any device that has a Java Virtual Machine installed, making it a highly portable language.

  2. Object-oriented programming: Java uses object-oriented programming concepts, allowing developers to organize code into reusable objects and modules.

  3. Large community: Java has a large and active developer community, providing easy access to help and resources.

  4. Extensive libraries: Java boasts a rich and diverse ecosystem of libraries and frameworks, making it suitable for developing applications in a wide range of domains.

  5. Enterprise-grade performance: Java is fast, secure, and reliable, making it ideal for building large-scale enterprise applications.

Overall, Java continues to stand out as a versatile, highly-capable and widely-used programming language.

Let's now jump into the basics of Java with an example program.

Basics of Java

Java or in general, programming languages are composed of a set of rules, symbols, and specific syntax that are used to write computer programs. We write syntax in files, that are known as source files, to create programs. They are essentially a way for humans to communicate with computers and specify what they want the computer to do.

Let's understand the naming convention of a source file in Java.

Java file names are written with .java extension. Programs can be one file or hundreds of files. In Java, we have to name the source file with the same name as the public class out of all the other classes that the source file contains. It is important to note that a single Java source file can contain multiple classes, but only one of these classes can be declared public. The name of the public class should match the name of the source file.

We will learn about classes more in future, as of now we are considering it an essential part of writing Java programs.

public class Greet {
    public static void main(String[] args){
        System.out.println("Hello, Coders!");
    }
}

For example, here we have a public class named "Greet", the source code for that class should be in a file named "Greet.java". The Java compiler will compile the code in the source file into a class file with the same name, "Greet.class".

A .class file in Java contains bytecode which is a machine-independent binary format. This file can be executed by the Java Virtual Machine (JVM). JVM is a virtual machine responsible for executing the bytecode and providing a runtime environment for the program. It also provides services such as memory management, garbage collection, and security.

Syntax

public class Greet {

}

In the example above, we are defining a class named Greet. We use curly braces to mark the beginning and the end of a class. Syntax inside the curly braces is part of the class.

Each file has one primary class named after the file. In our case, the class name is Greet and the file name is Greet. The words should be capitalized.

public class Greet {
    public static void main(String[] args){

    }
}

Inside the class, we have a main() method. The method main() in Java is the entry point of a Java program. It is a method that is called when the program is executed. Here also, we use curly braces to mark the beginning and end of a method.

We will learn about public, static, and void syntax in future.

Here, String[] args is a placeholder for information we want to pass to the program or as an argument to the main() method. This syntax is necessary for the program to run.

Print statement

public class Greet {
    public static void main(String[] args){
        System.out.println("Hello, Coders!");
    }
}

The program also has another line, "System.out.println("Hello, Coders!");", which is called a print statement, is responsible for displaying the output. In our case, the output is "Hello, Coders!". Print statements display the output to the output terminal. In this line of code, System is a built-in Java class, out stands for "output" and println stands for "print line".

public class Greet {
    public static void main(String[] args){
        System.out.print("Hello, Coders!");
        System.out.print("Let's code in Java.");
    }
}

Like the example above, we also can use System.out.print() to display some value on the screen. But there's a little difference between both the print statements. Whenever we want the program to create a new line on the screen after it prints some value, we use println() method. Where, print() outputs everything on the same line and doesn't create a new line.

We learnt some very basic concepts of Java. We will explore much more in the next articles.

Thank you for taking the time to read ๐Ÿค—.