Data types in Java programming3 min read

Data Types

  • All primitive data types in java are atomic and are not objects.
  • Each primitive data type has a predefined range of values.
  • Each primitive data type has a corresponding wrapper class that can be used to represent a primitive value as an object.
Primitive data types in Java
Primitive data types

There are two data types available in Java:

  1. Primitive data types
  2. Reference/Object Data Types in Java

Primitive Data Types in Java

Int

  • This is the most commonly used data type in Java.
  • It is a 32-byte integer type.
  • Its valid range is -2147483648 to +2147483647.
  • Int is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0.
  • Example:
    • int rollno, marks;
    • int a = 100000, int b = -200000

Double

  • This is a double-precision 64-bit number data type. Most of the math functions return a double value.
  • Mostly we use this type for manipulating large valued numbers or when we require to maintain accuracy in iterative calculations.
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • The double data type should never be used for precise values such as currency.
  • Example:
    • double d1 = 123.4

Float

  • The 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.
  • The valid range for float numbers is 3.4 -0.38 to 3.4e+0.38.
  • The float data type is never used for precise values such as currency.
  • Example:
    • float f1 = 234.5

Boolean

  • This is a special data type that holds only logical values.
  • 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.
  • The default value is false. This type is also used to represent the output of conditional expressions.
  • Example:
    1. boolean result;
    2. boolean one = true

Long

  • This is a 64-bit integer type.
  • The valid range for long integers is -922337236854775808 to +922337236854775807 Ex. long seconds, distance;
  • This is mainly used when int type insufficient to hold number i.e when the number is larger than the range of int.
  • This type is used when a wider range than int is
  • The default value is OL.
  • Example:
    • long a = 100000L, int b = -200000L

Byte

  • The byte data type is an 8-bit signed two’s complement integer
  • The minimum value is -128 (-247)
  • The maximum value is 127 (inclusive)(2^7 -1)
  • The default value is 0
  • Example:
    • byte a = 100, byte b = -50

Char

  • Java uses Unicode to represent characters. the char data type is a single 16-bit Unicode character.
  • Unicode is a fully international character set that can represent all of the characters in all human languages.
  • Java uses Unicode because the Java applets are written for worldwide use.
  • This is a 16-bit data type. The range of this data type is 0 to 65536.
  • There are no negative char.
  • Example:
    1. char c,d; c=’a’; d=65;
    2. char letter A =’A’

Short

  • This is a signed 16-bit integer type.
  • Its valid range -32768 to + 32767.
  • This type is very rarely used in Java.
  • This is mostly applicable to 16-bit computers.
  • The default value is 0
  • Example:
    • short s = 10000, short r = -20000

Reference/Object Data Types in Java

  • The constructors of the classes are used to create reference variables.
    They’re used to get to things. These variables have been declared to be of a particular type that cannot be modified.
  • For instance, Employee, Puppy, and so on.
  • The reference data type includes class objects and several types of array variables.
  • Any reference variable’s default value is null.
  • Any object of the defined type or a compatible type can be referenced by a reference variable.
  • Example:
    • Animal animal = new Animal(‘giraffe’);

Share:

Leave a Reply