Java Programming Basics

Java Programming Tutorial For Beginners

Java programming basics are easy to understand and the language syntax is similar to other programming languages . And therefore , it is relatively much easier to learn the  java programming basics .

Java is one of the most popular , versatile and widely used programming language . Java powers over three billion devices . One of the biggest advantage of java is the platform independence which makes it suitable for wide range of applications.

Java is said to be platform independent because java program once written can run on any platform as long as that device has Java Runtime Environment ( JRE ) .

Java language is simple , powerful , secure , object oriented and versatile high level programming language which makes it perfectly suitable for creating different types of applications .

Java Programming Basics

Java Languages Features

If you are a student who wants to learn java especially for android development then this tutorial guide you step by step for learning the  java programming essential skills for mobile app development using the android studio.

This java tutorial has been specially designed for absolute beginner as well as for experienced programmers . This tutorial is a comprehensive  resource to learn all the java fundamental programming concepts , java language syntax and its practical implementation.

Java Programming

Table Of Contents

Java Programming Basics

Table Of Contents

History Of Java

Java was developed by James Gosling and his colleagues while working on a project at  Sun Microsystems in year 1991. The Java language was initially developed for developing embedded software for electronic gadgets .

The Java language development project started as a project called “Oak” by James Gosling . However , due to some legal issues this language was later on renamed as Java . Java was officially released in year 1995.

The Java language syntax is quite similar to the syntax of C language and C++ . However , some crucial features of these languages were excluded in Java syntax in order to make the java more secure language .

The primary motive of the development of java was to create language that is platform independent . This means java program should have the capability of  “Write Once, Run Anywhere”.

The java’s birth also coincided with the advent of smart mobile handsets . The java’s capabilities were tailor made for mobile and touch screen devices . Java soon became one of the most popular , versatile and widely used language which now powers over 3 Billion devices .  

Java Programming Basics

Introduction To Computer Programming

The computer system consist of both software and hardware components . The operating system is a system software that manages all the crucial functions of the computer system.

A computer system is a digital electronic device that needs to be programmed to perform any meaningful task . The computer program directs the computer microprocessor ( CPU ) to perform the desired operations as per the program instructions .

Computer Hardware Components CE Subjects
Computer System Components CE Subjects

The computer program consist of set of program instructions and each of these instruction performs a specific task . The computer program can be written using any high level programming language .

Some of the most popular and commonly used programming languages include C language , C++ , Java , Python , JavaScript , C # ( C Sharp ) , MS .Net and many other high level languages.

However , the computer system can understand and execute only machine code instructions in binary . The binary code consist of only two numbers that is 0 ( zero ) and 1 ( One ) .

Java Programming Basics

How computer works , Computer Coding, Computer Program Compilation

And therefore , all the programs written in any programming language must be first converted to machine code instructions in binary which computer CPU can interpret and execute .This conversion process is known as program compilation.

When the user initiates the program execution , the operating system allocates the necessary resources in terms of processor time and the main memory ( RAM ) . The CPU is the brain of the computer system . The CPU reads these instructions from the memory and executes the program instructions one by one .

Why Computer Understands Only Binary Code ?

What Is A Computer Program ?

Java Programming Basics

What Is Java ?

Java is high level , platform independent , object oriented programming language. Java was developed by James Gosling at Sun Microsystems in year 1991 .

The Sun Microsystems where Java originated defines java as  simple , object oriented , network-savvy , interpreted , robust , secure , architecture-neutral ,  portable, high-performance  , multi threaded ,dynamic  computer  language.

We can think of Java as a general-purpose , object-oriented language that looks a lot like C and C++, but Java is much easier to use and far more versatile . Java lets you create more secure and robust programs that can run on multiple platforms.

Java comes in three editions . Java standard edition is used to create general purpose software . The enterprise edition of java is used to create far more complex and distributed enterprise level applications.

Whereas the micro edition of java is a light weight that is optimized for mobile app and embedded system development .  

Java Programming Basics

Java Standard Edition
Java Enterprise Edition
Java Micro Edition

Java Programming Basics

Why Java Is Preferred For Android Development ?

Android is a Linux based  open source software platform and operating system that is optimized for the touch screen and mobile devices. Android applications can be developed by using the Java programming language and the Android SDK.

Java has now become a default choice for android development for all types of mobile application development . Android apps are extensively used on all android devices .

Android is now owned and managed by Google in association with Open Handset Alliance ( OHA ) . The OHA consortium is led by Google.

Android has lion’s market share of over 86% in the mobile operating systems . In addition to Java , android application development can be done in other languages such as JavaScript and Google promoted  Kotlin.

Java Programming For Android Development , Java Programming Basics

Java Programming Basics

However ,  Java still continues to be the most preferred programming language for android mobile app development . And therefore ,the Java programming professionals are in great demand in various industries and also in the job market.

The Java Platform Micro Edition ( Java ME ) is optimized for mobile applications . The Java ME provides a high performance , flexible and  secure environment for creating applications that are suitable for embedded systems and mobile devices.

The mobile applications built with Java ME are portable, secure, and can take the advantage of the native capabilities of the mobile device.

The Android Studio which is most popular and extensively used Integrated Development Environment ( IDE ) has seamless  integration with Java . And for this reason expertise in java programming basics is prerequisite for android development.

Java Programming Basics

Programming Paradigms

The programming paradigms are simply different approaches to organize and structure the program code . Alternately , paradigms can also be referred as methodology , approach or a particular style  to write and organize the program code .

The computer science has evolved both in the hardware and  software domain. Similarly , the computer programming approaches and problem solving techniques has also evolved over the period of last few decades .

There are a number of alternative approaches to program methodology and  the manner in which the program is written and organized while building a software. Each of these programming style offers different advantages and limitations.

These different programming styles are  referred to as programming paradigms. Each of these paradigm represent fundamentally different approaches to building solutions to specific types of problems using programming.

Different programming languages came in to existence which allow the programmers to write the code as per one paradigm.  Most programming languages fall under one paradigm, but some languages  have  elements  of  multiple paradigms.

Before we start learning the java programming basics , let us first understand various programming methodologies . The programming paradigms can be broadly be grouped into three types .

Programming Paradigms

Non-Structured Programming

The non-structured programming paradigm was used in the first generation of programming languages . Basic , COBOL and Assembly language are examples of non-structured programming.

As per non-structured programming style of programming, the code is organized as collection of program statement which gets sequentially executed from first statement till the last statement.

In non-structured programming paradigm the code is written in a single continuous file . Each program statement has a statement number or label.

Non-Structured Programming
Non-Structured Programming

One of the major limitation of non-structured approach was the difficulties due to spaghetti code which makes the program code difficult to debug.

The spaghetti code is a badly entangled code due to liberal use of GOTO statement . In large and complex code it is difficult to track the flow of control due multiple use of GOTO statements.

The non–structured programming also had major limitation in terms of difficulty to reuse the program code. Further , it was not possible to secure the data from unintended changes in this approach.    

Java Programming Basics

Structured Programming

The next generation of programming languages like C language and Pascal were based on the concept of structured programming.

In structured programming , the program code is modularized in the form of functions . The function once defined can be called number of times. A large chunk of code can be replaced by a single function .

The structured programming approach also made it easy to debug the program code . For example the logical errors in the program code can be easily fixed by making the necessary corrections in the function code .  

A structured language has constructs like IF-THEN-ELSE condition statement ,  WHILE-NEXT conditional loop , SWITCH-CASE statements, DO-WHILE loop , and FOR-NEXT loop. These statements helps to simplify  the program algorithm .

Structured Programming
Structured Programming

Limitations Of Structured Programming

Although structured programming approach was a major improvement over its predecessor. However , this approach also had some major limitations.

In structured programming approach , it was difficult to secure the data and it was difficult to protect the data was unwarranted operations. In large and complex code , it is difficult to track the operations of various functions operating on the same data which can lead to spaghetti code.

Further , in structured programming approach , it is difficult to perfectly model the real world objects ( Entities ) .

OOP

Object Oriented Programming

OOP Introduction

The object oriented programming ( OOP ) approach was next generation of programming paradigm developed to overcome  the limitations of structured programming approach. The OOP methodology addressed two major limitations of the structured programming.

In structured programming , first it was difficult to secure the data and second , it was also difficult to model the real world entities .

The OOP methodology  does not provide direct access to the program data . The OOP allows  the data to be treated as private and the programmer can restrict the data access .

Further , with object oriented ( OOP ) approach , allows the programmers to easily and realistically model the real world entities .

A good understanding of OOP fundamental concepts is essential for learning the java programming basics.

Object Oriented Programming In Java , Java Programming Basics

Java Programming Basics

What Is Object Oriented Programming ?

The object oriented programming is an approach to the software application development where all application components  are treated as objects.

An object is a smallest component of a program that has some properties and behavior . The object behavior defines how to perform certain actions and interact with other elements of the program.

The objects are the basic units of object Oriented programming . In OOP methodology , the program code is organized in the form of set of classes . The class defines the object in the program code.

Each and every class represents a real world entity. In other words , the entities are objects  that need to be represented in the program code .

Object Oriented Programming , OOP Concepts , Java Programming Basics

OOP Example

Android App Development

The java programming basics is essential skill for android developers . Let us consider one example of android application since java is the post popular and widely used programming language for android development.

Let us consider example of java programming used for an android app development to understand the OOP concept of an Object and class .

An android application consist of set of activities ( Screens ) through which user interacts with the android application. The activity is simply a screen displayed to the app user .

Various controls ( Views in android ) are placed on the each activity for user to give inputs or perform some actions on these controls .

Java Programming Basics , OOP Example

Object Oriented Programming Example

Each activity ( Screen ) and the various controls displayed to the user  are objects . For example TextView is an object that displays text on the screen . 

These TextView objects are created during the program  runtime as defined in the TextView Class . Similarly , command button are objects of button class .

The various object used in the application program has corresponding classes . These classes defines the properties ( data ) and the behavior ( methods ) associated with that object .

You might find it a bit confusing initially but don’t worry , we are going to discuss all these OOP concepts much more in detail later on under  java programming basics.

Java Programming Basics

Let us now start getting familiar with java programming basics and the object oriented programming fundamental concepts .

In order to get the best out of this java tutorial it is important for you to maintain a logical learning sequence . This will help you logically correlate various java programming and object oriented programming ( OOP ) fundamental concepts.

The following learning sequence in this java tutorial will guide you understand each topic step by step. So , lets dive in . 

Java Programming Basics

Table Of Contents

Java Environment

The java environment refers to the runtime environment provided by the java platform needed for the development and the execution of java programs .

Java is a versatile language which is used for creating different types of software applications . Java is used for building desktop applications , enterprise level applications , web applications , android applications for mobile platforms and applets for web services .

The java developers would typically need all the three components required for the development and also for testing ( JDK = JRE + Tools )  the java applications.

Whereas the end user ( client ) machine would need only components ( JRE = JVM + Class Libraries ) necessary for execution of java program.

The java environment has three major components . These components include :

Java Environment ,Java Programming Basics

Java Development Kit ( JDK )

The Java Development Kit as the name suggest is primarily required for development of java applications . The JDK consist of two components . These two components are Java Runtime Environment ( JRE ) and some other application development tools.

The java development kit is intended for java developers . The JDK includes all the three components required for the development and also for testing ( JDK = JRE + Tools )  the java applications.

JDK = [ JRE + Development Tools ] 

Java Runtime Environment ( JRE )

The Java Runtime Environment as the name suggest is primarily required for providing runtime environment to the java applications . The JRE consist of two components .

These two components are Java Virtual Machine ( JVM ) and some class libraries. The JRE is installed on the end user machines for providing runtime environment to the java software application needed for software deployment.

JRE = [ JVM + Library Classes ] 

Java Virtual Machine ( JVM )

The JVM stands for Java Virtual Machine . The JVM is a vital component of the JRE which provides runtime environment to the java application.

The JVM accepts the platform independent bytecode ( dot class file ) generated by the java compiler ( javac.exe ) as input . The JVM converts the bytecode into platform specific native machine code and executes this code line by line .

Java Programming Basics

Java Environment Components JDK , JRE , JVM

Java Programming Basics

How Java Program Works ?

Java is a high level programming language . The java program gets compiled in two stages . The java compiler does not directly compile the java program  to native machine code .

Instead , the java program  first gets compiled into a platform independent bytecode. In the first stage we use the java compiler ( javac.exe ) which compiles the high level java program code into intermediate bytecode ( dot class file ) .  

This bytecode ( dot class file ) is a platform independent code that gets further compiled to native machine code. The JVM ( Java Virtual Machine ) then executes this platform specific native machine code line by line .

How Java Program Works

Java Program Compilation

The java program is said to be platform independent because the java program is compiled and executed in two stages .

In the first stage the java compiler ( javac.exe  part of JDK ) converts the java program source code file (  YourProgram.java  ) into a bytecode ( Also referred as dot class file YourProgram.class ) .

This java bytecode is a platform independent executable code that can run on any platform ( windows , mac , Linux , Unix ) which has Java Runtime Environment ( JER ) installed on it .

Java Program Compilation

Java Compilation , Java Programming Basics

The JVM ( Java Virtual Machine ) is an essential component of the JRE.  The JVM is an interpreter and responsible to execute the java bytecode ( .class file ) line by line.

And therefore , java program is platform independent but the JVM part of JRE is platform dependent . And for this reason we need to select the correct version java during the java download depending upon the platform .

Java Programming Basics

Java IDE

How To Write Java Program ?

Writing the java program is easy . In order to develop the java programs you need first install the java development kit ( JDK ) on your computer system . The java installation process has been explained in detail.

In addition to JDK you will also need a special software commonly referred as Integrated Development Environment ( IDE ) . Some of the popular and free Java IDE include :

NetBeans , Eclipse , IntelliJ IDEA Community Edition , Android Studio for android app development using java , Enide Studio 2014 and BlueJ.

Java IDE

How To Write Java Program ?

IMPORTANT

Although many java learners start writing the java programs using any IDE . However , if you are an absolute beginner then writing java programs  with IDE may not be the best option .

It is highly recommended to initially start writing your java programs using any simple text editor and save this file as java program source code ( Example.java ). You can also compile these programs from the command prompt.

Writing the java program using any text editor will give far better understanding of how java program works , how java program is compiled and executed at the command prompt.

How Java Program Works

How To Write Java Program ?

  • In this example , we have used Notepad software as a text editor to write the Hello World  java program .
  • You  can simply  write  your  first  Java  program  in  Notepad  which is commonly used as text editor for windows and   save  this  file  as  Helloworld.java  
  • Java  is  case sensitive  And  the class name must  start with the capital  letter .
  • Java being object oriented , the program code must be written inside a class.
  • The source code Helloworld.java is compiled at command prompt using java compiler  javac.exe
  • The compilation will generate Bytecode   Helloworld.class 
  • The  JVM  will  start  program  execution  with  main method .
  • Command Prompt is also known as cmd.exe  OR  cmd (  after its executable file name  ) . The cmd is the command-line   interpreter  on  Windows  and for  other  operating systems.

Java Programming Basics

How To Write Java Program ?

The IDE provides fully automated environment for java program development. The IDE provides a complete set of software and tools to write , debug and deploy professional level Java software applications.

This is similar to learning the car driving either on a fully automatic  car or on a manual transmission car. The manually driven car will teach you the judgment of clutch , gear and accelerator . Once you learn the driving then you can switch over to fully automatic car .

So , initially spend some time on the text editor writing your first few initial java programs. And once you understand the whole process of how java program works , then you can always switch over to any suitable IDE.

Java Programming Basics

Class And Object In Java

Java being a object oriented programming language views the various entities that needs to be represented in the program only in the form of objects .

The concept of class and object is a fundamental concept of Java programming basics . In object oriented programming the class contains the description of an object .

And therefore , as a programmer or a software developer , we must have a pretty good clarity about what is an object in the programming world and how to represent the objects in the program that we write .

What Is An Object In Java ?

We are all aware of the objects that exists in the real world . The world around us is full of objects such as house , car , pen , computer , cat , dog and also you as an individual .

We can recognize these objects in real world as per their properties such as shape , weight , height , color and also the behavior associated with these objects .

For example a dog is a real world object which has unique identity in terms of its breed , color , height , weight and other such properties . The dog also exhibits a behavior such as running , walking , sleeping and barking.

Object Oriented Programming , Java Programming Basics
What Is An Object In Java , Java Programming Basics

Java Programming Basics

What Is An Object In Java ?

However , in the programming world , the entities that we want to represent need to defined in the program while writing the program code .

When the program execution starts, the operating system allocates the necessary resources to the process in terms of processor time and the memory.

An object in object oriented programming ( OOP ) can either be either  tangible or intangible entity . For example car is a tangible object whereas a bank account is also a valid object but an intangible object.

An Object Definition In OOP

An object in OOP can be defined as an entity that is represented in the program which has a state , exhibits some well defined behavior and can be uniquely identified.

In OOP, an object is an entity that has properties ( data )  for identifies its state and methods that identifies its behavior or functionality . Every object is unique either by its state or because of its behavior. The events associated with object depicts the change of state.

What Is An Object In Java

Java Programming Basics

What Is Class In Java ?

As stated earlier , Java is a object oriented programming language and views the various entities that needs to be represented in the program only in the form of objects .

And therefore , the objects need to defined in the program code . In object oriented programming , a class is a description of an object . In other words , a class is a blue print or a template for creating the object .

In other words , an architect creates a blueprint for the house to be built . This architectural drawing defines the qualitative and quantitative dimensions of the house .

Similarly , the programmer needs to define the object in terms of its properties ( data ) and  the behavior in terms of the methods .  

For example , the android mobile app developer use Button class to create Button objects on the user interface ( activity ) and then defines the method in java code on button click event .

What Is Class In Java ?

What Is Class In Java

What Is Class In Java ?

The programmer first identifies the various objects that need to be represented in the program . The programmer then define these objects in the form of classes in the program code .

And for this reason , the java program code essentially consist of number of classes . The classes are defined in the java program code as per the java language syntax and the object oriented programming design principles.

In the following example , we have created a class with class name as Vehicle . This class Vehicle has data members and also member functions . The class binds together the data related to an object and the methods ( functions ) which operates on the data .

In this class Vehicle the data members include Model No , Color and Registration No. Whereas the member methods include Start ( ) , Forward ( ) , Backward ( ) and Stop ( ) .

IMPORTANT

The Class Is a design-time entity . Whereas an Object is a run-time entity.

Java Programming Basics

What Is Class In Java ?

Object Oriented Programming , Java Programming Basics
Java Programming , Computer Programming OPP

Java Programming Basics

Java Program Structure

It is important to understand the java program structure . The java program structure consist of different sections. Each section in the java program code handles a specific part of the program .

The different sections in the java program structure improve the maintainability and the readability of the java program code . The Java program structure include six sections.

Java Program Structure

Java Documentation Section

The documentation section is optional and contains the comments in the java program code . The comments are part of the program that are ignored and not compiled by the compiler.

The main purpose of the comments in the documentation section is to improve the readability and the maintainability of the java program code.

The comments are added to the program code especially in the large and complex project where number of programmers are writing the program code. The comments inserted in the program code makes it easier to understand the program code.

Java Documentation Example

Single Line Comment    // Example Comment Text

Multiple Line Comment    /* Example Multi-line Comment Line 1

This Is comment Line 2  */

Documentation Comment    /**  Example Documentation Comment 

Generated Automatically  */

Java Programming Basics

Java Package Statement

The java package allows the programmer to create a group of similar types of classes , interfaces and sub-packages . These packages once created can be easily imported into the program code .

The java packages allows the code re-usability and the programmer can simply add the required classes by importing the package that contains these classes . The java packages can be either built-in packages or user-defined package.  

The package statement in java program is optional and identifies the package that a Java program is a part of that package. The program belongs to the default package If the java program does not include a package statement.

If program does not include the package statement then  the program belongs to the default package, which is simply a package without any name.

Java Package Statement Declaration

package package_name;

Java Import Section

The java programmer can use the classes defined in other packages by directly importing the packages into the program code using import statement. The programmer can either import some classes or all the classes present in the package.

Java import statement is optional. The java programming language provide number of built-in packages which programmer can use to add additional functionality to the program .

Once the required package is imported, the classes in the package become available in the program code and can be referred to directly by using only its name. The import statement is very useful and frequently used in the Java program.

Java Import Statement Declaration

import  java.io.* ;

Java Interface Section

Java interfaces are similar to class but contains methods without any implementation details. The interfaces contains only method declarations .Java language does not allow multiple inheritance . However , the programmer can use multiple inheritance using an interface.

Java interface is an optional section. Java interface statement is used when programmers want to implement multiple inheritances within a java program code.

Java Programming Basics

Java Class Definitions

In java program , all the entities that need to be represented in the program in the form of objects are included as class . And therefore , a Java program may contain several classes defined in the program.

The Class declarations are an important element of the java program code and the java program contains number of classes .

Java Class With Main Method

As we have discussed earlier , the java program can contain number of classes . However , the java program file which will be compiled to generate the bytecode ( Dot Class File ) must have one public class that contains the main method .

The java program file must be saved with the same class name that contains the main method . This main method is the starting point of the program execution .

The Java Virtual Machine ( JVM ) which executes the java program  initiates the program execution by invoking the main method .

Java Main Method Declaration , Java Programming Basics

Java Programming Basics

Java Program Hello World

Let us now create and understand our first java program Hello World . You can write this program using any text editor ( such as Notepad ) and then compile this program on the command prompt .

In order to write and successfully execute the Java Program Hello World , you need to follow the following steps .

  • Install the JDK on your computer .
  • Set the JDK path with environment varia