Java is a widely used high-level, object-oriented
programming language, where high-level means that easily understood by humans.
The code written by humans is then converted into a machine-understandable
language consisting of a sequence of 0’s and 1’s by the Java Virtual Machine(JVM). This language was introduced by James
Gosling and team of researchers at Sun
Microsystems in 1995.
The original purpose of developing a Java
Programming language was to introduce a platform-independent, reliable, and
small enough for resource-constrained embedded systems like set-top boxes and
hand-held remote controls.
In this article, we’ll cover the basic concepts of
the Java programming language in great depth, along with working examples.
OOP’s Concept in Java
Object-Oriented Programming (OOP) in Java explains
software design by modeling real-world entities as Objects which bundle data
(attributes) and behavior (methods) using classes that serve as a blueprint for
creating objects within a class.
There are four main pillars in Object-Oriented
Programming (OOP) in Java that are Encapsulation,
Abstraction, Inheritance, and Polymorphism
forming the core principles for building modular, reusable, and
maintainable code by structuring programs around objects and their
interactions.
Given below is the description of the core
principles of Object-Oriented Programming in Java.
1) Encapsulation
It is the mechanism of wrapping data (attributes)
and the function (behavior) into a single unit. The data is protected from
external access and can only be accessed through specific public methods.
Real-life
analogy: You can think of encapsulation like a medicine
capsule, which bundles related data and methods (functions) into a single unit,
hiding the internal complexity and protecting the data from outside
interference.
2) Abstraction
It refers to the process of hiding the internal
mechanism of an object and focuse on knowing only the essential information
related to an object. It highlights the functions performed by an object while
hiding the internal mechanism of performing those operations.
Real-life
analogy: You can think of abstraction as a coffee machine
which makes hot coffee by performing some opeartion but it hides the internal
mechanism of the machine from the user.
3) Inheritance
It is a mechanism in which one class (child class
or subclass) inherits the properties and behaviors of another class (parent
class or superclass). This feature provide code reusability that reduces
duplicate code by allowing subclasses to inherit common attributes and
behaviors from a superclass.
Real-life
analogy: You can think of inheritance as a child acquiring
basic properties from his parent like, hair color, eye color, height, and skin
color.
4) Polymorphism
Polymorphism in Java is the ability to take on
many forms, allowing a single action or function name to be performed in
different ways. It enhances code flexibility, reusability, and scalability by
enabling objects of different classes to be treated through a common interface.
Features of polymorphism include multiple behaviors, method overriding, method
overloading, and runtime decision.
Real-life
analogy: Think of an adult person who plays different roles
in his life like father at home, employee at his company, and a friend among
his known people.
Structure
of a Java Program
Given below is the basic program in Java.
class
TpointTech {
public static void main(String[] args) {
System.out.println(“Welcome to Tpoint
Tech Website”);
}
}
Explanation of the above code
1.
class
TpointTech – The class named TpointTech is created
with the keyword class.
2.
public
– Access specifier that is accessible from any
class, package, and by subclasses.
3.
main()
– Entry point for program execution.
4.
System.out.println()
– Prints the output of the program.
Variables in Java
Variables are containers used to store data values. Every variable must be declared with a specific data type and a unique name. Given below is the description of different types of variables in Java.
Local Variables: These are the variables declared within a method, a constructor, or a specific block of code. The scope of a local variable is limited to a specific function or block of code under which they are declared and its lifetime is destroyed once the execution of that block finishes.
Example
public class LocalVariableExample {
public void localFunction() {
int number = 25; // local variable
System.out.println(“The value of the local variable is: ” + number);
}
public static void main(String[] args) {
LocalVariableExample object1
= new LocalVariableExample();
// Create an object name object1 to call a function.
object1.localFunction();
}
}
2. Instance variables: These
variables are used to store the unique state or data for each object (instance)
of a class. They are declared within a class, but outside of any method,
constructor, or block, and are non-static.
Example
class Example {
// These are the instance variables
public String name;
private int age;
// Constructor to initialize instance variable
public Example(String name, int age) {
this.name = name;
this.age = age;
}
// Method to print the name and age
public void displayInfo() {
System.out.println(“Name: ” + name + “ ,
Age: “ + age);
}
public static void main(String[] args) {
Example example1 = new Example(“Ridhi”, 25);
Example example2 = new Example(“Pooja”, 30);
example1.displayInfo();
example2.displayInfo();
}
}
Static variables: These are the variables belongs to the class itself rather than any specific function. These are declared using the static keyword. The scope of a static variable exists from the moment the class is loaded until the program stops running.
Example
class team {
// Instance variable
int JerseyNumber;
String playerName;
// Static variable that will be shared by all objects
static String teamName = “Punjab Football”;
// constructor
team(int n, String m) {
JerseyNumber = n;
playerName = m;
}
// Method to display player details
void display() {
System.out.println(“JerseyNumber: ” + JerseyNumber);
System.out.println(“PlayerName: ” + playerName);
System.out.println(“Tean Name: ” + teamName);
}
public static void main(String[] args) {
team p1 = new team(71, “Rohit”);
team p2 = new team(70, “Ajay”);
p1.display();
p2.display();
// Accessing the static variable directly using the class name
System.out.println(“Team name: ” + team.universityName);
}
}
Data Types in Java
Data types
refer to the type of values stored in a variable, and they also define its
size. Data type is divided into two categories: primitive data types and
non-primitive data types. Primitive data type are built in data type in
programming language that store simple values directly like int, float,
boolean, byte, short, long, etc.
Non-primitive
data type are defined by the programmer that does not store the actual data
value directly but rather a reference (memory address) to an object like
classes, objects, arrays, string, queues, trees, etc.
Primitive Data Type
class
PrimitiveDataTypeExample {
public static void main(String[] args) {
byte a = 100; // stores values between
-128 to 127
short b = 5000; // storing value range
from -32, 768 and 32, 767
int c = 100000; // most commonly used
integer
long d = 1500000000L; // Require ‘L’
suffix for large numbers
float e = 20.25f; // Require ‘f’
suffix and store about 6 to 7 decimal digits
double f = 19.99; // default for
decimal and store 15 to 16 decimal digits
char g = ‘c’; // single character
eclosed in single quotes
boolean isAvailable = true; // stores
true or false
System.out.println(“Byte value: ” + a);
System.out.println(“Short value: ” +
b);
System.out.println(“Int value: “ + c);
System.out.println(“Long value: ” + d);
System.out.println(“Float value: ” +
e);
System.out.println(“Double value: ” +
f);
System.out.println(“Char value: ” + g);
System.out.println(“Boolean value: ” +
isAvailable);
}
}
Non-Primitive Data Type
·
String
·
Array
·
Classes
·
Interfaces
Conclusion
Java is a
widely used programming language mainly used for building large-scale
enterprise applications, android mobile apps, and a wide variety of web-based
applications. Its core principle is “write once, run anywhere,” makes it highly
versatile and platform independent.
This
article describes about Java and their core concepts with working examples. To
know more about Java and other programming languages, you can visit the Tpoint Tech Website, where you can find
various articles on programming and other advanced technologies realted to
computer science. The articles are arranged in a structured format with
interview question and an online compiler for testing your code.


If you have any doubt related this post, let me know