Advanced Java Tutorial. Primitive and variable types. | by Emmanuel Tejeda | June 2022

0

Photo by Antoine Dautry on Unsplash

In this guide, we’ll break down data types and variables and show you how they work. Much of this guide will contain programming and math jargon, if you are unfamiliar with programming or are just getting started, I would not recommend this guide. If you are a student or studying for an interview, this information will be useful to familiarize yourself with.

Fields vs local variables

The fields

  • Fields are declared at the class level, as opposed to the method or block level.
  • They can be an instance variable, meaning each instance of the class has a unique instance of that variable. Or they can be a static class/variable, meaning there is only one instance in memory for that variable that any instance of the class can access and change. Static variables can be invoked with the class name rather than creating an instance. For instance className.variablename
    If you want more information on static vs instance, check out this Oracle tutorial
  • The fields will be automatically initialized with a default value. Here are the defaults for all primitive data types. String is the only outlier since it is an object (primitive data types are classified as such because they only contain raw values).
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Local variables

  • Local variables are declared in code blocks or methods
  • They need to be assigned a value/initialized
  • They can never be static
  • If undefined, will cause undefined error in console

The difference between primitive types and object references

Primitive data types

  • Store raw values.
  • Cannot store null

Object references

  • Stores a pointer to its value
  • Can store null

Primitive integral types

Integral types are values ​​that cannot be expressed in decimal format and can contain a certain range of values ​​depending on which one is used. They are all initialized with the value 0. The list of Integral types in ascending order of size is as follows.

  • byte. A byte is a sequence of 8 bits. A byte can contain numeric values ​​from -128 to 127. It is best used when the numbers that will be stored do not require a lot of memory.
  • short. A short is twice the size of a 16-bit byte. It can store values ​​from -32768 to 32767.
  • entire. An int is probably what most people who program are familiar with. An int is a 32-bit sequence and can contain values ​​from -2.1 billion to 2.1 billion. An int in most situations is not the ideal data type to use due to the amount of memory it takes. Most people assigning values ​​to an int will almost never approach its upper or lower bound.
  • long. A long is the largest of the integral types. At 64 bits, it can hold values ​​between -9.2 Con trillion and 9.2 Con trillion. Longs are most often used to measure time in milliseconds. When you have a number that an int can’t handle, a long is usually your choice. When you write a long value, you must add the letter “L” at the end to signify that it is a long value. The L can be uppercase or lowercase. Example long bigNum = 9,233,186,123L.

Floating and Dual Primitives

There are not many differences between a float and a double. They both behave very similarly and share many features. So for this section, we’ll list what they share, and then list their individual properties next.

Floats and doubles

  • Can store integral types such as long, int, short, etc.
  • Represent data in decimal format.
  • Have a wider range of values ​​than all integrals.
  • They both round the number after the decimal point if the value sent to the console has too many digits.
  • If the value output to the console is too large, it will be represented in scientific notation.

Now for the differences

Float

  • Is a 32-bit sequence.
  • Must always include the letter “F” in lowercase or uppercase at the end of the value. Otherwise, the system will recognize it as a duplicate.
    For instance, float aFloat = 21.22f . If you don’t add “f” at the end, you will get an error.

Double

Primitive Arctic Char

Most people familiar with the char data type know that it can hold a character like we see here char letter = 'B'but this is just an overview of how the char primitive works.

  • The char is actually a 16-bit integral type that can hold a value from 0 to 65535.
  • A character like all integral types cannot be assigned a decimal value
  • A char can also be represented as a Unicode literal. For instance 'A' and Unicode 'u0041' are the same and will both print the letter “A” to the console when running. Many characters in different languages ​​are represented in Unicode. You can explore the wiki on Unicode if you are interested.
  • Under the hood, a character is represented as an integer. For example, if you were to give a character a number like this and print it to the consolechar letterA = 65 you would get the letter ‘A’. This means that these three values 'A' = 'u0041' = 65 are all the same when assigned to a tank.
  • A char is initialized with the value 0 represented in Unicode as 'u0000' .

Using a number when using characters is very obscure and not something you will see every day. But it is very useful to know that it is a possibility.

I hope you found this informative. If you have any questions or comments, leave them and I will respond as soon as possible.

Have a nice day!

Resources

List of Unicodes and their values

Oracle tutorial. class/static vs instance variable

Share.

About Author

Comments are closed.