In short, a Variable is the name of a reserved area allocated in memory. In other words, it is a name of a memory location. It is a combination of “vary + able” which means its value can be changed. In this article, we discuss variables and data types in java.

A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Type of Variables: There are three (3) types of variables in java:types-of-variables-java

  1. Local Variable: A variable that is declared inside the method is called a local variable. Local variables are created when the method, constructor, or block is entered and the variable will be destroyed once it exits the method, constructor, or block. Access modifiers cannot be used for local variables.
  2. Instance Variable: A variable that is declared inside the class but outside the method, is called an instance variable. Instance variables are declared in a class, but outside a method, constructor, or any block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. Access modifiers can be given for instance variables.
  3. Class/Static Variables: A variable that is declared static is called a static variable. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor, or block. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value. Static variables can be accessed by calling the class name ClassName.VariableName.

 

Type of Data: Data types represent the different values to be stored in the variable. In Java, there are two (2) types of data types:

  1. Primitive data types: There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.
    byte The byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer. A byte data type is an 8-bit signed two’s complement integer
    short The short data type can also be used to save memory as a byte data type. A short is 2 times smaller than an integer. A short data type is a 16-bit signed two’s complement integer
    int The integer is generally used as the default data type for integral values unless there is a concern about memory. an int data type is a 32-bit signed two’s complement integer.
    long The long type is used when a wider range than int is needed. a long data type is a 64-bit signed two’s complement integer.
    float The float is mainly used to save memory in large arrays of floating point numbers but is never used for precise values such as currency. float data type is a single-precision 32-bit IEEE 754 floating point.
    double The double data type is generally used as the default data type for decimal values, generally the default choice but never be used for precise values such as currency. a double data type is a double-precision 64-bit IEEE 754 floating point.
    boolean The boolean data type represents one bit of information. This data type is used for simple flags that track true/false conditions. There are only two possible values: true and false.
    char The char data type is used to store any character. a char data type is a single 16-bit Unicode character
  2. Non-primitive (Reference/Object) data types: Non-primitive data types are created by programmers. It is also called ‘Reference Variables’ or ‘Object reference’ because it refers to a memory location where data is stored. On the other hand, primitive data type stores value.

Why string is not a primitive data type in java??
The string is not a primitive data type because it has methods and only class can have methods. ‘String’ is used to represent the string class. Primitive data types cannot have methods. So, String is a class that is a non-primitive type. It is a class in java provided with several methods used to store groups of characters.

Thanks for sharing Variables and Data Types in Java.

Variables and Data Types in JavaVariables and Data Types Variables and Data Types

The article was published on July 29, 2017 @ 1:54 PM

Leave a Comment