Data Types

Data Types

  • Data Type is used to store different types of values in variables.
  • Example
    • int myNum = 5;               // Integer (whole number)
    • float myFloatNum = 5.99f;    // Floating point number
    • char myLetter = 'D';         // Character
    • boolean myBool = true;       // Boolean
    • String myText = "Hello";     // String
  • Data types are divided into two groups:

1. Primitive data types - includes byte, short, int, long, float, double, boolean and char
2. Non-primitive data types - such as String, Arrays and Classes.

Primitive Data Types

  • A primitive data type specifies the size and type of variable values, and it has no additional methods.
  • Numbers
    • Primitive number types are divided into two groups:
      • (a) Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals.
        • Example: byte, short, int and long.
      • (b) Floating-point types represents numbers with a fractional part, containing one or more decimals.
        • Example: float and double.
  • 1. Byte:
    • Syntax: byte variable_name;
      • Example: byte myNum = 100;
      • System.out.println(myNum);
    • Characteristics:
      • Size: 1 byte
      • Capacity: Stores whole numbers from -128 to 127
  • 2. Short:
    • Syntax: short variable_name;
      • Example: short myNum = 5000;
      • Syste.out.println(myNum);
    • Charateristics:
      • Size: 2byte
      • Capacity: Stores whole numbers from -32,768 to 32,767
  • 3. Int:
    • Syntax: int variable_name;
      • Example: int myNum = 100000;
      • System.out.println(myNum);
    • Characteristics:
      • Size: 4 byte
      • Capacity: Stores whole numbers from -2,147,483,648 to 2,147,483,647
  • 4. Long:
    • Syntax: long variable_name;
      • Example: long myNum = 15000000000L;
      • System.out.println(myNum);
    • Characteristics:
      • Define: It is write at end the value with an "L":
      • Size: 8 byte
      • Capacity: Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • 5. Float:
    • Syntax: float variable_name;
      • Example: float myNum = 5.75f;
      • System.out.println(myNum);
    • Characteristics:
      • Define: It is write at end the value with an "f":
      • Size: 4 byte
      • Capacity: Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
  • 6. Double:
    • Syntax: double variable_name;
      • Example: double myNum = 19.99d;
      • System.out.println(myNum);
    • Characteristics:
      • Define: It is write at the end the value with a "d":
      • Size: 8 byte
      • Capacity: Stores fractional numbers. Sufficient for storing 15 decimal digits
  • 7. Boolean:
    • Syntax: boolean variable_name;
      • Example: boolean bl = True;
      • System.out.println(bl);
    • Characteristics:
      • Size: 1 bit
      • Capacity: Stores true or false values
  • 8. Char:
    • Syntax: char variable_name;
    • Example: char c;
    • Characteristics:
      • Define:
        • The character must be surrounded by single quotes, like 'A' or 'C' or 'S':
        • You can use ASCII values to display certain characters. such as a=65 or b=66
      • Size: 2 byte
      • Capacity: Stores a single character/letter or ASCII values

Non-Primitive Data Types

  • String:
    • Strings are used for storing text.
    • A String variable contains a collection of characters surrounded by double-quotes.
    • Syntax: String variable_name = "value";
    • Example: String greeting = "Hello";
  • Array:
    • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
    • To declare an array, define the variable type with square brackets:
      • Syntax: datatype variable_name[];
      • Syntax: datatype []variable_name;
      • Example: int age[];
    • Example: int []age;
  • Class:
    • Everything in Java is associated with classes and objects.
    • A Class is like an object constructor or a "blueprint" for creating objects.
    • To create a class, use the keyword class.
    • Syntax:
      • public class Class_name {
      • }
    • Example:
      • public class MyClass {
      • }

Difference between Primitive and Non-primitive:


  • Primitive types are predefined (already defined) in Java.
  • Non-primitive types are created by the programmer and is not defined by Java (except for String).
  • non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type has always a value, while non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.
  • The size of a primitive type depends on the data type, while non-primitive types have all the same size

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form