print
Advertisment
Advertisment

data types in java

definition

Java is a strongly-typed programming language that requires all variables to have a declared data type. There are two categories of data types in Java: primitive data types and reference data types.

types of data type

1. primitive data type

2. non-primitive or Reference data type


1. primitive data type

Primitive data types are the most basic data types in Java and are built into the language. There are eight primitive data types in Java, each with a different range and precision:

Primitive Data Types

Data Type Description Size Range
Byte A signed 8-bit integer 8 bits -128 to 127
Short A signed 16-bit integer 16 bits -32,768 to 32,767
Int A signed 32-bit integer 32 bits -2,147,483,648 to 2,147,483,647
Long A signed 64-bit integer 64 bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Double A 32-bit floating point number 64 bits Upto 16 decimal digits
Float A 64-bit floating point number 32 bits uPTO 7 DECIMAL DIGITS
Char A single 16-bit Unicode character 16 bits '\u0000' to '\uffff'
Boolean A true/false value 1 bit true, false

PrimitiveExample.java

public class PrimitiveExample {
   public static void main(String[] args) {
      // declaring and initializing primitive data types
      byte myByte = 100;
      short myShort = 5000;
      int myInt = 123456;
      long myLong = 123456789012345L;
      float myFloat = 3.14159f;
      double myDouble = 2.71828;
      char myChar = 'A';
      boolean myBoolean = true;

      // printing values of primitive data types
      System.out.println("byte value: " + myByte);
      System.out.println("short value: " + myShort);
      System.out.println("int value: " + myInt);
      System.out.println("long value: " + myLong);
      System.out.println("float value: " + myFloat);
      System.out.println("double value: " + myDouble);
      System.out.println("char value: " + myChar);
      System.out.println("boolean value: " + myBoolean);
   }
}

Output :

byte value: 100
short value: 5000
int value: 123456
long value: 123456789012345
float value: 3.14159
double value: 2.71828
char value: A
boolean value: true

2. Non-primitive or Reference Data Types

Reference data types are used to refer to objects. In Java, the reference data type is the class. There are many built-in classes in Java, such as String, Math, and Integer. Here are some of the commonly used reference data types in Java:

Non-Primitive/Reference Data Types

Data Type Description
String A sequence of characters
Array A group of elements of the same data type that are stored in contiguous memory locations
Class A blueprint for creating objects
Interface A collection of abstract methods used to achieve abstraction and multiple inheritance
Enumeration A data type that consists of a set of named constants

PrimitiveExample.java

public class ReferenceExample {
   public static void main(String[] args) {
      // declaring and initializing reference data types
      String myString = "Hello, World!";
      int[] myArray = {1, 2, 3, 4, 5};
      MyClass myObject = new MyClass();

      // printing values of reference data types
      System.out.println("String value: " + myString);
      System.out.println("Array value: " + Arrays.toString(myArray));
      System.out.println("Object value: " + myObject);
   }
}

class MyClass {
   // class implementation goes here
}

Output :

String value: Hello, World!
Array value: [1, 2, 3, 4, 5]
Object value: MyClass@5e481248

Conclusion

Data types in Java are essential building blocks of any program. Understanding them is essential to writing efficient and effective code. Primitive data types are used to represent basic values such as numbers, characters, and boolean values. Reference data types are used to represent more complex objects and data structures. By using the appropriate data types, Java developers can create robust, efficient, and flexible programs.

Every variable has a type, every expression has a type, and all types are strictly defined more every assignment should be checked by the compiler for type compatibility hence java language is considered a strongly typed programming language.

Advertisment

Java is pure object-oriented programming or not?

Java is not considered a pure object-oriented programming language because several oops features (like multiple inheritances, and operator overloading) are not supported by java moreover we are depending on primitive data types which are non-objects. Diagram:

Details in data types in java

Note: Except Boolean and char all remaining data types are considered signed data types because we can represent both "+ve" and"-ve" numbers.

numeric data types in java

Integral data types in java

byte in java:

Note: Byte is a primitive data type it has a memory of 1 byte(8 bits) and its range is from -128 to +127.

	
Size: 1byte (8bits)
Maxvalue: +127
Minvalue: -128
Range: -128to 127[-2? to 2?-1]	
Example: byte b = 15;	
	

Example:

ExampleForByte.java
public class ExampleForByte{
    public static void main(String[] args) {
        byte b = 15;
        byte d = 197;/*it will throw an error because the value of d is more than 127
        and we can store max value of 127 in byte.
        error type:- java: incompatible types: possible lossy conversion from int to byte
         */
    }
}

short:

Note: Short is a primitive data type .it is the most rarely used data type. It has a memory of 2 bytes and in this data type, we can store values from -32768 to +32767.

	
Size: 2 bytes
Range: -32768 to 32767(-2¹? to 2¹?-1)
Example: short s = 10000;
	

Example:

ExampleForShort.java
public class ExampleForShort{
    public static void main(String[] args) {
        short s = 32171;//it comes under the range of short
        short r = 32982;/*it will throw an error because the value of r is more than 32767
                        and we can store max value of 127 in byte.
                         error type:- java: incompatible types: possible lossy conversion from int to short
                        */
    }
}

Note: Short data type is best suitable for 16-bit processors like 8086 but these processors are completely outdated and hence the corresponding short data type is also out data type.

int in java:

Note: Int is a primitive data type used to store numeric values. It is the most commonly used data type .its memory size is of 4 bytes. it has values from -2147483648 to +2147483647.

	
Size: 4 bytes
Range: -2147483648 to 2147483647 (-2³¹ to 2³¹-1)
Example: int i = 178;
	

Example:

ExampleForInt.java
public class ExampleForInt{
    public static void main(String[] args) {
        int i = 29886568; // it is in range of int
        int j =3756345983;   /*it has valur of more than 2147483647 so it is out of int's range and
        will throw error
        error type:- java: integer number too large: 3756345983*/
    }
}

long in java:

Note: long is a primitive data type. Its memory size is 8 bytes. it can store value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Whenever it is not enough to hold big values then we should go for the long data type.

	
Size: 8 bytes
Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-2?³ to 2?³-1)
Example: long l = 1987638898L;

Example:

ExampleForLong.java
public class ExampleForLong{
    public static void main(String[] args) {
        long z = 1333464764342l;
    }
}
Advertisment

Note: We need to write l behind the number whenever we are storing value for long otherwise it will throw an error.

Floating Point Data types in java

Note: All the above data types (byte, short, int, and long) can be used to represent whole numbers. If we want to represent real numbers then we should go for floating point data types.

float in java

Note: A float data type is used to store a value of 5 to 6 numbers after the decimal. we can use float whenever we need to store any decimal value. We can store values up to 5 to 6 numbers after the decimal. It has a memory of 4 bytes. Its range is from 3.4e-038 to 3.4e+038.

	
Size: 4 bytes
Range: 3.4e-038 to 3.4e+038
Example: float f = 1.5F;
	

Note: float follows single precision.

Example:

ExampleForFloat.java
public class ExampleForFloat{
    public static void main(String[] args) {
        float m = 56.76888f; //it will not throw error and is correct form to use float.
        float n = 56.76888;  //it will throw error.
    }
}

Note: We need to write f behind the number whenever we are storing value in float otherwise it will throw an error.

double in java

Note: A double data type is used to store a value of 11 to 12 numbers after the decimal.

	
Size: 8 bytes
Range: 1.7e-308 to 1.7e+308
Example: double d = 15.78977679d;
	

Note: double follows double precision.

Example:

ExampleForDouble.java
public class ExampleForDouble{
    public static void main(String[] args) {
        double r = 21543.76435435894745d;  // it is correct form  to use double
        double s = 38443.45624385350947; // it will throw error 
        System.out.println(r);
        System.out.println(s);
    }
}

Note: We need to write d behind the number whenever we are storing value in double otherwise it will throw an error.

Non-numeric data types

char in java

Note: In old languages like C & C++ ASCII code based on the no. Of ASCII code characters are < 256 to represent these 256 characters 8 - bits is enough hence char size in old languages 1 byte
In java, we are allowed to use any worldwide alphabet characters and java is Unicode based and no. Of Unicode characters are > 256 and <= 65536. so to represent all these characters one byte is not enough so we should go for 2 bytes.

	
Size: 2 bytes
Range: 0 to 65535
	

boolean in java

	
Size: 1 bit
Range: Not applicable but allowed values are true or false.
	

Which of the following boolean declarations are valid?

DataTypesExample.java
class DataTypesExample {
	public static void main(String[] args) {
		boolean b1 = true;
		boolean b2 = True;//C.E:cannot find symbol
		boolean b3 = "true";//C.E:incompatible types
		boolean b4 = 0;//C.E:incompatible types
	}
}

Summary of java primitive data type:

	
The default value for the object references is "null". 
	

Advertisment
Advertisment
arrow_upward