Java Programming: An In-Depth Introduction

Osadha Madhuwantha
MS Club of SLIIT
Published in
6 min readJun 28, 2024

--

What is Java?

Java is a high-level, class-based, pure-object-oriented programming language designed to have as few implementation dependencies as possible. It was developed in 1991 by James Gosling in collaboration with Sun Microsystems Inc. Originally intended as a platform-independent language for programming home appliances, it was initially called “Oak” and later renamed “Java” in 1995. Java’s core philosophy is “write once, run anywhere” (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java applications are typically compiled to bytecode, which can run on any Java Virtual Machine (JVM) regardless of the underlying computer architecture. Java is now owned by Oracle.

Key Terminologies in Java…

> Class

A collection of data (fields) and methods (functions) that operate on that data. Classes serve as blueprints for creating objects.

> Method

A group of statements within a class that perform a specific task. Methods define the behaviors of objects created from the class.

> Attribute

A property of an instance of a class. Attributes, also known as fields or variables, hold data associated with objects.

> Interface

A reference type in Java that is similar to a class. It is a collection of abstract methods and static constants. Interfaces specify what a class must do, but not how.

> Package

A namespace for organizing classes and interfaces in a logical manner. Packages help avoid name conflicts and can contain multiple classes, interfaces, and sub-packages.

> Bytecode

A set of instructions compiled from Java source code, intended to be executed by the JVM. Bytecode is platform-independent.

> Virtual Machine

The runtime environment in which Java bytecode is executed. The JVM ensures that Java applications can run on any device or operating system that has a compatible JVM.

Object-Oriented Programming in Java

Java is a pure object-oriented language. Object-oriented programming (OOP) is a programming paradigm centered around objects. The key principles of OOP in Java are:

> Objects

Instances of classes representing real-world entities with data (attributes) and behaviors (methods).

> Encapsulation

Encapsulation involves bundling data (attributes) and methods that operate on the data into a single unit, or object. This hides the internal state of the object and protects it from outside interference and misuse.

> Polymorphism

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. This enables multiple classes to be treated through a common interface, promoting flexibility and reuse of code.

> Inheritance

Inheritance enables new classes (subclasses) to inherit attributes and methods from existing classes (superclasses). This promotes code reuse and hierarchical classification.

> Abstraction

Abstraction simplifies complex systems by modeling real-world entities as abstract concepts, focusing on relevant characteristics while hiding unnecessary details.

> Class

A blueprint or template for creating objects. A class encapsulates data (attributes) and behaviors (methods) common to all instances of the class.

Java Keywords and Data Types

Java Keywords:

Java has a set of reserved words known as keywords that have predefined meanings in the language syntax. Some common keywords include class, interface, extends, implements, public, private, protected, void, static, final, and abstract.

Java Data Types:

Data types specify the different sizes and values that can be stored in the variables. There are two main types in Java.

1. Primitive data types

2. Non-Primitive data types

  1. Primitive data types: Primitive data types are the building blocks of data manipulation in Java.
  • boolean: Stores true or false values. Default is false.
boolean isJavaFun = true;
  • byte: An 8-bit signed two’s complement integer. Range: -128 to 127. Default is 0.
byte age = 30;
  • short: A 16-bit signed two’s complement integer. Range: -32,768 to 32,767. Default is 0.
short year = 2024;
  • int: A 32-bit signed two’s complement integer. Range: -2³¹ to 2³¹-1. Default is 0.
int population = 1000000;
  • long: A 64-bit signed two’s complement integer. Range: -2⁶³ to 2⁶³-1. Default is 0.
long distance = 123456789L;
  • float: A single-precision 32-bit IEEE 754 floating-point. Default is 0.0f.
float temperature = 36.6f;
  • double: A double-precision 64-bit IEEE 754 floating-point. Default is 0.0d.
double salary = 10000.50;
  • char: A single 16-bit Unicode character. Range: ‘\u0000’ to ‘\uffff’. Default is ‘\u0000’.
char grade = 'A';

2. Non-Primitive Data Types: Non-primitive data types, also known as reference types, store references to objects in memory. These include classes, arrays, and interfaces.

Access Specifiers in Java

Access specifiers determine the scope and visibility of classes, methods, and variables. The four types of access specifiers are:

  • Private: Accessible only within the class.
private int age;
  • Default: Accessible within the same package. No keyword is used.
int number;
  • Protected: Accessible within the same package and by subclasses.
protected String name;
  • Public: Accessible from any other class.
public String address;

Java Architecture

Java Application (Bytecodes)

Java source code is compiled into bytecode by the Java compiler. Unlike machine language, bytecode is platform-independent and can be executed by any JVM. This bytecode is stored in .class files.

Java Virtual Machine (JVM)

The JVM is an essential part of the Java Runtime Environment (JRE). It translates bytecode into native machine code and executes it. Each platform has its own JVM implementation, which allows Java applications to run on any device that has the appropriate JVM.

Java Runtime Environment (JRE)

The JRE provides the libraries, Java Virtual Machine, and other components required to run applications written in Java. It does not include development tools like compilers or debuggers.

Java Development Kit (JDK)

The JDK is a superset of the JRE and includes development tools necessary for developing Java applications, such as the Java compiler (javac), the Java interpreter (java), and tools for documentation (javadoc) and debugging (jdb).

J…. Confusion

· JVM (Java Virtual Machine):

JVM is a part of both the JDK and JRE that translates Java byte codes and executes them as native code the client machine.

· JRE (Java Runtime Environment)

It is the environment provided for the java programs to get executed. It contains a JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc.

· JDK (Java Development Kit)

JDK contains tool needed to develop the Java programs ( javac, java, Javadoc, appletviewer, jdb, javap, rmic…) and JRE to run the programs

· Java SDK (Java Software Development Kit)

SDK comprises a JDK and extra software, such as application servers, debuggers, and documentation.

· Java SE (Java Standard Edition)

Java platform, standard edition lets you develop and deploy Java applications on desktops and servers (same as SDK).

· J2SE, J2ME, J2EE

Any Java edition.

Conclusion

Java’s robust architecture, rich API, and platform independence make it a versatile language for various applications, from web development to enterprise solutions. Understanding Java’s core concepts, including its object-oriented principles, data types, and architecture, is essential for any aspiring Java developer.

--

--

Osadha Madhuwantha
MS Club of SLIIT
0 Followers

Undergraduate in Bsc(hons) in Information Technology Specialized in Software Engineering