Basic Data Types in Java with Example | Java Primitive and Non-Primitive Data Types with Syntax

Data types define the values and sizes that can be stored in a variable. Basically, there are two types of data types in java i.e., Primitive data types and Non-Primitive data types. In this tutorial, we will discuss completely the Java Datatypes with Examples. Let’s grab the information on basic data types in java with Examples via direct links available here:

Basic Data Types in Java

Java is strongly or we can say that statically typed language that means the data type of a variable is known at compile time. This means that you must declare the variable before they can use it.

In the previous tutorial, we learned about the variable, We learned how to create and declare a variable. In this tutorial, we will learn about the basic data types in java.

There are two types of data types in Java:

  1. Primitive data types.
  2. Non-primitive data types.

Basic Data Types in Java with Example 1

Read More: 

Java Primitive Data Types

In Java, there are 8 basic primitive data types: boolean, byte, char, short, int, long, float, double. These data types can be put into 4 groups.

  • Integer: This group includes byte, short, int, long.
  • Floating-point: This group includes float and double.
  • Character: This group includes char value.
  • Boolean: This group includes boolean values.
Data Type Default Value Default size
boolean false 1 bit
char ‘\u0000’ 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

1. boolean data type

The boolean data type has only two possible values either true or false. We can use this data type for simple flags that can track true/false conditions. This data type represents the one-bit information but the size of the boolean data type is dependent on a virtual machine.

Example of boolean data type:

class JavaStudyPoint {
public static void main(String args[])
{
boolean b = true;
if (b == true)
System.out.println("Hi programmers!");
}
}

Output:

Hi Programmers!

2. byte data type

The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays. They are also useful when you are working with a stream of data from a network or a file.

Example of byte data type:

class JavaStudyPoint {
public static void main(String args[])
{
byte b = 126;

// byte is 8 bit value
System.out.println(b);

b++;
System.out.println(b);

// It overflows here because
// the range of byte from -128 to 127
b++;
System.out.println(b);

// Looping back within the range
b++;
System.out.println(b);
}
}

Output:

126
127
-128
-127

3. short data type

The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

Example of short data type:

class JavaStudyPoint {
public static void main(String[] args){
short num; //short data type.
num = 15000;
System.out.println(num);
}
}

Output:

15000

4. int data type

The most commonly used integer data type is int. It is a signed 32-bit type that has a range from -2,147,483,648 to 2,147,483,647. The variables of type int are commonly used to control loops and to index arrays.

Example of int data type:

class JavaStudyPoint {
public static void main(String[] args){
int num;
num = 15000000;
System.out.println(num);
}
}

Output:

15000000

5. long data type

The long is a signed 64-bit and is useful for those occasions where an int type is not large enough to hold the desired value. The range of long type is -2^63 to 2^63-1. Its minimum value is -2^63 and the maximum value is 2^63-1. This data type is useful when big, whole numbers are needed.

Example of long data type:

In this example, we compute the number of miles that light will travel in a specified number of days.

class JavaStudyPoint{
public static void main(String args[]){
int lightspeed;
long days;
long seconds;
long distance;

//approximate speed of light in miles per second.
lightspeed = 186000;
days = 1000; // specify number of days.
seconds = days * 24 * 60 * 60 //convert to seconds
distance = lightspeed * seconds // compute distance

System.out.println("In " +days);
System.out.println(" days light will travel about ");
System.out.println(distance + " miles. ");
}
}

Output:

In 1000 days light will travel about 16070400000000 miles.

6. float data type

The float data type is a single-precision 32-bit IEEE 754 floating-point. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. This data type should never be used for precise values, such as currency.

Example of float data type:

class JavaStudyPoint {
public static void main(String[] args) {
float num;
num = 20.67f;
System.out.println(num);
}
}

Output:

20.67

7. double data type

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. As mentioned above, this data type should never be used for precise values, such as currency.

Example of double data type:

In this example, we can use a double data type to compute the area of a circle.

class JavaStudyPoint{
public static void main(String args[]){
double pi, r, a;

r = 10.8; // radius of circle.
pi = 3.14 // value of pi.
a = pi * r * r; // compute area.
System.out.println("Area of a circle is: " +a);
}
}

Output:

Area of a circle is: 366.2496

8. char data type

The char data type is used to store the character value. The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).

Example of char data type:

class JavaStudyPoint{
public static void main(String args[]){
char ch1, ch2;

ch1 = 88; // code for x;
ch2 = 'Y';

System.out.println("ch1 and ch2: ");
System.out.prinytln(ch1 + " " +ch2);
}
}

Output:

ch1 and ch2: X Y.

Java Non-Primitive Data Types

Actually, Non-primitive data types in java are specified by the programing languages but designed by the programmer. Also, these data types are known as “Reference Variables” or “Object References” because they reference a memory location that saves the data.

Strings, Arrays, Classes, Interfaces, etc. are the non-primitive or reference data types in Java. Let’s move further and discuss the details of java non-primitive data types.

1. String Type

In Java, a string is a sequence of characters. To represent the Java Strings, we use double-quotes. Also, Java gives support for character strings via java.lang.String class. Here, we have given the syntax of the string data type:

String myString = "Java Programming";

Example of java string data type:

class Main {
  public static void main(String[] args) {
    
    // create strings
    String first = "Java";
    String second = "Python";
    String third = "JavaScript";

    // print strings
    System.out.println(first);   // print Java
    System.out.println(second);  // print Python
    System.out.println(third);   // print JavaScript
  }
}

2. Array Type

Java array is an object which includes elements of a similar data type. Arrays in Java are homogeneous data structures executed in Java as objects. Array in Java is index-based, the first element of the array is stored at the 0th index, the 2nd element is stored on the 1st index, and so on.

The syntax of declaring an Array in Java is as follows:

dataType[] arr; (or) 
dataType []arr; (or) 
dataType arr[];

Java Array Example:

Just have a look at the following simple example of a java array, where we are going to declare, instantiate, initialize and traverse an array.

//Java Program to illustrate how to declare, instantiate, initialize, and traverse the Java array.  
class Testarray{  
public static void main(String args[]){  
int a[]=new int[5];//declaration and instantiation  
a[0]=10;//initialization  
a[1]=20;  
a[2]=70;  
a[3]=40;  
a[4]=50;  
//traversing array  
for(int i=0;i<a.length;i++)//length is the property of array  
System.out.println(a[i]);  
}}

Output:

10
20
70
40
50