close

Learn Java Programming

The Definitive Guide

Java is a popular general-purpose programming language and computing platform. It is fast, reliable, and secure. According to Oracle, the company that owns Java, Java runs on 3 billion devices worldwide.

Considering the number of Java developers, devices running Java, and companies adapting it, it's safe to say that Java will be around for many years to come.

This guide will provide everything you need to know about Java programming language before you learn it. More specifically, you will learn about features of Java programming, its applications, reasons to learn it, and how you can learn it the right way.

What is Java Programming Language?

https://www.programiz.com/

Before getting started on Java programming, lets get familiarized with the language first.

Why Java was Created? A Brief History

In 1991, the team "Green Team" led by James Gosling at Sun Microsystems created a programming language for digital consumer devices. The language was called Oak then. Why Oak? Because there was an oak tree outside Gosling's office.

The "Green Team" demonstrated the use of the language with an interactive television. However, it was too advanced for the digital cable television at the time, and more suitable a technology that was starting to take off, the Internet.

Later, the language was renamed Green and finally renamed Java from Java coffee; hence the coffee-cup logo.

Since C/C++ was popular back then, James Gosling designed the language with C/C++ style syntax, and philosophy "write once, run anywhere". After years, Sun Microsystems released the first public implementation of Java in 1995. It was announced that the Netscape Navigator Internet browser would incorporate Java technology.

In 2010, Sun Microsystems was completely acquired by Oracle Corporation along with Java.

Java Version History

  1. June 1991 - Java language project was initiated
  2. JDK 1.0 - January, 1996
  3. JDK 1.1 - February, 1997
  4. J2SE 1.2 - December, 1998
  5. J2SE 1.3 - May, 2000
  6. J2SE 1.4 - February, 2002
  7. J2SE 5.0 - September, 2004
  8. Java SE 6 - December, 2006
  9. Java SE 7 - July, 2011
  10. Java SE 8 (latest version) - March 18, 2014
  11. Java SE 9 - July, 2017 (announced release date)

Features of Java Programming Language

  1. Java is platform independent

    Java was built with the philosophy of "write once, run anywhere" (WORA). The Java code (pure Java code and libraries) you write on one platform (operating system) will run on other platforms with no modification.

    To run Java, an abstract machine called Java Virtual Machine (JVM) is used. The JVM executes the Java bytecode. Then, the CPU executes the JVM. Since all JVMs works exactly the same, the same code works on other operating systems as well, making Java platform-independent.
  2. An object-oriented Language

    There are different styles of programming. Object-oriented approach is one of the popular programming styles. In object-oriented programming, a complex problem is divided into smaller sets by creating objects. This makes your code reusable, has design benefits, and makes code easier to maintain.

    Many programming languages including Java, Python, and C++ has object-oriented features. If you are serious about programming, you should definitely learn object-oriented style of programming.
  3. Java is fast

    The earlier versions of Java were criticized for being slow. However, things are completely different now. The new JVMs are significantly faster. And, the CPU that executes JVM are also getting more and more powerful.

    Now, Java is one of the fastest programming languages. Well optimized Java code is nearly as fast as lower level languages like C/C++, and much faster than Python, PHP etc.
  4. Java is secure

    The Java platform provides various features for security of Java applications. Some of the high-level features that Java handles are:

    - provides secure platform for developing and running applications
    - automatic memory management, reduces memory corruption and vulnerabilities
    - provides secure communication by protecting the integrity and privacy of data transmitted
  5. Large Standard Library

    One of the reasons why Java is widely used is because of the availability of huge standard library. The Java environment has hundreds of classes and methods under different packages to help software developers like us. For example,

    java.lang - for advanced features of strings, arrays etc.
    java.util - for data structures, regular expressions, date and time functions etc.
    java.io - for file i/o, exception handling etc.

Applications of Java

Java technology is everywhere, powering 3 billion devices worldwide. It's more than likely that you have used Java one way or the other. Here are some of the applications of Java.

  1. Android apps - Java programming language using Android SDK (Software Development Kit) is usually used for developing Android apps.
  2. Web apps - Java is used to create Web applications through Servlets, Struts or JSPs. Some of the popular web applications written in Java are: Google.com, Facebook.com, eBay.com, LinkedIn.com etc.
    It's important to note that, these sites may not be entirely written in Java, and may use other programming languages along with Java.
  3. Software Development - Softwares like Eclipse, OpenOffice, Vuze, MATLAB etc use Java.
  4. Big Data Processing - You can use popular software framework like Hadoop (which itself is written in Java) to process Big Data. To use Hadoop, you need to understand Java programming.
  5. Trading System - You can build trading applications having low latency using the Oracle Extreme Java Trading Platform.
  6. Embedded Devices - While C/C++ programming languages are still popular choices for working with embedded systems, Oracle's Java Embedded technologies provide platform and runtime for billions of embedded devices like: televisions, SIM card, Blu-ray Disc players etc.

Besides these applications, Java is also used for game development, scientific applications (like natural language processing), and many others.

 

Java Terminologies You Need to Know Before You Start

https://www.programiz.com/

Learning a new programming language can be challenging. You will hear a lot of new terms which can be overwhelming for a newbie. So, we have decided to explain a few terms that you are likely to hear in the world of Java programming language.

Note, this is not the complete list of Java Glossary. Instead, it's the list of terms you should know before you start learning Java programming.

Java - Java is a set of technologies (programming language and computing platform) for creating and running software. However, Java is often used to refer Java programming language for simplicity.

Java programming language - A powerful, general-purpose, platform-independent, object-oriented programming language.

Java 8 - Java 8 is the latest major release for Java. Our Java tutorial will include all major features of Java 8.

Java EE, Java ME and Java SE - Java EE, Java ME and Java SE stands for Java Platform Enterprise Edition, Micro Edition, and standard edition respectively.
Java EE is targeted for applications which run on servers. Java ME is targeted for resource limited devices like: embedded devices. And, Java SE is the basic Java environment used for creating standard programs.
If you are a java programming newbie, we recommend you to start with J2SE.

JVM - JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.

JRE - JRE (Java Runtime Environment) contains JVM, supporting libraries, and other components to run a Java program. However, it doesn't contain any compiler and debugger.

JDK - JDK (Java Development Kit) contains JRE and tools such as compilers and debuggers for developing Java applications.

Run Java on your OS

https://www.programiz.com/

Run Java on Mac OS X

Here are the steps to install Java for Mac OS X and MacOS:

  1. Download the lastest version of Java Platform (JDK) from Java SE Dowloads page.
  2. Double click the downloaded DMG file and follow the instructions to install Java.
  3. To verify your Java installation, go to terminal and issue the following command:
    javac -version

    If Java is properly installed, the command will display the version of Java (something like javac 1.8.0_60).

Now the Java is installed, the next step is to install an IDE (integrated development environment) to write Java code and run it. We will install community version of IntelliJ IDEA, which is free. Here are the steps to install IntelliJ IDEA and run Java on in it.

  1. Go to IntelliJ IDEA download's page and download the community version.
    Download page of IntelliJ Idea
  2. Open the downloaded DMG file and follow the instructions to install it. You might want to put IntelliJ IDEA on Applications folders for future use.
  3. Now, open IntelliJ IDEA. Select Do not import settings setting and click Ok. Then, accept the Jetbrains Privacy Policy by clicking Accept button.
  4. IntelliJ IDEA will now allow you to customize its interface. You can skip all and set defaults, or choose your custom changes. If you're unsure, just click Skip All and Set Defaults.
  5. You'll now be greeted with a Welcome page. Select Create New Project.
    Welcome screen IntelliJ Idea
  6. In the next window, select Java on the left window and click New at the top to select JDK. Here, select the installation location of the JDK we copied during the Java installation and hit Next.
    Run Java in IntelliJ Idea select option
  7. You will see option to Create project from template. Ignore it and click Next.
  8. In the next screen, provide name for your project and click Finish. (We named our project Hello)
  9. You should see your project on the left menu. If you can't see this window, go to Views < Tool Windows < Project
  10. Now go to Hello > New > Java and give the class name. We have named it First.
    Create a new Java class in IntelliJ IDEA
  11. Write Java code and save it.
  12. To run the program, got to Run > run... Then, click on First (file name) which will build the executable file and run it.
    Run Java IntelliJ IDEA

Run Java in on Linux

To run Java Programming on Linux, you'll need to install two things: JAVA SE Development Kit (JDK) and IDE to create your Java Projects.

Follow this step by step guide to get started.

Note: The guide belw is for on Ubuntu 14 (x64), however the process is similar for other distributions.

Install Java

  1. Open up your terminal and type the following commands to install java.
    sudo add-apt-repository ppa:webupd8team/java
    sudo apt update; sudo apt install oracle-java8-installer
  2. Agree to the license and terms, by clicking Ok and Yes respectively.
  3. Congratulations, you've successfully installed JAVA 8. To check if Java was properly installed, type the following on the terminal.
    java -version

    If you get the current version of Java, you've successfully installed Java 8. If not, check the Oracle's Download Help.

Install IntelliJ IDEA

  1. Go to IntelliJ IDEA Downloads page.
  2. Download the Community Edition of IntelliJ IDEA which is free. Click the Download button.
    IntelliJ IDEA download page for Linux
  3. After download, change your directory to the downloaded directory in the terminal and extract the Java tar to /opt by running the following command.
    sudo tar xf <filename>.tar.gz -C /opt/
  4. After extraction, change your current directory to the IntelliJ IDEA's bin folder.
    cd /opt/<intellij-folder>/bin
  5. Now, run the following command to start the IDE.
    ./idea.sh
  6. Select Do not import settings and click Ok. Then, accept the Jetbrains Privacy Policy by clicking Accept button.
  7. IntelliJ IDEA will now allow you to customize its interface. Select the one that you like and click Next: Desktop Shortcut. Create a desktop entry for your sytem for ease of access. Then, just click Next for all other screens to start the IDE.
  8. You'll now be greeted with a Welcome page. Select Create New Project.
  9. In the next window, select Java on the left window and check if the JDK is selected on the on the Project SDK. If not, click New and browse to the installed location which is: /usr/lib/jvm/java-8-oracle.
    Intellij IDEA JDK selection on Linux
  10. Click Next twice to create your Java project.
  11. Now, write the name of your project: HelloWorld (or anything you want) and click Finish. (If you don't see a sidebar on the left of the window, Go to View > Tool Windows > Project).
  12. Now, to add a new Java class, select src folder on the left > right click > New > Java Class. Write the name of your class. Make sure you don't have spaces in the class name.
    Creating new Java class in IntelliJ IDEA for Linux
  13. Write your Java code and save it.
  14. To run the program, go to Run > Run... Then, click on HelloWorld (or your project) which will build the executable file and run it.
    Running Java program in IntelliJ IDEA

Run Java on Windows (XP, 7, 8 and 10)

To run Java Programming in Windows, you'll need to install two things: JAVA SE Development Kit (JDK) and IDE to create your Java Projects.

Follow this step by step guide to get started.

Install Java

  1. Go to the Downloads page of Java Standard Edition Development Kit.
  2. Under Java SE Development Kit section, click Accept License agreement at the top of the table. Then, click the download link for Windows (x64) if you have a 64-bit system, or Windows (x86) if you have a 32-bit system.
    Download Java JDK
  3. After download, open the setup file and follow the instructions. Click Next. Select all features by selecting "This feature will be installed on local hard drive" and copy the installation location (highlighted by yellow) in a Notepad. And, click next again.
    Enable all features in JAVA
  4. During the installation, you'll be prompted to install JRE. Click Next and you're done. Click the Finish button to complete the installation.
  5. Now, you need to edit the PATH variable. Go to Control Panel > System and Security > System. Click on Advanced system settings on the left to get System Properties window.
    Setting environment variables in Java
  6. Click on Environment Variables. Then, on the lower System variables section, find Path and click Edit to the following window.
    Java path variables
  7. Copy the whole text and paste it in Notepad. This will make it easier for you to edit and check. Check if the text already contains C:\ProgramData\Oracle\Java\javapath;. If yes, you can go to the next step. If no, copy the earlier copied installation location in the front and append bin; to the end like: C:\Program Files (x86)\Java\jdk1.8.0_112\bin;. For you, the JDK version jdk1.8.0_112 might be different. Copy the text again and paste it in the earlier PATH edit window.
    Java path variable append
  8. Click Ok to save the changes.
  9. To check if Java was properly installed, open Command Prompt by typing cmd in Windows search or using Run window (Windows Key + R), and type java -version to get similar output as below. If you do, congratulations you've successfully installed Java. If not, check the Oracle's Download Help

Install IntelliJ IDEA

  1. Go to IntelliJ IDEA Downloads page.
  2. Download the Community Edition of IntelliJ IDEA which is free. Click the Download button.
    IntelliJ IDEA download page
  3. After download, open the setup file and follow the instructions (Next > Next). Then, create Desktop shortcut for 64-bit launcher, and add associations for .java (Select the ones you want). Click Next again and Install.
    Intellij IDEA installation options
  4. Now, open IntelliJ IDEA from your Desktop shortcut.
  5. Select Do not import settings and click Ok. Then, accept the Jetbrains Privacy Policy by clicking Accept button.
  6. IntelliJ IDEA will now allow you to customize its interface. You can skip all and set defaults, or choose your custom changes. If you're unsure, just click Skip All and Set Defaults.
  7. You'll now be greeted with a Welcome page. Select Create New Project.
  8. In the next window, select Java on the left window and click New at the top to select JDK. Here, select the installation location of the JDK we copied during the Java installation.
    Intellij IDEA JDK selection
  9. IntelliJ IDEA now recognizes the JDK installed. Don't check any of the options below and click Next.
  10. Now, write the name of your project: HelloWorld (or anything you want) and click Finish. If it says the directory doesn't exist, just click Ok. (If you don't see a sidebar on the left of the window, Go to View > Tool Windows > Project).
  11. Now, to add a new Java class, select src folder on the left > right click > New > Java Class. Write the name of your class. Make sure you don't have spaces in the class name.
    Creating new Java class in IntelliJ IDEA
  12. Write your Java code and save it.
  13. To run the program, go to Run > Run... Then, click on HelloWorld (or your project) which will build the executable file and run it.
    Running Java program in IntelliJ IDEA

Your First Java Program

https://www.programiz.com/

Often, a program called Hello World is used to introduce a new programming language to beginners. It is a simple program that outputs Hello, World!. In this section, you will learn to write the same program in Java using Eclipse.

  1. Open IntelliJ IDEA.
  2. Go to File > New > Project... > Java (on the left navigation)
  3. Give the Project Name. We will call it Hello World for now and click Finish
  4. Now, you need to create a new Java class.
  5. Select HelloWorld's src folder in the left sidebar. When it is selected, go to File > New > Java Class and give Name to the class and click Ok. We will call it HelloWorld.
  6. Copy the following code in HelloWorld.java file and save it. (Shortcut: Ctrl +S, or Cmd +S for Mac)
     
    public class HelloWorld {
    	
        public static void main(String[] args) {
            // prints "Hello, World!"
            System.out.println("Hello, World!");
        }
    }
  7. Finally, hit run button. If everything goes right, you will see Hello, World! output on the screen.

To learn in detail on how this program works, visit Java Hello World program.

How to Learn Java?

https://www.programiz.com/

Learn Java from Programiz

We are working really hard to create easy to follow Java tutorial. Subscribe now and once the tutorial is complete, you will be the first one to know.

Our tutorials are designed for beginners who do not have any prior knowledge of Java. Each tutorial is written in depth with examples and detailed explanation.

Official Java Documentation

Oracle, the company that owns Java, provides quality Java Tutorials. The Java tutorial describes the features in Java SE 8, and is continuously updated to keep up with changes to the Java Platform.

Note: Official Java Documentation may not be the easiest of the tutorials to follow.

If you are serious about learning programming, you should get yourself a good book.

Granted, reading a 1000 page book takes a lot of time and patience. However, following a good Java book will help you learn Java programming, the way it should be learned.

 

Final Words and Pro Tips

You cannot go wrong with learning Java. It's the language of opportunities and possibilities.

You must be eager to learn Java by now. However, here are some tips, and best practices to follow before you learn Java.

  • Don't read Java tutorials and examples like a novel. The only way to get better in programming is by writing a lot of code.
  • Learn Java language the right way. If you are switching from another programming language (let's say C#), don't write C# style code in Java. Also, check this article on How to write good Java code?
  • Join Java communities. Once you get the hang of writing simple Java programs, join Java communities and forums. Then, try to solve other programmer's Java problems. It's a good way to expand your Java knowledge. Also, you can get help when you are stuck.

I hope this article has encouraged you to learn Java, and helped you to get started. There is no reason to wait now. Let's explore the world of Java programming language.