Variables, Data types and Modifiers

Datatypes, Variables and Modifiers in Java:

Rupam Jha
JavaMadeTranquil
Published in
7 min readOct 28, 2020

--

I have assembled these topics into this single article, as they are interlinked with each other’s definitions and hold better explanations when put together. Also I as a publisher would never like my readers to remain in any sort of a dilemma while reading the articles which I intend to write to make things and concepts look little lesser complex to you all out there.

Hello Comrades,

So to begin with, let us understand a basic terminology of what these words actually mean, and as we proceed further we will dive deep into their classifications and usages in Java.

Variables :

A variable is basically a container which holds the value while any Java program gets executed. These are nothing but reserved memory locations to store values, which means that when you create any variable you reserve some space in the memory. A variable is the basic unit of storage in a program. It is always assigned with a data type to it. Data types defines values that any variable can take.

Note: The value that we store in a variable can be changed during the execution of program as per the modifiers they are used with. A variable is the only name given to the memory locations and any operation done with the variables actually effects the memory locations. Java is a statically typed language. By statically typed language I mean, the data type of the variable is known at compile time[while writing a code], which further means we have to specify the type of variable before we could even use it.

Declaration of a variable:

In Java the variables are declared before they can be used.

eg: int a = 100;

here int refers to the datatype

a refers to the name of the variable

100 refers to the value assigned to it.

Variable naming convention:

  • Variable name should not contain any white spaces.
  • Variable name can begin with special characters like _ and $
  • Variable name is recommended to start with small characters.
  • Variable names are case sensitive.

Types of Variables :

There are four types of variables in Java.

  • Instance variables[Non-Static variables] :

These variables are defined without the “static” keyword. They are defined outside a method declaration. They are specific to the objects of the class, and hence are known as Instance[object] variables. Each instance [object] of a class has its own copy of a instance variable.

  • Class variables [Static variables]:

These variables are initialized only once at the beginning of the program execution. These are defined with “static” keyword. This tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class is been instantiated [no matter how many times objects are made of that class].

  • Local variables:

These are the variables specific to any single method as they are declared within a method. Their scope is limited to the method which means that we cannot access these variables outside a method.

  • Parameters:

The parameters of a method are always classified as a variable and not as fields. eg. public static void abc(int a).

Data types:

The datatypes are defined as specifiers which specify and allocate different sizes and types of values that can be stored in the variables.

Types of data types:

  • Primitive data type: [integer, character, boolean, float]

These are predefined data types that are already present in Java language. There are 8 primitive data types categorized into 4 sub types as : Integer data type[byte[1 byte], short[2 bytes], int[4 bytes], long[8 bytes]], floating data type[float[4 bytes], double[8 bytes]], textual data type[char[2 bytes]], logical data type[boolean[1 byte]].

  • boolean data type: A boolean data type comprises of a bit of information which can store either true or false values. This data type usually tracks true/false conditions.
  • byte data type: A byte data type comprises of a 8-bit signed two’s compliment integer, storing value ranging from -128 to 127.
  • char data type: A char data type is used when we have to store a single character , enclosed within a single quote. We can use ASCII values too to display these characters.
  • short data type: A short data type is greater than a byte in terms of size and less than that of an integer, storing range from -32,768 to 32,767.
  • int data type: An int is a preferred data type when we create a variable with numeric value assigned to it. It stores values ranging from -2147483648 to 2147483647.[ not recommended to be memorized]
  • long data type: A long data type comprises of a 64-bit two’s complement integer ranging from -2⁶³ to 2⁶³ -1.
  • float data type: A float data type comprises of storage of fractional numbers. The value of a float data type should always end with ‘f’. eg. float a = 10.7f;
  • double data type: A double data type comprises of storage of fractional numbers. The value should always end with ‘d ’. eg. double a = 10.7d;

Note: The size of the data types remains same on all platforms[32-bit / 64-bit]

  • Non-Primitive data type:[strings, classes, arrays, interfaces]

These are not actually defined by the programming language but are rather created by programmers / developers, also termed as “reference variables” or “object references”.

  • Strings: A string is an object that represents sequence of characters.
  • arrays: An array stores one or more values of a specific data type there by providing indexed access to the elements of an array.
  • classes: It is termed as a blueprint that composes of all the data. A class contains variables , methods, constructors, that describes the behavior of an object in it.
  • interfaces: Like a class even interfaces comprises of methods and variables , but the methods are abstract [by default] i.e. no implementation within the body of a method.

The basic difference between Primitive and Non-Primitive data types:

  • Primitive data types are predefined whereas Non-Primitive are programmer defined.
  • Primitive data types cannot hold null values to it, but Non- Primitive can hold null values to it.

Modifiers:

Access modifiers are keywords in any object oriented programming languages that set accessibility of classes, methods and other members. They facilitate the encapsulation of components. In other words, they help restrict the scope of a class, constructor, variable, method or data member. The modifiers are further grouped into two sections based on their functionalities as Access modifiers and Non-access modifiers. Access modifiers are used to control the visibility of a class or a variable or a method, where as non- access modifiers are used to provide other functionalities like synchronizing a method or a block, restricting serialization of any variable etc.

Types of access modifiers:

  • private: private members of a class be it any method or a constructor, they cannot be access outside of a class in which they are defined, not even to their sub classes.
  • public: public members are visible to all the packages and all the class and sub classes in any packages.
  • default: default members are actually with no access modifier and are visible anywhere within a package. They are visible to their sub classes within the package.
  • protected: protected members hold half the characteristics of the public members and half of default members. i.e. they are visible within package like default members and visible to their sub classes like public members.

Types of Non- access modifiers:

  • static: The members declared static becomes common for all the instances[objects] of the class. These are basically class level members and are stored in class memory. It makes any class member independent of any object of that class.
  • final: The members declared final becomes restrictive to any further modifications in the variable, method or a class. The value of variable declared as final cannot be modified further.The method declared as final cannot be overridden by any sub classes we cannot create any sub class of a class declared as final[i.e. we cannot inherit any final class]
  • abstract: Either a class or a method can be declared as abstract. No variable or constructor can be declared as abstract. A method declared as abstract has to be modified[implemented] in any of its sub classes, and we cannot instantiate[create an object] of any abstract classes. The abstract modifier is heavily used with the concept of polymorphism.
  • synchronized:When two or more threads needs to access a same resource, we somehow need to make sure that only one of the thread get the access at a time, and in order to achieve so, we need to synchronize them. A method or a block declared as synchronized becomes accessible by only one thread at a time in any multi threaded environment. Synchronization is a strategy or a method to avoid thread interference hence protecting data from inconsistency. Synchronization in Java is built around an entity called as “ object lock or monitor”.
  • transient: The modifier is basically used in a process called serialization. A variable declared as transient will not be serialized during the object serialization. Serialization is a process of converting state of an object to byte stream which can be persisted in to a disk /file or sent over a network to any other running JVMs. Its main purpose is to save the state of any object in order to be able to re create it when needed [reverse process is called de-serialization]
  • volatile: If any variable is marked as volatile, it indicates that the state of the variable can change its value between different accesses among’st various threads . In order to better understand “volatile” we need to know a little about how the memory works? Usually, a variable’s value is stored in the main memory [RAM] but so as to boost the performance, the processor may store the value in its cache [L1, L2,L3 caches]. In this case, any change in the value is written back to the main memory only when the cache synchronizes with the main memory. Now if a variable is declared as volatile, the compiler [even runtime] seeing this “volatile” keyword knows that this variable is shared and hence volatile variables are never cached in registers and the read operations on these variables returns the most recent write by any thread.
  • strictfp: This modifier is used for any floating point calculations. It ensures that we get same calculation results on every platform.

That is it on this article about data types, variables and modifiers in Java. for any further clarification please drop in your queries or suggestion in the comment below. I hope this article was worth your time.

Until next time!

Peace Out!

Rupam Pawan Jha

--

--

Rupam Jha
JavaMadeTranquil

Senior DevOps Engineer. Automation Enthusiast. Sharing my experiences on AWS, CI/CD, Docker, Kubernetes, and IaC.