Java – Data Types | Primitive Type – Reference Type[String Type]

data types, java types, java data types, reference types, primitive types, string types

Variables are nothing more than reserved memory locations for storing values. This means that when you create a variable, you reserve some space in memory. Based on the data type that is assigned to the variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, Java can store integers, decimals, or characters in those variables.

Types Of Data Types

  • Primitive data types,
  • Reference data types,
  • Consider the String type separately.

Primitive Data Type

There are eight data types supported by Java. Basic data types are predefined by the language and named by a keyword. Now let’s look in detail at these eight basic data types in the Java programming language.

Data types, primitive data types

byte

  • byte information sort is an 8-bit marked two’s supplement whole number.
  • Maximum worth is 2^7 -1, which is equal to 127. This value is also included in the range of these values.
  • Minimum worth is -2^7, which is equal to -128.
  • Default value stored in a variable of this type is 0.
  • byte information sort is utilized to spare space in vast exhibits, principally set up of numbers, since a byte is four times littler than an int.

Example :- byte a = 100;
byte b = -50;

Short

  • Short information sort is a 16-bit marked two’s supplement number.
  • Maximum value is 2^15 -1, which is equal to 32,767. This number is also included in the range.
  • Minimum value is -2^15, which is equal to -32,768.
  • Short information sort can likewise be utilized to spare memory as byte information sort.
  • A short is 2 times littler than an int
  • The default value for this data type is 0.

Example :- short s = 10000;
short r = -20000;

int

  • int information sort is a 32-bit marked two’s supplement number.
  • Maximum value for this data type is 2^31 -1, which is equal to 2,147,483,647. This
  • Number is also included in the range for this data type.
  • Minimum value for this data type is -2^31, which is equal to – 2,147,483,648.
  • int is for the most part utilized as the default information sort for its indispensable qualities unless there is a worry about memory.
  • The default value for this data type is 0.

Example :- int a = 100000;
int b =-200000;

long

  • long information sort is a 64-bit marked two’s supplement whole number.
  • Maximum value for this data type is 2^63 -1, which is equal to 9,223,372,036,854,775,807.
  • Minimum value for this data type is -2^63, which is equal to -9,223,372,036,854,775,808.
  • This sort is utilized when a more extensive memory range than int is required.
  • The default value for that data type is 0l.

Example :- long a = 100000L;

                  long b =-200000L;

float

  • float is a data type, which is known for its solitary exactness, 32-bit IEEE 754 gliding point.
  • It is most of part, used to spare memory in vast exhibits of coasting point numbers.
  • The default value for this data type is 0.0f.
  • float information sort is never utilized for exact values, for example, money.

Example :- float f1 = 234.5f;

double

  • The double data type is a double-precision 64-bit IEEE 754 floating-point.
  • Usually used for decimal values.
  • The double type should never be used for an exact value, such as currency.
  • The default is 0.0d.

Example :- double x = 321.4;

char

  • char information sort is a solitary 16-bit Unicode character.
  • Maximum value for a variable of this type is “\ uffff” (or 65,535 comprehensive).
  • Minimum value for a variable of this type is “\ u0000” (or 0).
  • char information sort is utilized to store any character.

Example :- char letterA =’A’;

boolean

  • boolean information sort speaks to one bit of data.
  • Any boolean variable can assume one of the two values: true or false.
  • This information sort is utilized for basic banners that track genuine/false conditions.
  • The default value for this data type is false.

Example :- boolean one = true;

Reference Data Type

  • Reference variables are created using specific class constructors. They are for accessing objects. These variables are declared with a specific type that cannot be changed. For example Employee, Puppy, etc.
  • Class objects and various kinds of array variables fall under the reference data type .
  • By default in Java, the value of any reference variable is null.
  • A reference variable can be used to denote any object, declared or any compatible type.

Example :- Animal animal = new Animal(“giraffe”);

String Type

The String type is not a primitive data type, but it is one of the most used types in Java. String is for storing strings of text. A few examples of using String-

// Create a string using the
String constructor myString = new String (“The weather was fine”);
// You can also create a string using quotes “”
String myString = “The weather was fine”;

Operator “+” is defined for strings

public static void main (String [] args) {

String s1 = “I have”;
String s2 = “apples”;
int num = 3;
String s = s1 + num + s2;

System.out.println (s);
}

Program output.

I have 3 apples