Today is the world of new emerging technologies, applications, software, etc. From a simple text message to highly advanced robotics, we have to code. It is the most important aspect of the software field. There is no developer without the coding knowledge. To make software, websites, applications, etc. we need to code a lot. There are many languages and methods to code; among them, object-oriented programming languages have gained their attention. There are many programming languages being implemented such as Java, Python, Ruby, etc.
Especially variables and data types are available in Java which are the important aspects to code. To store the values, reserved memory locations are needed which are called Variables. This is to create a memory space for that variable. The type of the data that is being stored in the variable is called a Data type.
For the variables, memory is reserved based on their types of the data by the operating system, and it decides which variable can be stored in which place. Characters, Decimals, Integers, etc. can be stored by assigning different data types to the variables.
There are mainly two data types available in Java:
• Object or Reference Data Types
• Primitive Data Types
Primitive Data Types:
Java supports eight primitive data types. Data types that are predefined by the language are Primitive data types, and they are named as keywords, i.e., they can’t be modified. Let us know about some of the primitive data types.
byte:
• It is an 8-bit signed data type called Byte which is the complement integer of two
• The minimum value it accepts is -128 which is -2^7 and the maximum is 2^7-1 which is 128
• If you don’t have a value to assign to it, it takes 0 as the default value.
• To save large amounts of space in arrays, a Byte is used as it is four times smaller size than that of an integer.
integer:
• It is an 32-bit signed data type called int
• The minimum value it accepts is -2,147,483,648, and the maximum is 2,147,483,647
• If you don’t have a value to assign to it, it takes 0 as the default value.
• If you don’t have any special concern about the memory, this is the default data type for integral values.
Some other data types used in Java are
boolean, character, short, long, float, and double, where each of them have different minimum and maximum limitations.
We use variables to store the values. Three different types of variables are there such as
• Class/static variables
• Local variables
• Instance variables
Local variables:
• Within the blocks, methods and constructors, Local variables are declared. They can’t be accessed out of the methods.
Instance variables:
• They are declared and defined outside a block, method or a constructor Unlike local variables, but created inside the class.
Class/static variables:
• The variables that are declared within the keyword static of a class are called the Class or static variables.