Java Tutorial

Java Tutorial - 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 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 an example to gain in-depth knowledge of Java

Table of Contents - Java Tutorial

Overview of Java Tutorial 

Java is one of the most popular programming languages. It was developed by James Gosling in the year 1991, and the purpose was to use it in his set-of-the-box projects. It was initially named ‘Oak’ which is a tree name that 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, and it was the first core component system of Sun Microsystems. Java has got massive popularity among 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.

[Checkout: Thymeleaf vs JSP]

Java Features

#1 Object-Oriented

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

#2 Platform Independent 

Unlike the C compiler, the 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 the hardware platform of any OS. Due to this, Java source code can run on all OS and hence make Java a platform-independent language.

#3 Secure

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

#4 Robust

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

Are you interested in taking up Core Java Certification Training? then visit MindMajix to Enroll Now for Core Java Training

#5 Portable 

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

#6 High Performance 

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

#7 Architecture-neutral 

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

Top 10 Programming Languages that you need to watch out to boost your career in 2023

 

#8 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.

#9 Distributed 

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

[Related Article: Java Interview Questions]

How does 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 does not physically exist hence called a virtual machine.  JVM converts bytecode into the language understood by a particular OS and is responsible for executing Java code line by line. It is also termed an 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 applications using JRE. It contains a 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 applications, compiling java code, and executing them is possible through JDK.

MindMajix Youtube Channel

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.

[Related Article: Programming Languages ​​in Data Science]

Java Environment Setup

Java can be installed from oracle’s official website. Once it is installed, you can verify the version from the command prompt using the 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 program 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 the bin directory of Java installation. The path can be like this - C:Program FilesJavajdk1.8.0_171bin 

[Checkout: What is PyCharm]

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 a source file. 

[Related Article: Goldman Sachs Interview Questions]

Few things to consider while writing Java programs:

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

[Related Article: Linked List Interview Questions]

Compiling the program

To compile the program, we need to pass the 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 the source file. Java bytecode is the intermediate presentation of the code which is understood by JVM. So, the output of javac cannot be executed directly. 

To run the program, we need to pass the class name with the 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. The test is the identifier which is the name of the class.
  • // - A single line of comment is defined by // in Java
  • public – the public is an access modifier that is concerned with 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 the main method is called by JVM without any objects. This is due to the static keyword used which allows the main method to be called without any object of a class.
  • void – the void is the return type of method. This tells the compiler that the main method is not returning a value.
  • String args[] – this declares a parameter called args which is an array of strings. Any command-line arguments will be stored in this parameter.
  • System.out.println() – This statement is used to print text passed in method println(). The system is a predefined class that gives access to the system. out is the object of PrintStream class and println() is the method of the same class. 

[Checkout: What is Site Reliability Engineering]

Advanced Concepts of Java Programming Language

Data Types Variables and Arrays in Java
Data TypesPrimitive Data Types
Variables Scope and Lifetime of Variables
Arrays 

 

Java Operators
Arithmetic Operators in Java Assignment Operators in Java
Increment and Decrement Operators in JavaBitwise Operators Java
Shift Operators In JavaRelational Operators
Boolean Logical OperatorsAssignment Operator
Assignment Operator In JavaOperator Precedence

 

Control Statements in Java
Decision Making in JavaLooping Statements in Java
Branching Statements in Java 

 

Classes and Objects in Java
Create a Classes in JavaObjects and Methods in java
Constructors in JavaGarbage Collection in Java 
Access Modifiers in Java  

 

Method Overloading in Java
Constructor overloading in java with example Recursion in Java
Inheritance-in-javaTypes of Inheritance 
Constructor's behavior for inherited classesMethod Overriding in Java

 

Abstraction in Java
Abstract Classes  Multiple Inheritance in Java 
InterfacesWhy interface is required?
Key Points to remember about interfaces 

 

Exception Handling in Java
What is an Exception?Exception Vs Error
Exception Keywords in JavaException Handling Example 
Java’s Built-In Exceptions  

 

Multithreading in Java
Lifecycle of a Thread Creating a Thread in Java
Synchronization 

 

String Handling in Java
String LengthString Modification
String Concatenation Extraction
Replace  Trim 
Case ConversionString Conversion
String Comparison 

 

Some Important Program Logics in Java
Fibonacci SeriesPrime Number
Armstrong Number Perfect Number 
FactorialReverse a String/Palindrome String
Reverse a Number/Palindrome Number Swap number without using 3rd variable
Bubble SortSelection Sort
Insertion SortRemove duplicate elements from an Array

 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Core Java TrainingDec 07 to Dec 22View Details
Core Java TrainingDec 10 to Dec 25View Details
Core Java TrainingDec 14 to Dec 29View Details
Core Java TrainingDec 17 to Jan 01View Details
Last updated: 26 May 2023
About Author

Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

read less
  1. Share:
Java Articles