Core Java -Datatypes

Variables:

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java:
  • Primitive Data Types
  • Non-Primitive Data Types

  • Primitive Data Types:

    There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.
    byte:
  • Byte data type is an 8-bit signed two's complement integer.
  • Minimum value is -128 (-2 7)
  • Maximum value is 127 (inclusive)(27 -1)
  • Default value is 0
  • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
  • Eg: byte a = 100, byte b = -50

    short:
  • Short data type is a 16-bit signed two's complement integer
  • Minimum value is -32,768 (-215)
  • Maximum value is 32,767(inclusive) (215 -1)
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
  • Default value is 0.
  • Eg: short s= 10000, short r = -20000

    int:
  • int data type is a 32-bit signed two's complement integer.
  • Minimum value is - 2,147,483,648.(-231)
  • Maximum value is 2,147,483,647(inclusive).(231 -1)
  • Int is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0.
  • Eg: int a = 100000, int b = -200000

    long:
  • Long data type is a 64-bit signed two's complement integer.
  • Minimum value is -9,223,372,036,854,775,808.(-263)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive). (263 -1)
  • This type is used when a wider range than int is needed.
  • Default value is 0L.
  • Eg: int a = 100000L, int b = -200000L

    float:
  • Float data type is a single-precision 32-bit IEEE 754 floating point.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Default value is 0.0f.
  • Float data type is never used for precise values such as currency.
  • Eg: float f1 = 234.5f

    double:
  • double data type is a double-precision 64-bit IEEE 754 floating point.
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • Double data type should never be used for precise values such as currency.
  • Default value is 0.0d.
  • Eg: double d1 = 123.4

    boolean:
  • 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.
  • Eg: boolean one = true

    char:
  • char data type is a single 16-bit Unicode character.
  • Minimum value is '\u0000' (or 0).
  • Maximum value is '\uffff' (or 65,535 inclusive).
  • Char data type is used to store any character.
  • Eg: char letterA ='A'

    Non-Primitive Data Types:

    Unlike primitive data types, these are not predefined. These are user-defined data types created by programmers. These data types are used to store multiple values. For example, consider an array that stores a group of values.
    Class is also a primitive type that stores different methods and variables. Therefore, these are also called as advanced data types in Java. Whenever a non-primitive data type is defined, it refers a memory location where the data is stored in heap memory i.e., it refers to the memory location where an object is placed. Therefore, a non-primitive data type variable is also called referenced data type or simply object reference variable.
    An object reference variable lives on the stack memory and the object to which it points always lives on the heap memory. The stack holds a pointer to the object on the heap.In Java programming, all non-primitive data types are simply called objects that are created by instantiating a class.
    Types of Non-primitive data types:
    Types of non-primitive data types in Java are as follows:
  • String
  • Array

  • String:
    A string represents a sequence of characters.String is the class of Java.
    One of the ways to create a string and store a value in it is shown below:
        String str = "You're the best";  
    
    Eg:
        public class StringExample {  
            public static void main(String[] args) {  
                  
                // creating a string and initializing it  
                String str = "Hello! This is example of String type";  
                  
                // applying substring() on above string  
                String subStr = str.substring(0,14);  
          
                // printing the string  
                System.out.println(subStr);  
            }  
        }  
    
    Output:
    Hello! This is

    Array:
    An array is a data type which can store multiple homogenous variables i.e., variables of same type in a sequence. They are stored in an indexed manner starting with index 0. The variables can be either primitive or non-primitive data types.
    Below example shows how to declare array of primitive data type int:
        int [ ] marks
    
    
    Eg:
        import java.io. * ;  
        import java.util. * ;  
        
        public class ArrayExample {  
        public static void main(String[] args) throws IOException {  
            int i;  
            Scanner sc = new Scanner(System. in );  
            // declaring and initializing an array  
            int arr[] = {1, 2, 3, 6, 9};  
            // defining another array arr1  
            int arr1[] = new int[5];  
            // reading values from the user  
            System.out.println("Enter the numbers (size = 5) :");  
            for (i = 0; i < 5; i++) {  
            arr1[i] = sc.nextInt();  
            }  
            System.out.println("Previous array with initialized size is: ");  
            for (i = 0; i < 5; i++) {  
            System.out.print(arr[i] + " ");  
            }  
            System.out.println("\nThe new array we have entered is:");  
            for (i = 0; i < 5; i++) {  
            System.out.print(arr1[i] + " ");  
            }  
        }  
        }  
    
    
    Output:
    Enter the numbers (size = 5) :
    56
    43
    22
    1
    7
    Previous array with initialized size is:
    1 2 3 6 9
    The new array we have entered is:
    56 43 22 1 7


    (Core Java - Constraints)