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.

11 comments:

  1. I am very enjoying to read your well-written article posts. It seems that you devote a great deal of hard work and time onto your own blog.
    Learn PMP training in Hyderabad

    ReplyDelete
  2. Great article with excellent information found useful thanks for sharing looking forward for next blog.
    Data Science Training in Hyderabad

    ReplyDelete
  3. Amazing article with very informative information found very useful and enjoyed reading it thanks for sharing.
    Data Analytics Course Online

    ReplyDelete
  4. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data science course in Hyderabad

    ReplyDelete
  5. 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.

    Business Analytics Course in Bangalore

    ReplyDelete
  6. A good blog always contains new and exciting information and as I read it I felt that this blog really has all of these qualities that make a blog.

    Data Analytics Course in Bangalore

    ReplyDelete
  7. Very nice blogs!!! I have to learning for lot of information for this sites…Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data science training in Hyderabad

    ReplyDelete
  8. Thanks for sharing great information. I highly recommend you.data science courses

    ReplyDelete
  9. I have to search sites with relevant information ,This is a
    wonderful blog,These type of blog keeps the users interest in
    the website, i am impressed. thank you.
    Data Science Course in Bangalore

    ReplyDelete
  10. hello sir,
    thanks for giving that type of information. I am really happy to visit your blog.Leading Solar company in Andhra Pradesh

    ReplyDelete