Quick Enquiry Form

×

    EnquiryEnquire Now

    Quick Enquiry

      Data Types in Java

      Data types in java
      Blog

      Data Types in Java

      Data Types in Java play an important role in programming as it tells the compiler or interpreter how the developer uses variables. Generally, they are attributes to define the operations and what type of values can be stored in them. In this blog, we are explaining what are the various data types in Java along with examples. 

      Data Types in Java are classified into two categories as per the properties they possess. They are

      • Primitive Data Types
      • Non-Primitive Data Types

      Primitive Data Type: It is a pre-defined data type by Java Programming Language. The type and size of variable values don’t have additional methods as they are specified.

      Non-Primitive Data Type: It is a user-defined data type that can be created by the programmer. It is also known as “reference variables” or “object references” as it references a memory location for the stored data.

      Types of Primitive Data Types in JAVA

      Primitive Data Types in Java are classified into 8 categories as follows

      • Boolean Data Type
      • Byte Data Type
      • Char Data Type
      • Short Data Type
      • Int Data Type
      • Long Data Type
      • Float Data Type
      • Double Data Type

      Boolean Data Type: 

      It comprises a bit of information that can store only true or false values. It is used to track conditions whether it is true or false. Here is the example for Boolean Data Type

      class booleanDataType{

      public static void main(String args[]){

      // Setting the values for boolean data type

      boolean Java = true;

      boolean Python = false;

      System.out.println(Java); // Output will be true

      System.out.println(Python); // Output will be false

      }}

      Byte Data Type: 

      It is used to store whole numbers that are between -128 to 127. It is good for saving large amounts of memories as it is an 8-bit signed two’s complement integer. Following is the example

      class ByteExample {

      public static void main(String[] args) {

      byte n, a;

      n = 127;

      a=177;

      System.out.println(n); // prints 127

      System.out.println(a); // throws an error because it cannot store more than 127 bits

      }}

      Char Data Type: 

      It is used to store a single character and it should be enclosed within single quotes like ‘S’ or ‘s’. Below is the example for storing values in char data type.

      char example = ‘S’;

      char a = 65, b = 66, c = 67;

      System.out.println(example); // prints S

      System.out.println(a); // Displays 65

      System.out.println(b); // Displays 66

      System.out.println(c); // Displays 67

      Short Data Type:

      It is used to store values that are ranging from -32,768 to 32767 and the default size of the short data type is 2 bytes. The following is an example of the short data type.

      class ShortExample {

      public static void main(String[] args) {

      short n= 3435,

      System.out.println(n); // prints the value present in n i.e. 3435

      }

      }

      Int Data Type: 

      It is used to store integer numbers that are between -2147483648 to 2147483647. It is preferred widely by programmers for creating variables with a numeric value. An example follows

      int n = 9898234

      System.out.println(n); // prints 9898234

      Long Data Type:

      This is a 64-bit two’s complement integer with the default size as 64-bit and the values range from -263 to 263 -1.

      Example

      long n = 1223000L;

      System.out.println (n); // prints 1223000

      Float Data Type: 

      It is used to store fractional numbers that range from 3.4e-038 to 3.4e+038 and the value should end with ‘f’. The following is an example of a floating data type.

      float n = 98.23f;

      System.out.println(n); // prints 98.23

      Double Data Type:

      It is used to store fractional numbers that are between 1.7e-308 to 1.7e+308 and the value should end with ‘d’. Following is the example of a double data type.

      double n = 27.987d;

      System.out.println (n); //prints 27.987

      Non-Primitive Data Types

      Non-Primitive Data Types are known as reference data types. They include arrays, classes, strings, etc.

      Strings:

      It is a sequence of characters and is used by the programmer when storing more than one character in a variable.

      The Syntax for declaring a string: <String_Type> <string_variable> = “<sequence_of_strings>”;

      Example: 

      String s = “Softlogic”;

      String s1 = new String (“Softlogic”);

      Class:

      It is a user-defined prototype from which objects are created. It represents the collection of properties that are common to all objects of one type. It includes the following components:

      • Modifiers to declare the class is public or private access.
      • Class name to define the name of the class that should begin with a capital letter.
      • Superclass is the name of the parent class that is preceded by the keyword extends.
      • The interface is implemented by the class preceded by the keyword implements.
      • Body is the statements that are surrounded within braces { }.

      Arrays: 

      Arrays in Java programming language denote homogeneous data structures that contain more than one value of a similar Data Types in Java. It offers indexed access to the stored data for accessing a particular element by its index.

      Conclusion

      Learn briefly about the Data Types in Java by enrolling in our Java Training in Chennai as we offer an experimental-based learning environment for the students who begin their career in the software development field. Our Java Course Curriculum is designed meticulously as per the requirements of global industries along with hands-on practices.

      1
      Softlogic-Academy