Data type in java
Suppose your mother asks you to fetch pepper from the kitchen. You will go to the kitchen and search for pepper.
After searching for a while, you get red pepper; you bring the same back to your mom.
But your mother gives you a disappointed look because she wanted black pepper and not the red one. You’ll be frustrated naturally.
The problem could have been easily solved if you were told:
- The kind of pepper: black or red
- The definite location in the kitchen where it is stored.
If you were already told that your mom wants BLACK pepper which is in the CABINET in the Kitchen, the task would have been accomplished in lesser time and effort.
Similarly, constant data may be of varying kinds: Numbers (10, 58, 9765) ; Decimal Point Numbers (5.8 ,10.5, 89.94125) ; Character ( ‘N’, ‘V’, ‘A’) ; String or Alphanumeric data ( “Tony Stark” , “ABC64cx”).
Every constant and variable in Java has a data type. Note that the constant and variable must be of same data type.
If you want to store an integer data (constant), it must be stored into an integer variable. Similarly, a string variable will store a String Constant only.
So, it is important to define a data type for every variable we are using during the course of the programming.
On the basis of their type, the data are given specific names:
|Integer||Includes natural numbers like 64,897,632541||int|
|Double||Numbers with two decimal places (10.59, 341. 74)||double|
|Float||Numbers with decimal like (36.9896, 41.27826)||float|
|Char||Single Characters [within single quotes] (‘N’,’v’)||char|
|String||Alphanumeric data (“Iron Man”, “GhCz63oR”)||String|
|Boolean||Has only two kinds of values: True or False||boolean|
The syntax of declaring a variable is:
<Data_Type> <Variable Name> = <Constant Value> ;
int k = 5;
[Data Type: Integer, Variable Name: k, Constant Value: 5]
char z = ‘N’;
[ Data Type: Character, Variable Name: z, Constant Value: N]
[Data Type: Double, Variable Name: b, Constant Value: 6.52]
String x= “Vivek”;
[ Data Type: String, Variable Name: x, Constant Value: “Vivek”]
Actually, the data types have an ever broader classification:
Data Types are of two kinds:
- Primitive Data Type.
- Non- Primitive Data Type.
Primitive Data Type in java:
Primitive means basic, these data type are independent of any other data type.
They are further classified into:
- Integer Type: Data consisting of whole numbers. Can be a negative or a positive number but without any decimal points.
- Floating Type: Data consisting of fractional numbers. For small range, we use float data type, but for larger ranges, we use double data type.
- Character Type: Data consisting of a single character or a group of characters. If the data is a single character or a special symbol, we use char data type. For a group of characters or alphanumeric data, we use a String data type.
- Boolean Type: Boolean data can have only of the two values at a time: either ‘True’ or ‘False’.
Non Primitive Data Type in java:
Data types which are dependent on other data types.
Non Primitive Data Type are also called Derived Data Type. We’ll discuss it in the upcoming lessons.
You know, every variable must have a value.
But what if you haven’t declared any value for a variable? The variable still must contain a value.
This pre-defined value is absurd and is known as garbage value.
If you execute your program with this garbage value, the results may come erroneous.
Thus it becomes necessary to initialize a variable.