Search This Blog

Learning Java

Hi peeps! How are you all doing? It's been almost five months since I wrote an article. I had been really busy with other things and was also a bit off. I started learning Java since the last month for which I read Head First Java book. It's a brilliant and engaging book. I would recommend you people to read it too to have a wonderful start. As a wise man once said that knowledge grows when it is shared, I decided to summarise what I learned and do a series of blog posts on Java. I'll try to continue to share what I learnt so far as clearly as possible. These series of articles will also help me when I want to revise. The articles will be more inclined towards practical stuff and don't assume that reading only my articles will be enough to be an expert in Java. What you have to do beside reading my articles is to read a standard book and practice coding in Java. If blogs and YouTube videos could teach a programming language as vividly as a book can then people would have stopped buying books and the authors would have been on a long vacation. Read books, it's important. My articles are intended to introduce you to the concepts in a practical approach and to make you hungry. If you're a newbie to programming and don't actually know what all this is about then you must read this article first: How to dive into programming?

Stay hungry. Stay foolish.

Learning Java is really fun. It is a general-purpose Object Oriented programming (OOP) language. This fast, robust and secure language has been developed by James Gosling at Sun Microsystems and released in 1995. In 2010, Oracle completely acquired Sun Microsystems along with Java. Oracle states that Java runs on 15 billion devices and there are 10 million Java developers worldwide. It is the most popular programming language till now. You can get an Android app or a good-looking desktop application ready with Java. You might remember that before smartphones boomed the market we played games that were written in Java and came in .jar (Java ARchive) package file format on keypad mobile phones. Those phones supported a Java-based application platform for mobile and embedded devices known as Java 'Micro Edition' (ME) developed by Sun Microsystems. Now, when Android, the mobile operating system, hit the market it became an alternative to Java ME to develop mobile apps for Android smartphones.

Java ME is one of the three flavors that Java comes in. It helps you to write programs for embedded systems like sensors, microcontrollers, TV set-top boxes, printers, and smart cards like ATM and SIM cards (using Java Card technology). The other two are: Java 'Standard Edition' (SE) and Java 'Enterprise Edition' (EE) informally known as 'Core Java' and 'Advanced Java' respectively. Java SE has numerous libraries and APIs to help you build an application for desktop such as a billing software or just a simple text editor whereas Java EE can help you to develop web applications such as banking systems, e-commerce platforms, etc. You can use Java to work with frameworks like Hadoop, which itself is written in Java, to process Big Data. Java is also used for game development, scientific applications, cloud computing and artificial intelligence.

The design philosophy of Java programming language is that Java must be:

Simple, Object Oriented, and Familiar

Robust and Secure

Architecture Neutral and Portable

High Performance

Interpreted, Threaded, and Dynamic

All of these principles makes Java awesome and strong. In OOP, almost everything is considered to be an object. Different OOP features like inheritance, polymorphism and encapsulation provides many benefits to a programmer such as code maintainability, reusability, flexibility and extensibility.

Java is pretty fast and provides a secure platform for developing and running applications. The Garbage Collector (GC) in Java is excellent at its work. It deals with the memory well. It removes unused things from memory that your program no longer needs and reduces memory corruption.

Java is a concurrent language that means it executes several computations concurrently instead of sequentially (one completing before the next starts). This is achieved by multithreading. Suppose a program manages a Graphical User Interface (GUI) which is waiting for a particular component to load, the program creates and uses another thread to perform the wait instead of the default GUI thread for both the tasks. This is how concurrent execution works. This keeps the GUI responsive and does maximum utilization of the CPU.

How does Java work?

Suppose, you have to write a program to display your name as output. It would be something like this:

Save this file as PrintName.java (the filename should be the same as the public class name) to create a Java source code file. Compile it by running javac (The Java Compiler). The javac tool reads class and interface definitions and also checks for any syntax errors. If there are no errors, you'll get a .class file. This is the compiled bytecode file that is machine independent unlike in C/C++ where the compiler generates .exe file which is OS dependent. .class can run on any Java Virtual Machine (JVM) installed on any device (computer, set-top box or mobile phone) regardless of the physical configuration of that system. The JVM is an abstract machine that works like an interpreter that translates the bytecode into native machine code. This is how Java keeps the promise of "Write Once, Run Anywhere (WORA)" and achieves portability.

Following are the commands you need to write in a terminal to compile and run a Java program.

javac filename.java
java filename

Output of the program

Running a program means telling JVM to load .class file (public class) and start executing the main method. Main method is where your entire program starts from. It's just like the head node of a Linked List. If you've the head pointer of a linked list then you can access the whole list.