Java

Java Tutorials – An Introduction

Hello people…! This is the first post in Java Tutorials – An Introduction. This, is a whole new section of Theory of Programming, where we talk about one of the most widely used languages in industry, Java. I have quite a lot of experience programming in Java and developing small applications. So, I am combining my knowledge and experience to provide a series of posts which will be tutorials for various Java language topics.

Preface

As a prerequisite, I assume you already know at least one programming language such as C. Java cannot be explained in one post, it would be sheer madness..! So, there will be a several posts. There are a lot of sample programs which I have written to explain the topics. I have written my programs in NetBeans IDE, and I would suggest you the same. Believe me, writing Java in NetBeans is a luxury..!

For most of the posts there will be practise questions. I insist you to go through them carefully and try them for the best results. You can always comment if your have any doubts…! 🙂

What and Why ?

Java Logo
Java Logo

Java is a programming language developed by the Green Project Team of the Sun Microsystems, headed by James Gosling, in the late 1990’s. Perhaps the most prominent reason why Java became so successful is that it was the first programming language which was not chained to one operating system. Java is designed to be platform independent. Never before did any language posses such a remarkable feature. Other features of Java include –

  • Java Virtual Machine (JVM) – The JVM is what we need to run Java Programs, nothing else. So, if two systems have JVM installed, they both can run Java Programs and even share them irrespective of their native operating system. The JVM initially converts the programs to what is called the bytecode. This bytecode is then converted to the native machine code.
  • Object-Oriented – Java is an object-oriented language. The programs have data and code in the form of classes and objects. Java’s Object Oriented model is very easy to understand, that is why, many people prefer Java to be their starting step in Object Oriented Programming
  • Robust – Java was designed to be reliable, safe, small and simple, not exactly powerful. So, in comparison, Java will have lesser programming language features than the other object-oriented languages such as C++, or C#. But, the developers of Java didn’t want to design a language which would grant boons to programmers, but an error-free and reliable language so that the programmers need not pray every time they run code…! 😛

Java has always been one of the most-wanted languages by the industry. Tiobe measures the popularity of the programming languages. It is not at all surprising to see Java is always in the top 2..! So, let’s get started..! 😀

Hello Java…!

public class HelloJava {

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

}

This is the simplest Java program. It simply prints “Hello Java…!” on the terminal. Now, if you are a C programmer, almost every word must seem strange to you. That’s okay..! It should..! Everyone felt like that in the beginning. For now, digest whatever you don’t understand, and let’s look at the program from Line 1.

    public class HelloJava {

In this line, we are declaring what is called as a class. A class is a user defined data type, much like the structures in C. But unlike structures in C, we can write functions in class. The Java code, be it variables or functions, are always written inside a class. So, the class acts as a boundary for your program. But, the class is much more than it seems to be, we will get to it later. For now, remember that, whatever set of instructions we want to write to achieve an output, we do it inside a class.
This class is marked as public, which is an Access Modifier. Public means that it can be accessed from anywhere, you can think of it as marking the class global. And the name of the class is HelloJava. There are a couple of rules while writing a Java program –

  1. We can write multiple classes in one file. But, there can be at most one public class in a .java (Java programs are written in a file having .java extension) file. And if there is a public class, the name of the file must be exactly same as the name of the public class, otherwise, the file name can be arbitrary.
  2. We can have any number of non-public classes in one file.

These are some fundamentals. We won’t be writing more than one class in a .java file for quite a while, so you needn’t be too cautious about this. Now, let us look at the line where the main() was declared –

    public static void main(String[] args) {

This is the main() function. As we know, this is where the execution of a program starts. In C, the Operating System calls the main(), but here the Java Virtual Machine (JVM) calls the main(). Unlike in C, the main() function in Java cannot return anything, so its return type is void. It is marked public static for a reason, which we will discuss later. The main() function in Java must have an array of Strings as the parameter. Strings in Java, are not character arrays..! They are completely different..!  We will have a detailed discussion about them later. This is how an array of Strings are mentioned –

String[] args;

Note that the “[]” are given just after the data type. They can be given after the name also, but this is the right way to do it.
The name “args” might give you a hint as to what the array of Strings are meant for. They are meant to store the command line arguments, if any. The name “args” is just a tradition, it can be anything like, “JavaRocks”, or “Hacker”, or any name that is valid.
We can also write ellipses, “…”, instead of “[]”, but they must be just after the data type. It is the Java syntax for variable number of parameters. The last thing I want you to keep in the back of your mind is that the keywords static and public are interchangeable, but mandatory. So, putting all this together, we can say that, the following declarations of the main() function are perfectly valid –

public static void main(String[] args) {
    // code
}
static public void main(String[] args) {
    // code
}

static public void main(String args[]) {
    // code
}

public static void main(String[] MarkElliotZuckerberg) {
    // code
}

public static void main(String... NewYork) {
    // code
}

Where as, the following are not –

static void main(String[] args) {
    // public is missing
}

public void main(String args[]) {
    // static is missing
}

public static int main(String... Java) {
    // return type not void

    return 0;
}

public void Main(String args[]) {
    // "main" not "Main"
}

public void main(string args[]) {
    // "String" not "string"
}

public void main(String.. SayHi) {
    // Ellipses is 3 dots !
}

The last line to discuss is –

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

This, prints a line and takes the cursor to the next line. So, in C, it is equivalent to –

printf("Hello Java...!\n");

So, the println() method, implicitly brings the cursor to the next line. If you don’t want to come to the next line, you can use –

System.out.print("Hello Java...!");

So, these are the two variants of printing a line to the terminal. I know you still don’t understand what the “System.out.” thing is doing there. But trust me, we will de-mystify everything soon..! 😉 … So, you should be able to guess what Samy has to say without a sweat..!

public class FatherOfJava {

    public static void main(String... BeingHuman) {
        System.out.print("Samy : ");
        System.out.print("The Father of Java is ");
        System.out.println("James Gosling..!");
    }

}

Summary

  • This is how we write a “Hello World” program in Java –
    public class HelloWorld {
    
        public static void main(String[] args) {
            System.out.println("Hello World...!");
        }
    
    }
    
  • A class is a user-defined data type just like structures in C, but we can have functions too.
  • We can write any number of classes in a file, but there can be at most one public class.
  • If there is a public class, the .java file name should be exactly same as the name of the public class.
  • This is how we declare the main() function in Java –
    public static void main(String[] args) {
         // code
    }
    
  • The keywords static and public are mandatory but interchangeable.
  • The JVM calls the main() function.
  • The main() function in Java has a parameter which is an array of Strings, used for the command-line arguments.
  • This is how we print text on to the console –
    System.out.println("Printing with a new line.");
    System.out.print("Printing without a new line");
    

Practise

  1. I have given some examples regarding incorrect declarations of the main() function. Try running a sample Java program with those incorrect main() function declarations. Some give a compilation error, and some don’t..! Try to run those programs where it doesn’t give a compilation error. Try to guess why things ended up in the way they did..! 😉

This post was to introduce Java to you. I hope I did not scare you..! 😛 … Feel free to comment your doubts..! We will discuss more in the next post…! Keep practising…! Happy Coding..! 😀

8 thoughts on “Java Tutorials – An Introduction

  1. I am following your tutorials its very helpful, can you please post a topic on entire OOPs concepts.

  2. vamsi i m actually an ece student i have to learn java by complete basic and programming to how to install java and all i m nt able to start anything could please help me in this sort

    1. Hello, Harita..! 🙂 …. Everybody has the starting problem 😉 … First to get Java installed in your PC, download Java from this link. Accept the license agreement under “Java SE Development Kit 8u65” and download the file as per your operating system. Once downloaded, install it, you just have to keep pressing Next. Then download NetBeans IDE from this link. I recommend you to download the “All” bundle. Then the installation is again simple. If you want you can refer to this video if you are stuck with installation. Now to learn the actual java language, I recommend you follow the book, Programming with Java A Primer, by E Balagurusamy…. Start with the basics and make sure you redo the each of the examples and exercises in your PC, you’ll learn Java very quickly 🙂 All the best 😀

Leave a Reply