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.

Casting

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.

6 comments:

  1. I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors.

    Data Science Training in Bangalore

    ReplyDelete
  2. Really impressed! Everything is a very open and very clear clarification of the issues. It contains true facts. Your website is very valuable. Thanks for sharing.

    Digital Marketing Training in Bangalore

    ReplyDelete
  3. Happy to chat on your blog, I feel like I can't wait to read more reliable posts and think we all want to thank many blog posts to share with us.

    Machine Learning Course in Bangalore

    ReplyDelete
  4. I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.

    Artificial Intelligence Training in Bangalore

    ReplyDelete
  5. Mind-blowing went amazed with the content posted. Containing the information in its unique format with fully loaded valid info, which ultimately grabs the folks to go through its content. Hope you to keep up maintaining the standards in posting the content further too.

    Data Science Course in Bangalore

    ReplyDelete
  6. Well we really like to visit this site, many useful information we can get here.
    data science training

    ReplyDelete