World's most popular travel blog for travel bloggers.

Data Types

, , No Comments

Data types in Java

There are majorly two types of languages. First one is Statically typed languagewhere each variable and expression type is already known at compile time.Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C,C++, Java. Other, Dynamically typed languages: These languages can receive different data types over the time. Ruby, Python
Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.

Types of Data types 

  1. Primitive Data Types
    • Integer types
    • Floating type
    • Character type
  2. Non-Primitive Data Types
    • Arrays

Java: Primitive data types

A data type is a classification of data, which can store a specific type of information. Data types are primarily used in computer programming, in which variables are created to store data. Each variable is assigned a data type that determines what type of data the variable may contain.
The term "data type" and "primitive data type" are often used interchangeably. Primitive data types are predefined types of data, which are supported by the programming language. For example, integer, character, and string are all primitive data types. Programmers can use these data types when creating variables in their programs. For example, a programmer may create a variable called "lastname" and define it as a string data type. The variable will then store data as a string of characters.
The eight primitive data types in Java are:
  • boolean, the type whose values are either true or false
  • char, the character type whose values are 16-bit Unicode characters
  • the arithmetic types:
    • the integral types:
      • byte
      • short
      • int
      • long
    • the floating-point types:
      • float
      • double

Values of class type are references. Strings are references to an instance of class String.
Primitive data types in Java
TypeDescriptionDefaultSizeExample Literals
booleantrue or falsefalse1 bittruefalse
bytetwos complement integer08 bits(none)
charUnicode character\u000016 bits'a''\u0041''\101''\\''\'''\n''ß'
shorttwos complement integer016 bits(none)
inttwos complement integer032 bits-2-1012
longtwos complement integer064 bits-2L-1L0L1L2L
floatIEEE 754 floating point0.032 bits1.23e100f-1.23e-100f.3f3.14F
doubleIEEE 754 floating point0.064 bits1.23456e300d-1.23456e-300d1e1d

int - Integer data types

Integers are whole numbers that can have both positive and negative values but no decimal values. Example: 0, -5, 10
In C programming, keyword int is used for declaring integer variable. For example:
int id;
Here, id is a variable of type integer.
You can declare multiple variable at once in C programming. For example:
int id, age;
The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1. If you try to store larger number than 231-1, i.e,+2147483647 and smaller number than -231, i.e, -2147483648, program will not run correctly.
Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1.

float - Floating types

Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare a floating point variable in C by using either float or double keyword. For example:
float accountBalance;
double bookPrice;
Here, both accountBalance and bookPrice are floating type variables.
In C, floating values can be represented in exponential form as well. For example:
float normalizationFactor = 22.442e2;

char - Character types

Keyword char is used for declaring character type variables. For example:
char test = 'h';
Here, test is a character variable. The value of test is 'h'.
The size of character variable is 1 byte.

Java: Non- Primitive data types

Non-primitive, or reference data types, are the more sophisticated members of the data type family. They don't store the value, but store a reference to that value. Instead of partNumber 4030023, Java keeps the reference, also called address, to that value, not the value itself.
Reference types can be a class, interface, or array variable. Remember that a class is a set of plans for a given object. There are thousands of tree objects, but the parent set of plans would belong in the tree class. Variables can exist inside the tree class, such as height or tree type. These are reference variables.
An array is a single object that contains multiple values of the same type. We could have declared our integer for partNumbers as an array to hold a given number of partNumbers in a single object.
The diagram you're looking at on your screen illustrates an example of primitive variables versus a reference variable. Notice how the values are stored in the primitive variables but the non-primitive or reference variable points to an address in memory.


Post a Comment

Let us know your responses and feedback