Major Language Classification

There are majorly two types of languages. 

  • First, one is Statically/strongly typed language where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types. Any type mismatch error should be correct before the compiling process
    Example: C, C++, Java.
  • The other is Dynamically typed languages. These languages can receive different data types over time. 
    Example: Ruby, Python

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier/variable .

Data Type Classification

  1. Primitive Data type
  2. Non-Primitive Data type

Primitive Data type

A primitive data type can be of eight types bytes, short,  int, long, char, float, and double.

Integer

This group includes byte, short, int, long

  1. byte
    • The byte data type is an 8-bit signed two’s complement integer. 
    • Syntax  byte bytevar = value ;
    • Example  byte a = 126;
  • Short
    • The short data type is a 16-bit signed two’s complement integer.
    • We use a short to save memory in large arrays, in situations where the memory savings actually matters.
    • Syntax  short shortVar;
    • Example short a=10;
  • Int
    • It is a 32-bit signed two’s complement integer.
    • Syntax  int intVar;
    • Example int  a=10;
  • Long
    • The long data type is a 64-bit two’s complement integer.
    •  Syntax  long longVar;
    • Example long  a=10;
package javaBasics.Datatypes;

public class byteExample
{
    public static void main(String[] args)
    {
//        This is byte example
        byte a=10;
//       This is short example
        short b=10;
//        This is int example
        int c=10;
//        This is long example
        long d=10;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

 

Floating-Point type

This group includes float, double

  • Float
    • The float data type is a single-precision 32-bit IEEE 754 floating-point.
    • Use a float (instead of double) if you need to save memory in large arrays of floating-point numbers.
    •  Syntax  float floatVar;
    • Example float  a=10.0f;
  • 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;
    • Example double  a=10.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 BigDecimal class instead. 

package javaBasics.Datatypes;

public class floatingpointExample
{
    public static void main(String[] args)
    {
//      this is float example
        float a=10.0f;
        System.out.println(a);
//      this is double example
        double b=11.0;
        System.out.println(b);

    }
}
//Outputs
//10.0
//11.0

boolean type

This group includes boolean

  • Boolean
    • Boolean data type represents only one bit of information either true or false, but the size of the boolean data type is virtual machine-dependent.
    •  Syntax  boolean booleanVar;
    • Example boolean  a=true;

package javaBasics.Datatypes;

public class booleanExample
{
    public static void main(String[] args)
    {
//         this boolean Example
        boolean a=true;
        boolean b=false;
        if(a==true && b==false)
        {
            System.out.println(a);
            System.out.println(b);
        }

    }
}
//Outputs
//true
//false
Character type

This group includes Char

  • Char
    •  The char data type is a single 16-bit Unicode character.
    •  Syntax  char charVar;
    • Example char  a= ‘codershandbook.in’;
package javaBasics.Datatypes;

public class characterExample
{
    public static void main(String[] args)
    {
//        this is character Examples
      char character='A';
        System.out.println(character);

    }
}
//Outputs
//A

Why is the size of char is 2 byte in java..? 

In other languages like C/C++ uses only ASCII characters and to represent all ASCII characters 8-bits is enough, But java uses the Unicode system not the ASCII code system and to represent Unicode system 8 bit is not enough to represent all characters so java uses 2 bytes for characters.

Unicode defines a fully international character set that can represent most of the world’s written languages. It is a unification of dozens of character sets, such as Latin, Greeks, Cyrillic, Katakana,  many more.

Non-Primitive Data type

  • The Non-Primitive Data type will contain a memory address of variable value because the reference types won’t store the variable value directly in memory. They are strings, objects, arrays, etc. 
  • String: Strings are defined as an array of characters.
  • Difference between char and string
    • String-string is designed to hold a sequence of characters in a single variable
    • Char- a character array is a collection of separate char type entities.
  • Syntax  String stringVar;
  • Example String  s= ‘codershandbook.in’;
package javaBasics.Datatypes;

public class stringExample
{
    public static void main(String[] args)
    {
//        this is string Examples
        String s="https://codershandbook.in";
        System.out.println(s);

    }
}
//Outputs
//https://codershandbook.in