» » Java data types with examples


  1. Primitive data types or Fundamental data types.
  2. Referenced data types or Derived data types.



1.Primitive Data types


1.byte

  1. package com.instanceofjava;
  2.  
  3. public class ByteDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         byte a=10; // a is a variable of type byte holding the value 1;
  8.         byte b=20;// b is a variable of type byte holding the value 2;
  9.         
  10.         byte c= (byte)(a+b);
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 3

2.short

  1. package com.instanceofjava;
  2.  
  3. public class ShortDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         short a=1;
  8.         short b=2;
  9.         
  10.         short c= (short)(a+b);
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 3

3.int

  1. package com.instanceofjava;
  2.  
  3. public class IntDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         int a=10; // a is a variable of type integer holding the value 10;
  8.         int b=20;// b is a variable of type integer holding the value 20;
  9.         
  10.         int c= a+b;
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 30


4.long

  1. package com.instanceofjava;
  2.  
  3. public class LongDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         long a=1234567890;
  8.         long b=1234567890;
  9.  
  10.         long c= a+b;
  11.  
  12.         System.out.println(c);
  13.  
  14.     }
  15. }

Output:

  1. 2469135780


5.float

  1. package com.instanceofjava;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         float a=1234567.8f;
  8.         float b=1234567.8f;
  9.  
  10.         float c= a+b;
  11.  
  12.         System.out.println(c);
  13.  
  14.     }
  15. }

Output:

  1. 2469135.5


7.double

  1. package com.instanceofjava;
  2.  
  3. public class DoubleDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         double a=1234567910112181314151634444422233334491.1d;
  8.         double b=1234567910112181314151634444422233334491.8d;
  9.         double c= a+b;
  10.         System.out.println(c);
  11.  
  12.     }
  13. }

Output:

  1. 2.4691358202243627E39



8.char

  1. package com.instanceofjava;
  2.  
  3. public class CharDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         char a='A';
  8.         char b= 'B';
  9.  
  10.       System.out.println(a);
  11.       System.out.println(b);
  12.       System.out.println(a+b);
  13.  
  14.     }
  15. }

Output:

  1. A
  2. B
  3. 131


9.boolean

  1. package com.instanceofjava;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         boolean a=true;
  8.         boolean b= false;
  9.  
  10.       System.out.println(a);
  11.       System.out.println(b);
  12.   
  13.  
  14.     }
  15. }

Output:

  1. true
  2. false

1.Referenced Data types

1.class


  1. package com.instanceofjava;
  2.  
  3. public class Sample{
  4.  int a,b; 
  5.  
  6. void add(){
  7. System.out.println(a+b);
  8. }

  9. public static void main(String[] args) {
  10.  
  11.        Sample obj= new Sample();
  12.  
  13.        obj.a=10;
  14.        obj.b=12;

  15.        obj.add();
  16.  
  17.     }
  18. }

Output:

  1. 22

2.Array

  1. package com.instanceofjava;
  2.  
  3. public class ArrayDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         int a[]={1,2,3,4,5,6};
  8.  
  9.         for (int i = 0; i < a.length; i++) {
  10.             System.out.println(a[i]);
  11.         }
  12.   
  13.  
  14.     }
  15. }

Output:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6


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