DEV Community

Cover image for Java: The datatypes. {}
Raksha Kannusami
Raksha Kannusami

Posted on

Java: The datatypes. {}

🎯 What is a data type?

Wikipedia says "In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support basic data types of integer numbers (of varying sizes), Floating-point numbers (which approximate real numbers), characters, and Booleans. A data type constrains the values that an expression, such as a variable or a function, might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. A data type provides a set of values from which an expression (i.e. variable, function, etc.) may take its values."

🎯 Types:

There are 2 types of data types in Java: Primitive and Non-Primitive. In simple words, primitive data types are predefined whereas non-primitive data types are user defined.

🎯 Primitive Datatypes:

A primitive data type specifies the size and type of variable values and has no additional methods. There are 8 primitive data types in Java: byte, short, int, long, float, double, boolean, char. Let us see how to initialize them below.

Byte

byte x=10;
Enter fullscreen mode Exit fullscreen mode

Short

short x=10;
Enter fullscreen mode Exit fullscreen mode

Int

int x=10;
Enter fullscreen mode Exit fullscreen mode

Long

long x=10000000;
Enter fullscreen mode Exit fullscreen mode

Float

float x=10f;
Enter fullscreen mode Exit fullscreen mode

Double

double x=10.1010;
Enter fullscreen mode Exit fullscreen mode

Boolean

boolean x=1;
Enter fullscreen mode Exit fullscreen mode

Char

char c ='s';
Enter fullscreen mode Exit fullscreen mode

🎯 Non-Primitive Datatypes:

Non-primitive data types are called reference types because they refer to objects. There are many non-primitive data types in Java: arrays, strings, classes etc. Let us see how to initialize an array and a string.

Array

int[] array = new int[10];
Enter fullscreen mode Exit fullscreen mode

String

String s = "hello world";
Enter fullscreen mode Exit fullscreen mode

Since now we know what data types are, it is important to learn the usage of these data types so that we know where to use them to solve a problem.

...To be continued! πŸŽ‰
keep learning, keep coding!πŸ’–

Top comments (1)

Collapse
 
rakshakannu profile image
Raksha Kannusami

Thank you for adding this point! :)