Data Type, variables, and constants

a couple of weeks ago

Data Type

Data Type, based on where they are stored at the time of execution are categorized into 2 major groups:

  1. Primitive Data Types
  2. Reference Data Types(Non-Primitive DataType)

PRIMITIVE DATA TYPES

Such data types are predefined by the Java programming language and named by its reserved keyword written in lower case. A primitive data type is the most basic data type in Java that specifies the size and type of variable values and has no additional methods. These were included in the Java programming language to maintain the portability of Java since the size of these does not change in different operating systems.

A primitive data type can be further sub-divided into numerical and non-numerical types. There are in total 8 primitive data types in Java, namely, byte, short, int, long, float, double, boolean, and char.


Figure: Primitive data types-sub classification
Data TypeSizeDescription
Byte1  byteStores whole numbers from -128 to 127
Short2 bytesStores whole numbers from -32,768 to 32,767
Int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
Long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
Double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
Boolean1 bitStores true or false values
Char2 bytesStores a single character/letter or ASCII values

Table 1: Primitive data types examples

Numerical Primitive Data Types:

The numerical primitive data types are further sub-divided into Integer and Floating point types.

Integer Types: are used for storing whole numbers, both positive and negative. Examples: byte, short, int, and long.

  • Byte: This data type is used to store whole numbers within the range -128 to 127. In cases where the value is meant to fall under the prescribed range and not beyond that, one must use byte, instead of other integer types to save memory.

Example: byte a = 15, byte b = -15

  • Short: This data type is used to store whole numbers within the range -32768 to 32767 Example: short s = 20000, short r = -2000
  • Int: This is the most used data type to store whole numbers in Java. It can store whole numbers within the range -2147483648 to 214748364

Example: int a = 10, int b = -20

  • Long: This data type can store a fairly high range of whole numbers, i.e., from 9223372036854775808 to 9223372036854775807, and hence used when a variable is assigned a value this large. The letter ‘L’ is placed at the end of the value to further characterize it as a long data type.

Example: long a = 200000L, long b = -300000L

Floating Point Types:

  • Float: This data type is used to store fractional numbers falling under the range 3.4e−038 to 3.4e+038. The precision of float is 6 or 7 decimal digits, i.e., it can have 6 or 7 values after the decimal point. The letter ‘f’ is placed at the end of the value to further characterize it as a float data type.
  • Double: This data type is used to store fractional numbers falling under the range 1.7e−308 to 1.7e+308. The precision of double is 15 decimal digits, i.e., it can have 15 values after the decimal point, hence it is considered better to use double instead of float for most calculation. The letter ‘d’ is placed at the end of the value to further characterize it as a double data type.

Floating point data types can also be used to hold scientific numbers such as 3.2E3 or 32 * 10^3

Example: float x= 20e3f ,   double y= 20E4d

Non-numerical Primitive Data Types:

The non-numerical primitive data types are further sub-divided into characters and boolean types.

  • Char: Char data type is used to store a single character and is written in single quotes.

Example: char qa = ‘t’

  • Boolean: This data type is mostly used in cases where conditional testing is applicable, and hence can only take value ‘true’ or ‘false’.

Example: Boolean value = false

REFERENCE DATA TYPES

Reference data types contain reference/address of dynamically created objects. They are also known as non-primitive data types. The reference data types, unlike the primitive data types are created by the programmer and not predefined by Java, can be ‘null’, can be used to call methods to perform certain operations, and begin with an uppercase letter.

Strings, arrays, classes and interface are a few examples of reference data types.

  • Strings: This data type is used to store a sequence of characters, written within double quotes.

Example: String s=”qatechhub”;  

Read more at: String Handling

  • Class: A class is referred to as an object constructor. It basically is a blueprint for creating objects in Java.

Example: class Employee

Read more at: Classes and Objects

  • Array: In Java, an array stores multiple elements of the similar type, which are indexed starting from 0.

Example: int a[]=new int[5]

Read more at: Arrays – One Dimensional and Two Dimensional

  • Interface: An interface in Java is similar to a class and stores a collection of abstract methods. Whenever a class needs to use interface abstract methods, it must implement it.

Example: Interface Student

Read more at: Interfaces

VARIABLES

A variable is simply used to store a value. The name of the variable is nothing, but the name of the location in the memory where this value is stored. A variable has a data type based on the value stored in it.

Declaration of variables

To create a variable, a data type and value is assigned to it.

Syntax:

type variableName=value;

To declare many variables of the same type, a comma is used as shown below:

Syntax:

type <variableName>=value, <variableName>=value, <variableName>=value;

A variable can also be declared without assigning a value to it. A value can be assigned later, depending on the type of variable.

Syntax: type <variableName>;

New value can be assigned to a previously declared variable, overwriting the previously assigned value. However, in case the programmer doesn’t want the value assigned to a variable be changed, the keyword final can be added before the type to declare the variable as constant or read-only.

VARIABLE TYPES

There are 3 types of variables in Java based on their scope:

  • Local variable: A variable that is declared inside the body of the method and is only visible, hence accessible within that method and not in any other method within the class. Local variables are implemented at stack level internally. They do not have a default value assigned to them, hence must be declared with an initial value. Access modifiers cannot be used for local variables.
  • Instance variable: An instance variable is declared in a class, but outside the body of any particular method, hence are visible and accessible for all methods within the class. Its value is instance specific and is not shared among instances. Such variables have default value assigned to them, such as 0 for numbers, false for boolean and null for object references. Access modifiers can be used for instance variables.
  • Static variable: Static variables are declared using the keyword static in a class, but outside any particular method, hence are also known as class variables. They are mostly declared public to be visible and accessible both within the class and for the users of that class. Only one copy of such variables exist per class. Static variables are stored in the static memory and are created as the program starts, and destroyed as the program stops. Static variables are assigned the same default values as the Instance variables.

Example:

public class Test {

	int instanceVar = 10; // instance variable
	static int staticVar = 100;// static variable

	void functionToShowLocalVariable() {
		int localVar = 90;// local variable 
	}

}// end of class  


Variable Operations

In Java, a set of basic operations can be performed on variables, such as, using arithmetic operators, referencing variables to objects post object instantiation, etc.

Arithmetic operations such as addition, subtraction, multiplication and division can be used to perform calculations on variable values.

Addition of two variables: Performed using the ‘+’ operator

Subtractions of two variables: Performed using ‘-‘ operator

Multiplication of two variables: Preformed using ‘*’ operator

Division of two variables: Performed using ‘/’ operator

Example:

package resources;

public class Test {

	public static void main(String args[]) {
		int instanceVar = 10;// local variable
		int staticVar = 100;// local variable

		// Program to demonstrate the Variable operations
		System.out.println(instanceVar + staticVar);// Output -110
		System.out.println(instanceVar - staticVar);// Output -90
		System.out.println(instanceVar * staticVar);// Output -1000
		System.out.println(staticVar / instanceVar);// Output -10
	}
}// end of class  


CONSTANTS

Constants in Java are read-only variables, i.e., their value cannot be changed once assigned.

Java doesn’t implement constants directly. For any variable to behave like a constant we need to use ‘static’ and ‘final’ modifiers in the following manner:

The keyword final is used to declare a constant. As per the naming convention in Java, all constants must be written in uppercase.

Syntax: static final type variableName=value;

public class Test {
	static final int instanceVar = 10;// constant variable

	public static void main(String args[]) {

		// Program to demonstrate the constant variable use
		System.out.println("Value of Constant variable initially" + instanceVar);// Output -10
		instanceVar = instanceVar + 10;
	}
}// end of class  

When we will try to execute the above-mentioned program, then it will give us the following error. Since the Constant variable value can’t be changed

Thanks, for reading out this article. Hope you have enjoyed reading it out. If you have any questions or queries. Please feel free to write us at support@qatechhub.com

Tejasvi Nanda

About the Author

Tejasvi Nanda