» » » Data types in java with example programs

  • 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. 
  1. byte  
  2. short
  3. int
  4. long 
data types in java


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
Java example program on byte data type:

  1. package com.instanceofjava;
  2. Class ByteDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  byte x=10;
  7.  byte y=20;
  8.  
  9.   byte z= (byte) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 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:

  1. package com.instanceofjava;
  2. Class ShortDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  short x=10;
  7.  short y=20;
  8.  
  9.   short z= (short) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 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:

  1. package com.instanceofjava;
  2. Class IntDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9.   int z=x+y;
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 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:

  1. package com.instanceofjava;
  2. Class LongDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  long x=109876677777l;
  7.  long y=20666677766l;
  8.  
  9.  long z=x+y;
  10.  
  11.  System.out.println(z);
  12.  
  13. }

  14. }



Output:

  1. 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 
  1. float
  2. double


float data type in java

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:

  1. package com.instanceofjava;
  2. Class FloatDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  float x=231231.0031f;
  7.  float y= 53423423434.43231f;

  8.  float z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 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:

  1. package com.instanceofjava;
  2. Class DoubleDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  double x=231231.0031f;
  7.  double y= 53423423434.43231f;

  8.  double z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 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"
  1. 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:

  1. package com.instanceofjava;
  2. Class CharDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  char a='a';
  7.  System.out.println(a);
  8.  
  9. }

  10. }


Output:

  1. 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:

  1. package com.instanceofjava;
  2. Class BooleanDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. boolean x = false;
  7. System.out.println(x);
  8.  
  9. }

  10. }

Output:

  1. false

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu