Data types in java

 Data types in Java

Java has two categories of data:

  • Primitive Data Type: such as boolean, char, int, short, byte, long, float and double
  • Non-Primitive Data Type or Object Data type: such as String, Array, etc.
Primitive Data Type

Primitive data are only single values and have no special capabilities. 

 There are 8 primitive data types:

  1. boolean: boolean data type represents only one bit of information either true or false, but the size of boolean data type is virtual machine-dependent. Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.

    Syntax:

    boolean booleanVar;
    

    Size:

    virtual machine dependent
    

    Values:

    true, false
    

    Default Value:

    false
    
    filter_none

    edit

    play_arrow

    brightness_4

    // Java program to demonstrate boolean data type
      
    class college
        {
        public static void main(String args[])
        {
            boolean b = true;
            if (b == true)
                System.out.println("Hi");
        }
    }
    Output:
    Hi
     
  2. byte: The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays.

    Syntax:

    byte byteVar;
    

    Size:

    1 byte ( 8 bits )
    

    Values:

    -128 to 127
    

    Default Value:

    0
    
    filter_none

    edit

    play_arrow

    brightness_4

    // Java program to demonstrate byte data type in Java
      
    class college {
        public static void main(String args[])
        {
            byte a = 126;
      
            // byte is 8 bit value
            System.out.println(a);
      
            a++;
            System.out.println(a);
      
            // It overflows here because
            // byte can hold values from -128 to 127
            a++;
            System.out.println(a);
      
            // Looping back within the range
            a++;
            System.out.println(a);
        }
    }
    Output:
    126
    127
    -128
    -127
    
  3. short: The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.

    Syntax:

    short shortVar;
    

    Size:

    2 byte ( 16 bits )
    

    Values:

    -32, 768 to 32, 767 (inclusive)
    

    Default Value:

    0
     
  4. int: It is a 32-bit signed two’s complement integer.

    Syntax:

    int intVar;
    

    Size:

    4 byte ( 32 bits )
    

    Values:

    -2, 147, 483, 648 to 2, 147, 483, 647 (inclusive)
    

    Default Value:

    0
    

    Note: In Java SE 8 and later, we can use the int data type to represent an unsigned 32-bit integer, which has value in the range [0, 232-1]. Use the Integer class to use int data type as an unsigned integer. 

     

  5. long: The long data type is a 64-bit two’s complement integer.

    Syntax:

    long longVar;
    

    Size:

    8 byte ( 64 bits )
    

    Values:

    -9, 223, 372, 036, 854, 775, 808 
              to 
    9, 223, 372, 036, 854, 775, 807
          (inclusive)
    

    Default Value:

    0
    

    Note: In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. The Long class also contains methods like compare Unsigned, divide Unsigned etc to support arithmetic operations for unsigned long.

  6. float: The float data type is a single-precision 32-bit  floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers.

    Syntax:

    float floatVar;
    

    Size:

    4 byte ( 32 bits )
    

    Values:

    upto 7 decimal digits
    

    Default Value:

    0.0
     
  7. double: The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice.

    Syntax:

    double doubleVar;
    

    Size:

    8 byte ( 64 bits )
    

    Values:

    upto 16 decimal digits
    

    Default Value:

    0.0
    

    Note: Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended not to use these data types and use Big Decimal class instead.

  8. char: The char data type is a single 16-bit Unicode character.

    Syntax:

    char charVar;
    

    Size:

    2 byte ( 16 bits )
    

    Values:

    '\u0000' (0) to '\uffff' (65535)
    

    Default Value:

    '\u0000'
     
    Non-Primitive Data Type or Reference Data Types:- 
     The Reference Data Types will contain a memory address of variable value because the reference types won’t store the 
    variable value directly in memory. They are strings ,object,arrays etc.
     
    • String:
       Strings are defined as an array of characters. The difference between a
       character array and a string is the string is terminated with a special
       character ‘\0’.
      Syntax:
      <String_Type> <string_variable> = “<sequence_of_string>”;
    •  Example:
      // Declare String without using new operator 
      String s = "WDN"; 
      
      // Declare String using new operator 
      String s1 = new String("WDN"); 
    • Class:
    • A class is a user-defined blueprint or prototype from which objects are
       created.  It represents the set of properties or methods that are 
      common to all objects of one type. In general, class declarations can 
      include these components, in order:
       
    • Modifiers : A class can be public or has default access
    •  Class name: The name should begin with a initial letter (capitalized by convention). 
    • Superclass(if any): The name of the class’s parent 
      (superclass), if any, preceded by the keyword extends. A class can only 
      extend (subclass) one parent. 
    • Interfaces(if any): A comma-separated list of 
      interfaces implemented by the class, if any, preceded by the keyword 
      implements. A class can implement more than one interface. 
    • Body: The class body surrounded by braces, { }.  
    • Object:
       It is a basic unit of Object-Oriented Programming and represents the 
      real-life entities.  A typical Java program creates many objects, which 
      as you know, interact by invoking methods. An object consists of :
       
    • State : It is represented by attributes of an object. It also reflects the properties of an object. 
    • Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects. 
    • Identity : It gives a unique name to an object and enables one object to interact with other objects. 
    • Interface:-
       Like a class, an interface can have methods and variables, but the 
      methods declared in an interface are by default abstract (only method 
      signature, no body).  
       
    • Interfaces specify what a class must do and not how. It is the blueprint of the class. 
    • An Interface is about capabilities like a Player may be an 
      interface and any class implementing Player must be able to (or must 
      implement) move(). So it specifies a set of methods that the class has 
      to implement. 
    • If a class implements an interface and does not provide method 
      bodies for all functions specified in the interface, then class must be 
      declared abstract.  
    Array:-
    •  An array is a group of like-typed variables that are referred to by a 
      common name.Arrays in Java work differently than they do in C/C++. 
      Following are some important point about Java arrays.
       
    • In Java all arrays are dynamically allocated.(discussed below)
       
    • Since arrays are objects in Java, we can find their length 
      using member length. This is different from C/C++ where we find length 
      using sizeof.  
    • A Java array variable can also be declared like other variables with [] after the data type. 
    • The variables in the array are ordered and each have an index beginning from 0. 
    • Java array can be also be used as a static field, a local variable or a method parameter. 
    • The size of an array must be specified by an int value and not long or short. 
    • The direct superclass of an array type is Object.
      

    •  

Comments

Popular posts from this blog

Inheritance in Java

Type Conversions

Life cycle of a Thread and creating thread class