Primitive Data Types in Java

Graphic with the eight primitive data types by MakeTheBrainHappy.
Data in computer science, especially in Java, it defined with a type that defines the type of data that can be stored (words, numbers, etc.) and the range of values (i.e. length of the words or size of numbers, etc.) that can be acted upon.

Primitives are built-in data types; other types are "reference" types that need to be defined in a class blueprint and "created" as objects. Primitives are simpler and will allow us to begin our discussion of data. Java has eight primitive data types (the three most common are starred):

Byte: integer value (8 bits)

Short: integer value (16 bits)

*Int: integer value (32 bits)

Long: integer value (64 bits)

Float: floating point # (32 bits)

*Double: floating point # (64 bits)

Character: one unicode character

*Booleans: a true or false value

A larger number of bits corresponds to a larger number which can be stored into those variables.

Declaring Primitives:

byte varName;
short VarName;
int varname;
long Varname;
float vArname;
double vArName;
char varNAme;
boolean varNAMe;

These variables can be then be assigned using the = operator to values fitting the descriptions for each of the primitive data types. If you add the keyword final between the type and variable name, then you have declared a constant (you cannot change the data within the variable later in the program).

Reserved Keywords:

Java has certain reserved keywords that cannot be utilized as variable names due to their special functions within the language. These are abstract, assert, boolean, break, byte, case, catch, char, class, const, default, do, double, else, enum, extends, false, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, null, package,  private, protected, public, return, short, static, strictfp, super, switch, synchronized,  this, throw, throws, transient, true, try, void, volatile, while, and continue.

Visibility Modifiers:

Variables can either be declared as public or private, reserved keywords which are placed in front of the type. Public variables can be accessed by all classes while private variables can only be utilized by the class in which they are declared.

Static vs. Non-Static Variables:

This keyword, also placed before the type but after the visibility modifier, determines whether the variable is a part of the object declared from the class (thereby only being accessible through the identifiers of an object) as opposed to static variables are a part of the class and therefore can be accessed from "static" contexts such as the main method or static methods.


Some variables can be changed from one type to another through casting. Between the six types dealing with numerical values, you can cast utilizing the casting operator placed before the variable name. For example, here is a variable being converted from a integer to a double.

int varname = 1;
double newDouble = (double)varname;

This is a widening conversion since a more restrictive data type is changed to a more free version; therefore, no data is lost. But if you are converting a double to an integer, you would lose the information encoded by the decimals. This means that this would be a narrowing conversation.

No comments

Powered by Blogger.