Java-Data Types-example

Variables reserved memory locations to store values. This means that when you create a variable you reserve some space in the 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 types of data types in Java:

Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
Non-primitive data types: he non-primitive data types include Classes, Interfaces, and Arrays.


Primitive data

Primitive data are only single values; they have no special capabilities. There are 8 primitive data types




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


// A Java program to demonstrate boolean data type 
class KnowledgeFactoryDemo {
public static void main(String args[]) {
boolean b = true;
if (b == true)
System.out.println("Hi KnowledgeFactory");
}
}

Output:Hi KnowledgeFactory



byte:

  • Byte data type is an 8-bit signed two's complement integer
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -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 integer.


// Java program to demonstrate byte data type in Java 


class KnowledgeFactoryDemo {

public static void main(String args[]) {

byte count = 125;

                // byte is 8 bit value

System.out.println(count );

count ++;

System.out.println(count );

}

}

Output:

           125
           126


short:

  • Short data type is a 16-bit signed two's complement integer
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (2^15 -1)
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer
  • Default value is 0.
short count = 125;


int:


  • Int data type is a 32-bit signed two's complement integer.
  • Minimum value is - 2,147,483,648 (-2^31)
  • Maximum value is 2,147,483,647(inclusive) (2^31 -1)
  • Integer is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0
int count = 125;


long:

  • Long data type is a 64-bit signed two's complement integer
  • Minimum value is -9,223,372,036,854,775,808(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
  • This type is used when a wider range than int is needed
  • Default value is 0L
long count = 1258767865644L; 


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
float count = 125232.233f;


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
double count = 125233434342.233d;


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
char alphabet='g';


Non-Primitive Data Types

Let’s now understand these non-primitive data types in short.

Strings: String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. If you wish to know more about Java Strings, you can refer to this article on Strings in Java.

Arrays: Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index. If you wish to learn Arrays in detail, then kindly check out this article on Java Arrays.

Classes: A class in Java is a blueprint which includes all your data.  A class contains fields(variables) and methods to describe the behavior of an object.


Interface: Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body).



This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
                                                                                                        Thank U!

Comments