Skip to content

Commit a22f5c6

Browse files
authored
Merge pull request #85 from shoebxsiddiqui/shoebxsiddiqui-Data-Type
Update dataTypes.md
2 parents 41f97dc + 2600c3d commit a22f5c6

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

lessons/dataTypes.md

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,107 @@ it later. That's why these are called **literals**, cuz it can not be modified.
3030
Data types are important because each programming language needs different types of data like integers and
3131
strings, and these data are stored as that data type. Data types are used to define a variable before to use in a
3232
program, its a constrains that a expression, such as variable or a function, might take.
33-
In C++, whenever a variable is defined, the compiler(which compiles source code written in a high-level language(C++)
33+
In Java, whenever a variable is defined, the compiler(which compiles source code written in a high-level language(Java)
3434
into a set of machine-language that can be understood by a digital computer's CPU) allocates some memory for that
3535
variable based on their data-type. It has some types:
3636

3737
![data-types](./images/dataTypes.png)
38+
39+
There are two types of data types in Java:
40+
41+
1. **Primitive data types**: The primitive data types include boolean, char, byte, short, int, long, float and double. These are pre defined or they are the most basic structure for building more sophisicated data types.they always have a value.
42+
43+
2. **Non-primitive data types**: The non-primitive data types or reference data types include Classes, Objects, Interfaces, Strings, and Arrays. They have null as default value.
44+
45+
In Java, all variables must be declared first before they can be used.
46+
47+
# Primitive Data-Type
48+
49+
These are the building blocks of data manipulation. In Java, these are the most basic data types.
50+
51+
These are of `8 types`:
52+
53+
|Data Types |Default Value |Default Size |
54+
|-------------|-----------------|---------------|
55+
|boolean |false |1 bit |
56+
|char |'\u0000' |2 byte |
57+
|byte |0 |1 byte |
58+
|short |0 |2 byte |
59+
|int |0 |4 byte |
60+
|long |0L |8 byte |
61+
|float |0.0f |4 byte |
62+
|double |0.0d |8 byte |
63+
64+
1. **Boolean Data Type** : Variables declared with boolean data type can store only true/false. These are used as flag which checks for a particular conditions. Its size is only 1 bit.
65+
```java
66+
boolean a = false;
67+
```
68+
2. **Byte Data Type** : It is 8-bit signed 2's complement integer. Its value-range lies between -128 to 127 (inclusive). Its default value is 0. The byte data type is used to save memory in large arrays where the memory savings is most required. It saves space because a byte is 4 times smaller than an integer. It can also be used in place of "int" data type.
69+
```java
70+
byte a = 10;
71+
byte b = -20;
72+
```
73+
3. **Short Data Type** : It is a 16-bit signed 2's complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its default value is 0. A short data type is 2 times smaller than an integer.
74+
```java
75+
short a = 10000;
76+
short b = -5000;
77+
```
78+
4. **Int Data Type** : It is a 32-bit signed 2's complement integer. Its value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its default value is 0. It is generally used as a default data type for integral values.
79+
```java
80+
int a = 100000;
81+
int b = -200000;
82+
```
83+
5. **Long Data Type** : It is a 64-bit 2's complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its default value is 0. The long data type is generally used when the integer that we have to store exceeds the range provided by int data type.
84+
```java
85+
long a = 100000L;
86+
long b = -200000L;
87+
```
88+
6. **Float Data Type** : It is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited. It is recommended that we should use float (instead of double) if yweou need to save memory in large arrays of floating point numbers. It should never be used for precise values, such as currency. Its default value is 0.0F.
89+
```java
90+
float f1 = 234.5f ;
91+
```
92+
7. **Double Data Type** : It is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. This is also generally used for decimal values just like float. The double data type also should never be used for precise values, such as currency. Its default value is 0.0d.
93+
```java
94+
double d1 = 12.3;
95+
```
96+
8. **Char Data Type** : It is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive). It is used to store characters.
97+
```java
98+
char letterA = 'A';
99+
```
100+
101+
# Non-Primitive Data-Type
102+
103+
These are created by programmers, unlike primitive data types which are already built-in. These are used to store a group of values for eg, Arrays, in arrays we store a group of data, another eg is list. Therefore, these are also known as advanced data types in Java. When we define a variable of non-primitive data types, it references a memory location where data is stored in the heap memory. That's why, a non-primitive data type is also known as reference data type in Java or simply object reference variable.
104+
105+
Non-Primitive data types are created by instantiation, i.e, an object is created. This object reference variable lives on the stack memory and the object to which it points always lives on the heap memory. The stack holds a pointer to the object on the heap.
106+
107+
This object can have more than one reference variable, and if we make changes on the data of the object through one reference variable the actual object gets changed and this changed objects's data is changed in all the variables that are pointing(or referencing) to this object.
108+
109+
Let's see some of its types:
110+
111+
1. **Class** : Every class is considered as user-defined data type as a user creates a class. In Java programming, a class is basically a user-defined data type that act as a template for creating objects of identical type. It represents the common properties and actions (functions) of an object.
112+
```java
113+
public class GitHub
114+
{
115+
// class body.
116+
}
117+
```
118+
2. **Object** : An object is any real-world thing that has some properties and actions. In other words, an entity that has state and behavior is known as an object. An Object has three characterstics - State, Behaviour and Identity.
119+
```java
120+
Person p = new Person();
121+
```
122+
3. **String** : A String is also a class. It represents a sequence of characters like Shuaib, A123, etc. The simplest way to create a string object is by storing sequence of characters into string type variable.
123+
```java
124+
String str = "India";
125+
```
126+
4. **Arrays** : It is an Object which is used to store multiple data of same data type. It can store Primitive as well as Non-Primitive data in it.
127+
```java
128+
int [] numbers;
129+
Student [] x; // Student is a name of class.
130+
```
131+
5. **Interface** : An interface is declared like a class but the only difference is that it contains only final variables and method declarations. It is a fully abstract class. It is blue print of a class. It is mechanism in Java to achieve abstraction and multiple inheritance. There can only be abstract methods and variables and no method body.
132+
```java
133+
interface printnum{
134+
void print();
135+
}
136+
```

0 commit comments

Comments
 (0)