- In java mainly we have two types of data types.
- Primitive data types or Fundamental data types.
- Referenced data types or Derived data types.
1.Primitive Data types
1.byte
- package com.instanceofjava;
- public class ByteDemo {
- public static void main(String[] args) {
- byte a=10; // a is a variable of type byte holding the value 1;
- byte b=20;// b is a variable of type byte holding the value 2;
- byte c= (byte)(a+b);
- System.out.println(c);
- }
- }
Output:
- 3
2.short
- package com.instanceofjava;
- public class ShortDemo {
- public static void main(String[] args) {
- short a=1;
- short b=2;
- short c= (short)(a+b);
- System.out.println(c);
- }
- }
Output:
- 3
3.int
- package com.instanceofjava;
- public class IntDemo {
- public static void main(String[] args) {
- int a=10; // a is a variable of type integer holding the value 10;
- int b=20;// b is a variable of type integer holding the value 20;
- int c= a+b;
- System.out.println(c);
- }
- }
Output:
- 30
4.long
- package com.instanceofjava;
- public class LongDemo {
- public static void main(String[] args) {
- long a=1234567890;
- long b=1234567890;
- long c= a+b;
- System.out.println(c);
- }
- }
Output:
- 2469135780
5.float
- package com.instanceofjava;
- public class FloatDemo {
- public static void main(String[] args) {
- float a=1234567.8f;
- float b=1234567.8f;
- float c= a+b;
- System.out.println(c);
- }
- }
Output:
- 2469135.5
7.double
- package com.instanceofjava;
- public class DoubleDemo {
- public static void main(String[] args) {
- double a=1234567910112181314151634444422233334491.1d;
- double b=1234567910112181314151634444422233334491.8d;
- double c= a+b;
- System.out.println(c);
- }
- }
Output:
- 2.4691358202243627E39
8.char
- package com.instanceofjava;
- public class CharDemo {
- public static void main(String[] args) {
- char a='A';
- char b= 'B';
- System.out.println(a);
- System.out.println(b);
- System.out.println(a+b);
- }
- }
Output:
- A
- B
- 131
9.boolean
- package com.instanceofjava;
- public class BooleanDemo {
- public static void main(String[] args) {
- boolean a=true;
- boolean b= false;
- System.out.println(a);
- System.out.println(b);
- }
- }
Output:
- true
- false
1.Referenced Data types
1.class
- package com.instanceofjava;
- public class Sample{
- int a,b;
- void add(){
- System.out.println(a+b);
- }
- public static void main(String[] args) {
- Sample obj= new Sample();
- obj.a=10;
- obj.b=12;
- obj.add();
- }
- }
Output:
- 22
2.Array
- package com.instanceofjava;
- public class ArrayDemo {
- public static void main(String[] args) {
- int a[]={1,2,3,4,5,6};
- for (int i = 0; i < a.length; i++) {
- System.out.println(a[i]);
- }
- }
- }
Output:
- 1
- 2
- 3
- 4
- 5
- 6
No comments