DEV Community

Cover image for Understanding Data Types in Java
Fikra_Dev
Fikra_Dev

Posted on

Understanding Data Types in Java

Background

Data types play a very important role in all programming languages but more so when it comes on to strictly typed languages. Some of the more well known strictly typed languages include C# and Java. But what does strictly-typed means?.

In very simple terms, strictly-typed programming languages are languages where every variable/object when being created must be declared of some type. The type will be determined by the kind of data that the programmer intends to store or operate on. For example, if he/she intends to store a user's name in a variable then that variable would be declared using the following syntax:

data-type variablename;

String userName;
Enter fullscreen mode Exit fullscreen mode

The type that a variable is declared with will determine the kind of operations that can be done on it. For example, the variable created above, could be joined/concatenated to another variable or value using the "+" operator which, coincidentally is also the addition operator that is used with numerical variable/values. Other common String operations include making it all upper or lowercase. The main idea to be understood here is that the data types determine which operations can and cannot be executed.

Primitive and Non-Primitive

There are essentially two (2) data types in Java, primitive and non-primitive. A primitive data type is one that can store simple values or better yet a single value. Examples of primitive data types include byte, int, boolean, short, long, float, char, double. Eight (8) in total.

A non-primitive data type on the other hand is one that can store multiple or more complex values. The non-primitive data types in Java are classes, objects, Strings, Arrays, Interfaces.

String being a non-primitive is kind of peculiar but comes down to the fact that a String is an immutable array of characters that has its own set of methods to perform operations on it such as toUpperCase(). In this way, by having its own methods would mean that String is a class, hence a non-primitive data type.

Enforcing Data Types

So how is the data type enforced? If you try to run a Java program with an incorrect data type stored in a variable or object etc, that program would throw an error. Even before that, as you type your code, most modern IDEs would indicate the error by either highlighting it or underlining it with red squiggles. The error would need to be corrected before your program would run successfully.

Closing

Having a firm understanding of this foundation concept is important if anyone is to become successful at using Java.

Thanks for the taking the time out to read my post. It is truly appreciated. If you want to know more about my journey follow me at:

https://www.ryancampbell.codes

Twitter: @coding_scorpion

Github: https://github.com/FikraDev

Top comments (0)