Data Types (Part 5)

Data Types

Whenever any variable is declared , it is declared with a data-type

eg- int a , String str 

Data types are divided into two groups:

  • Primitive Data type include byteshortintlongfloatdoubleboolean and char
  • Non-primitive data types - such as String, Arrays and Classes    
The main difference between primitive and non-primitive data types are:
  • Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String).
  • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type has always a value, while non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
  • The size of a primitive type depends on the data type, while non-primitive types have all the same size.

Eg:-
 public class MyClass {   
    public static void main(String[] args) {   
       int num1 = 5;   
       float num2 = 5.6f;   
       double num3 = 6.7777;   
       boolean bool = true;   
       char a = 67;   
       String str = "Hello";   
       System.out.println(num1);   
       System.out.println(num2);   
       System.out.println(num3);   
       System.out.println(bool);   
       System.out.println(a);   
       System.out.println(str);   
    }   
  } 
Output:-
  5   
  5.6   
  6.7777   
  true   
  C   
  Hello 



< Previous Next >