In java, To store the value in variables, we need data types. Data type is a kind of data that is used to store a specific type of value. Java is a strongly types language, meaning every variable must have its declaration type since variables are declared as per data types. Once a variable is declared to be a certain data type, other data type values cannot be assigned to that variable like if variable age is declared as int data type, it cannot store other data types (char, float etc) values. In Java, Data types are divided into 2 categories Primitive data types and Non-primitive data types.
Primitive Data Types
Primitive Data Types are simple and natural data type which can work on the base architecture of the underlying computer. There are 8 types of Primitive Data Types which are further divided into 4 main categories:
- Integer Type
- Floating-Point Type
- Character Type
- Boolean Type
Integer Data Type is used for all the numbers – positive numbers, and negative numbers including zero but not for fractional numbers. Java provides 4 types of Integer data types as given below:-
|Minimum value (MIN_VALUE)
|Maximum value (MAX_VALUE)
In java, the range of Integer does not depend on the operating system, while it is fixed and will be the same for each and every operating system.
Example of Integer Data Types
Floating-Point Data Type is used for fractional numbers. Java provides two types of Floating-Point Data Types as given below:-
|Significant decimal digits allowed(Approximately)
As per the given table, it is clear that we can use 6-7 decimal digits data in float data type, but if our decimal digits are greater than 7 we can use double data type.
For float data type suffix ‘F’ has been used, for example In Java code, we use 3.432 as 3.432F for float data type. For double data type suffix ‘D’ has been used like 3.432D.
In Java code, if a fractional number is written without a suffix then it will be considered as a double data type.
All Floating-Point computation follows the IEEE754 specification. Special floating type values – Positive infinity, Negative infinity, and Not a Number (NaN) are used to denote overflows and errors.
In Java program, if more than 7 decimal digits value (eg 5.98979850000F) is assigned to float data type variable, then although there will be no compilation error but internally it will use only up to to 7 decimal digits.
Example of floating-point data type
‘char’ data type is used to store character type data in Java. It has all the unsigned integer values indicating all 65536 (216) characters in the 16-bit Unicode character set. Single Quotes (‘) are used to denote char constants, Like ‘K’. Remember single quotes ‘K’ is different from double quotes “K”, former ‘k’ denotes char data type while later “K” denotes String data type.
Characters in char type are denoted in the Unicode encoding scheme. Unicode was designed to handle all the characters of all the written languages in the world. Unicode is a 2-byte code and its characters lie between the range of 0 to 65535. Unicode characters are represented as hexadecimal values which start from ‘\u0000’ to ‘\uFFFF’, here \u denotes as Unicode value while four hexadecimal value denotes Unicode characters.
In Unicode, the range for lowercase alphabets(a-z) is from 97 to 122 and the uppercase alphabets(A-Z) are from 65 to 90.
|Storage requirement (Bytes)
|Storage requirement (bits)
|Minimum Unicode value
|Maximum Unicode value
|\u0000 or (0)
|\uFFFF or (65535)
Some escape sequence for the special characters used in java coding
|double quote single quote
Example of Character Data Type
Boolean Data Type
Boolean Data Type is used to hold true or false values in the variable. It is also used to evaluate logical conditions( true or false).
Example of Boolean Data Type
Non-Primitive Data Types
Data types other than Primitive Data Types are called Non-Primitive Data Types. These are also called Reference Data Types because these refer to objects. These data types do not store the value while storing the reference (memory address) of the value.
Basically there are four Non-Primitive Data Types
String means a sequence of characters. In Java, String is a class under the java.lang package. The string object represents the sequence of characters/words or sentences.
Arrays in Java are homogeneous data structures. It stores one or more values of similar data type like if a String type array variable is declared, only String type values can be stored in that array variable. In Java, Arrays are based on indexing to store and access the array elements. Array’s first element stores at the 0th index, 2nd element on 1st index, and so on and can be accessed by using the corresponding index value.
In java, class is a kind of blueprint or prototype that includes all your data. Classes are needed to create the objects in java. A class contains fields(variables), methods, constructors, blocks to describe the behavior of an object of the class.
Class can have any or all of the variable type- instance variable, static variable, local variables
Class is created by using the access modifier- either public or package
The interface is like a class but in the interface, we can only declare the methods and variables but we can give a definition of the method(s) in the interfaces.
- All methods are declared as abstract
- All variables are declared as final static
- Interface does not have constructor since it cannot be instantiated
- Interfaces cannot be extended by the class while it can only be
implemented by the class
- Interfaces can be extended by an Interface, so multiple interfaces can be extended by an Interface
Difference Between Primitive and Non-Primitive Data Types
- Primitive types are already defined data types in Java while Non-primitive types are created by the programmers.
- To perform certain operations inside the method(s), Non-primitive types can call the methods, while primitive types cannot call the methods.
- A primitive data type instance variable always has a default value like for int it is zero, for boolean it is false while non-primitive data type instance variable value is null.
- Primate data type variable value cannot be null, while non-primitive data type variable value can be null.
- Examples of primitive types are int, float, char boolean etc while Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
- A primitive data types start with a lowercase alphabet (like int, float, double, etc), while non-primitive types start with an uppercase alphabet like (String, Array, List, etc).
A variable can be considered as a name to store the value of any particular data type. It has a name, type, and value associated with it. Variables can be used to store any kind of information like texts, codes (e.g. country codes, currency codes, etc.), numbers, temporary results of multi-step calculations etc
Java Variable Declaration and Initialization
Variable declaration is used to specify the name and type of the variable. Java declaration implicitly establishes memory allocation and values that can be stored in it.
In Java we declare a variable like this:
Once the declaration is done value can be initialized to the declared variable. Value is assigned using equal sign (=) sign like this
you can declare and initialize the value at the same time like this
Java Variable Naming Conventions
There are a few rules to give name to the Java variables
The rules are:
- Java variable names are case sensitive, so declare and use java variables carefully.
- Java variable names must start with any alphabet (a-z or A-Z), or the $ (dollar) or _(underscore) character.
- In Java variable name, after the first character, we can use the number or special character($, and _ character).
- Java reserved keywords cannot be used as variable name. like double is keyword cannot be used as variable name
- Space is not allowed between the words of variable name
- Java variable name can be of any length. But variable name should be given in meaning full way as the variable name is used in other places too within the class or outside the class.
Although below naming conventions are not java given convention but it is good to make in practice
- A variable name should be self-explanatory and should be used in lowercase. Like name, age
- If we need to use multiple words in variable names, We should use a camel case, which means the first letter of each word should be capitalized after the first word. like in departmental variable- Department and Name two words are used in which ‘d’ is small for department word while ‘N’ is Capital for Name word.
- Although we can use the dollar and underscore signs but it is still a good practice for the variable name to start with the alphabet as first character.
- A variable name with static final keyword to make a constant variable(constants) should be given in all uppercase. More than one word should be separated with an underscore (_). Example EXCHANGE_RATE, DEPARTMENT
Java Variable Types
In Java there are four types of variables:
- Local variables
- Instance variable
- Static/Class variables
- The scope of local variables is limited to only methods, constructors, or blocks.
- Local variables are created within the method, constructor, or block. These variables are created once the method, constructor, or block is called and destroyed once exits from these.
- As these are created and destroyed within the method, constructor or block since Access modifiers are not required for local variables
- Local variables are maintained using the stack data structure.
- As local variables do not have any default value since local variables should be declared and initialized before use otherwise, code will not be compiled and you will get compilation error like “The local variable marks may not have been initialized”
- Instance variables are declared on a class level which means these are declared inside the class but outside the method, constructor, or block of that class.
- Instance variables created/accessed when an object of the class is created and destroyed when an object of that class is destroyed.
- Instance variables value scope is within the instance or object of that class. It is not shared among the objects of that class. Like if marks named instance variable(s) is created inside the Student class, then marks variable value can be different for the different objects of this class.
- Instance variables are managed through the Heap data structure.
- Instance variables cannot be declared with the static keyword.
- Instance variables can be declared using the final keyword. Final type instance variable value should be initialized as default value does not work. If a variable id declared final its value cannot be changed. ( Example :private final String studentClass= “Second”;).
- Instance variables can be declared using any of the four Access Modifiers (private, public, protected, or default).
- The instance variables are visible to all the methods, constructors, and blocks within the class.
- When Instance variables are created it has its own default values. The default value for numbers is 0, for Booleans is false, and for the object is null.
- We can assign values to the instance variables during the declaration or within the constructor/methods/blocks.
Static Variables/Class Variables:
Static variables are created using a static keyword inside a class.
Only one copy of the instance variable is created and this copy is shared among all the objects of the class.
Static variables are created in memory when program execution starts (or class is loaded) and destroyed once program execution completes.
Static variables are accessed directly with the class name, although these can be accessed without the class name within the same class (class in which the static variable is declared). If static variables are accessed in another class, these will be accessed by the class name only.