Data Types in Java with Example

A data type in a programming language is a set of data with values having predefined characteristics. Examples of data types are the integer.

The language usually specifies the range of values for a given data type, how the values are processed by the computer, and how they are stored.

In Java, we have 2 types of Datatypes –

  1. Primitive Datatype
  2. Non-Primitive Datatype

DataTypes in Java

 

Primitive DataTypes

Primitive types are the data types defined by the language itself.  Datatypes are used to define the variable type. Like if we declare a variable like

int c ;  here int is datatype and c is the variable name.

 

Byte : byte data type is the first data type with least memory size allocated which can only be used for numbers of small ranges. The memory size allocated to byte is 8 bits. a byte can represent from 0 to 127 on the positive side.

On the negative side, it can represent the numbers from -1 to -128.

Example —   byte i = 50 ;

 

Short : Memory size allocated to short is 16 bits. On the positive side, it represents from 0 to 32767. On the negative side, it represents from -1 to -32768.

Example —   short i = 25000 ;

 

int : Memory size allocated is 32 bits. Represents a total of 4,294,967,296 numbers.

Example —   int i = 250000 ;

 

long : This data type is mostly used for huge values. Memory size allocated to long is 64 bits. When declaring the variable as long use “L” as a suffix.

Example —   long i = 250000L ;

 

float : This data type is used to represent the numbers with decimals.  Memory size allocated to float is 32 bits. When declaring the variable as long use “f” as the suffix.

Example —   float i = 32.00f ;

 

double : This data type is used to represent the numbers with decimals.  Memory size allocated to float is 64 bits. When declaring the variable as long use “d” as a suffix.

Example —   double i = 32000.00d ;

 

char : The char data type is used to represent the characters. Memory size allocated to char is 16 bits. Please note that a char can only hold one character at a time and should be quoted in single inverted commas.

Example —   char c = ‘A’ ;

 

boolean : This could be either true or false. By default boolean value is false.

Example —   boolean b = true ;

 

Non- Primitive DataTypes

Non-primitive data types are not defined by the programming language but are instead created by the programmer. They are sometimes called “reference variables,” or “object references” since they reference a memory location, which stores the data. In the Java programming language, non-primitive data types are simply called “objects” because they are created, rather than predefined.

 

String  : String is a sequence of characters. In the Java programming language, strings are objects. (In future tutorials we will discuss this in details)

 

Array  : An array objects that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

 

Selenium Testing Video:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply