Blog

Java Tutorial for Beginners

  • (4.0)
  • | 346 Ratings |
  • Last Updated February 12, 2019

Java is a robust and widely used programming language. This Java tutorial is designed for anyone who wants to learn Java from scratch. This tutorial will walk you through to all Java concepts by using examples. Examples are 100 times better than text when it comes to understanding any concept. We have included all the concepts with example to gain in-depth knowledge of Java.

java-tutorial

Java Overview

Java is one of the most popular programming la0nguages.It was developed by James Gosling’s in the year 1991, and the purpose was to use it in his set of box project. It was initially named as ‘Oak’ which is a tree name stood outside the Gosling’s office window, and also went renamed it as a ‘Green’, but later it ended up with a name known as JAVA. 

Sun Microsystems originally developed Java programming language in the year 1995 initiated by James Gosling’s, and it was the first core component system of the Sun Microsystems. Java has got massive popularity among the programming languages due to its flexibility to configure on multiple platforms. For example, J2EE is used for the development of Enterprise applications and J2ME is for mobile applications.

Java Features

Object Oriented: Object-oriented programming is at the heart of Java. Almost all the Java programs are object-oriented at some extent. Object-oriented programming (OOPs) is a method to simplify software development and maintenance by following some rules.

Platform Independent: Unlike C compiler, Java compiler produces and converts the source code into a unique format called bytecode. Bytecode is understandable by any JVM installed on any Operating System. Java’s runtime platform runs on top of hardware platform of any OS. Due to this, Java source code can run on all OS and hence make Java a platform independent language.

Secure: Java is declared as most secure programming language. It is secure as its source code runs in a virtual machine sandbox.

Robust: Java focuses on compile-time and runtime error checking thus reducing the error-prone situations.

Portable: Java bytecode is portable. You can carry it to any platform and it will work. It doesn’t require any implementation.

High Performance: Java’s bytecode is close to native code and that is why it is faster than all other programming languages. 

Architecture-neutral: Java compiler created an architecture-neutral object file format. This facilitates the compiled code to work on many processors with JRE.

Multi-threaded: Java enables developers to code in such a manner that multiple tasks can be done simultaneously. This can enhance the user experience of any application due to its interactive nature.

Distributed: Java is designed and developed to work in distributed environments on internet.

How Java Works?

To know how Java works, we need to understand 3 basic terms: JDK, JRE and JVM.

How Java Works

JVM: JVM stands for Java Virtual Machine. It is not physically exist hence called virtual machine.  JVM converts bytecode into the language understood by particular OS and is responsible for executing Java code line by line. It is also termed as interpreter.

JRE: JRE stands for Java Runtime Environment. It provides the runtime environment for executing Java code. It can only execute the existing code. We cannot develop code or build new application using JRE. It contains set of library class files and other files which JVM uses at runtime.

JDK: JDK stands for Java Development Kit. It contains JRE and development tools to develop Java applications and build new apples. Developing new applications, modifying existing application, compiling java code and executing them is possible through JDK.

Java Prerequisites

For executing Java code, we need below 3 things installed in our machine

  • Linux or Windows Operating System
  • Java Runtime Environment
  • Notepad or any other text editor to write code

Nowadays, we have so many java tools available like Eclipse, Netbeans which makes coding interesting and easy.

Java Environment Setup

Java can be installed from oracle’s official website. Once it is installed, you can verify the version from command prompt using command “java -version” like below:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:Userspalakharshadbhai.sha>java -version
java version "1.8.0_171"
Java (TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot (TM) Client VM (build 25.171 -b11, mixed mode)

Once it is installed, we need to set it up in our system to run the java programmes effortlessly. To set up the Java environment in Windows, follow the below steps.

  • Navigate to System Properties → Environment variables
  • Choose the system variable “Path” and edit it
  • Add a new path to that variable. Give the value of bin directory of Java installation. Path can be like this - C:Program FilesJavajdk1.8.0_171bin 

A Simple Java Program

Let us start by writing a simple Java program.

class Test {
    //first sample program
 public static void main(String args[]) {
 System.out.println("Our first Java program");
}

The file which contains Java source code is called source file. 

Few things to consider while writing Java programs:

  • All the java code must reside in a class
  • Java is case sensitive language so the variables v1 and V1 holds different values.
  • Source file name should be same as the name of the main class
  • A source file can contain multiple classes
  • All statements in Java ends with semicolon
  • Java compiler needs the source file to have .java file extension

Compiling the program

To compile the program, we need to pass filename to the command javac like shown below:

javac Test.java

The javac compiler generated the .class file which is nothing but the bytecode version of the code written in source file. Java bytecode is the intermediate presentation of the code which is understood by JVM. So, output of javac cannot be executed directly. 

To run the program, we need to pass the class name with command java.

java Test

This will execute the bytecode and print the output:

Our first Java Program

Now, let us try to decompose the code and understand the keywords used in this code unit.

  • class – class is used to declare the new class. Test is the identifier which is the name of the class.
  • // - A single line of comment is defined by // in Java
  • public – public is an access modifier which is concerned of class members accessibility. public means all class members are visible to all
  • static – To call any method we need an object of a class. But main method is called by JVM without any objects. This is due to the static keyword used which allows main method to be called without any object of a class.
  • void – void is return type of a method. This tells compiler that main method is not returning a value.
  • String args[] – this declares a parameter called args which is an array of string. Any command line arguments will be stored in this parameter.
  • System.out.println() – This statement is used to print text passed in method println(). System is a predefined class which gives access to system. out is the object of PrintStream class and println() is method of the same class. 

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.


DMCA.com Protection Status