- The data type is something which gives information about
- Size of the memory location and range of the data that can be accommodated inside that location.
- Possible legal operations those can be performed on that location.
- What type of result come out from an expression when we use these types inside that expression.
- Whichever the keyword gives these semantics is treated as "data type".
- The java language provides different categories of data types.
1.Primitives Data types:
- The primitive data types are the predefined data types given by the programming language and they are meant for storing a single value.
- Based on the type and range of data, primitive types are divided into 8 types.
1.Integer category:
- This category can be used for storing numbers which can be either positive value or negative value without decimal points.
- In this category we have 4 primitive data types whose memory sizes are different.
- All the 4 types under this category are used for storing same data. But there ranges are different. The java language is providing 4 types under Integer category. So that the memory is utilized efficiently.
- byte
- short
- int
- long
byte data type in java:
- Size 1 byte. i.e 8 bits
- Minimum value is -128 (-2^7)
- Maximum value is 127 (2^7 -1)
- Default value is 0
- package com.instanceofjava;
- Class ByteDemo {
- public static void main(String[] args) {
- byte x=10;
- byte y=20;
- byte z= (byte) (x+y);
- System.out.println(z);
- }
- }
Output:
- 30
short data type in java:
- Size 2 bytes.
- Minimum value is -32,768 (-2^15)
- Maximum value is 32,767 (2^15-1)
- Default value is 0
Java example program on short data type:
- package com.instanceofjava;
- Class ShortDemo {
- public static void main(String[] args) {
- short x=10;
- short y=20;
- short z= (short) (x+y);
- System.out.println(z);
- }
- }
Output:
- 30
int data type in java:
- Size 4 bytes.
- Minimum value is - 2,147,483,648.(-2^31)
- Maximum value is 2,147,483,647 (2^31 -1)
- Default value is 0
Java example program on int data type:
- package com.instanceofjava;
- Class IntDemo {
- public static void main(String[] args) {
- int x=10;
- int y=20;
- int z=x+y;
- System.out.println(z);
- }
- }
Output:
- 30
long data type in java:
- Size 8 bytes.
- Minimum value is -9,223,372,036,854,775,808.(-2^63)
- Maximum value is 9,223,372,036,854,775,807 (2^63 -1)
- Default value is 0
Java example program on long data type:
- package com.instanceofjava;
- Class LongDemo {
- public static void main(String[] args) {
- long x=109876677777l;
- long y=20666677766l;
- long z=x+y;
- System.out.println(z);
- }
- }
Output:
- 130543355543
2.Floating point category:
- This category can be used for storing numbers which can be either +VE or –VE with decimal point. In the floating point category we have two types whose size is different. The two data types are float and double.
- Both the data types under the floating point category are used for storing same data but there range is different. The java language provide as two data types under floating point category so that memory is utilized efficiently.
- Default value of float is 0
- Default value of double is 0.0
- float
- double
float data type in java:
- Size 4 bytes.
- Minimum value is 1.4e-45
- Maximum value is 3.4e38
- Default value is 0.0f
Java example program on float data type:
- package com.instanceofjava;
- Class FloatDemo {
- public static void main(String[] args) {
- float x=231231.0031f;
- float y= 53423423434.43231f;
- float z= x+y;
- System.out.println(z);
- }
- }
Output:
- 5.3423653E10
double data type in java:
- Size 8 bytes.
- Minimum value is 4.9e-324
- Maximum value is 1.8e308
- Default value is 0.0d
Java example program on double data type:
- package com.instanceofjava;
- Class DoubleDemo {
- public static void main(String[] args) {
- double x=231231.0031f;
- double y= 53423423434.43231f;
- double z= x+y;
- System.out.println(z);
- }
- }
Output:
- 5.342365466543541E10
3.Character category:
- This category can be used for storing a single character. A character can be represented by alphabets, a digit and special symbols.
- This category contains only one data type an it is char.
- Default value of char is "one space"
- char
char data type in java:
- Size 2 bytes.
- Minimum value is 0
- Maximum value is 65,535
- Default value is ' '
Java example program on char data type:
- package com.instanceofjava;
- Class CharDemo {
- public static void main(String[] args) {
- char a='a';
- System.out.println(a);
- }
- }
Output:
- a
4.Boolean category:
- This category can be used for storing either true or false. Under the Boolean category only one data type an it is Boolean. The size is dependent on JVM to JVM.
boolean data type in java:
- boolean data type represents one bit of information.
- There are only two possible values: true and false.
- This data type is used for simple flags that track true/false conditions.
- Default value is false. '
Java example program on boolean data type:
- package com.instanceofjava;
- Class BooleanDemo {
- public static void main(String[] args) {
- boolean x = false;
- System.out.println(x);
- }
- }
Output:
- false
No comments