Java - Variable Types. How to Create a Variable in Java

FacebooktwittertumblrFacebooktwittertumblr

Variable-visualization-Vertex-Academy

This is one of the articles from our Java Tutorial for Beginners.


What is a variable in Java?

It's common to encounter the following definition of a variable. A variable is a container which holds values that are used in a Java program.

Do you remember the basic math you learned in school?

y = x + 1

Here, as you can see, the y variable changes when the x variable is different. For example:

  • if x = 1, then x + 1 = 2
  • if x = 2, then x + 1 = 3
  • if x = 1.5, then x + 1 = 2.5

In Java, variables play the same role as in the above math example: y = x + 1. So, variables are containers that hold values. Just like we did with the x variable.

Variable types in Java. Variable declaration

In Java, it is possible to specify which values can be possessed by a variable.

For this purpose, all variables are divided into 4 groups:

  1. Variables of an integer type (among these are byte, short, int, long)
  2. Floating point variables (among these are float, double)
  3. Symbols (char)
  4. Logic variables (boolean)

variables-Java-vertex-academy

Note: There are 8 types of variables (byte, short, int, long, double, char, boolean). The highly respected computer programmer Bruce Eckel includes a 9th type, the so-called void type (“null” value). But this article will only cover the 8 commonly-used types. We’ll discuss the void type in another topic dedicated to methods in Java.

Now let’s look at each variable group. Let’s start with the variables of the integer type.

Variables of the integer type: byte, short, int, long

integer_variables_vertex-academy

The table shows that byte, short, int, long are integer values. For example, 1, 9, 1278, -5, -107, etc.

It is obvious, that:

  1. byte can possess a value between -128 and 127 and it takes up 1 byte of memory
  2. short can possess a value between -32768 and 32767 and it takes up 2 bytes of memory
  3. int can possess a value between -2147483648 and 2147483647 and it takes up 4 bytes of memory
  4. long can possess a value between -9223372036854775808 and 9223372036854775807 and it takes up 8 bytes of memory

Now you might be thinking: “If byte, short, int and long are responsible for all the integral values, which type should I choose?”. Well, do you remember those math problems from childhood? They may be very helpful in the following explanation of what byte, short, int and long are responsible for.

Problem 1

Apples_3Daniel has 3 apples; Amber has 2 apples. How many apples do Daniel and Amber have together?

As you can see, the problem is about whole apples. It is not assumed that the apples can be divided into pieces.

So if we wrote a Java code which would suggest the solution to this problem, we would use variables of an integer type. But which type we would use: byte, short, int or long?

You should always take the context into account:

  1. If we are certain that we can change the numbers (for example, Daniel with 50 apples and Amber with 30 apples) and the solution of this problem is still less than 127 apples, then we can safely use the byte type.
  2. If we know that changing the numbers (for example, Daniel with 10,000 apples and Amber with 7000 apples) will put us out of the acceptable range (127), then we we won’t be able to use the byte type anymore. Instead, we will have to use the short type, whose maximum permitted value is 32767.
  3. Let’s say that Daniel and Amber own apple orchards throughout the country and they have more than 32767 apples. In that case, we need to use the int type. By the way, int stands for integer.
  4. Now let's assume that Daniel and Amber are apple barons and they own all the apple orchards in the world. In this case, the value might be much higher even than the maximum value of int, which is 2147483647. Therefore, we need to use the long type.

However, it's worth mentioning that you will use the int type in programming more often than the other types. It is the most common integer type. Moreover, the int type in Java is the default type for integer types. What exactly that means, you will soon find out.

Before using a variable, you need to declare it. A variable is declared in the following manner:

int_declaration_vertex-academy

Some examples of variable declaration:

byte apples;

short apples;

int apples;

long apples;

It’s necessary to mention that if the name of a variable consists of two or more words it should be written as one word (without spaces), like the humps of a camel. This style of writing is called CamelStyle.

CamelStyle-Vertex-Academy-2

For example:

int applesDaniel;

int applesAmberJune;

int applesDanielJuneUkraine;

As you can see, the examples of variable names consist of two or more words and are written as one word. Also, the first letter of the first word is lower-case, but all the first letters of the subsequent words are capitalized. Now you know why this style is called CamelStyle. We even have a separate article dedicated to this topic 🙂

Well, it’s time to learn what floating point variables do.


Floating point variables: float, double

Let's refer to the math problem example once again.

Problem 2:

Apples-Float-Vertex-AcademyDaniel has 3.5 apples and Amber has 2.5 apples. How many apples do Daniel and Amber have together?

As you can see, the problem is no longer about whole apples; now it is about non-integer numbers.  That means that we won't be able to use the integer type and, consequently, we won’t be able to use byte, short, int, or long types. Keep in mind: whenever you see non-integer numbers, it means that you have to use float or double. Some examples of floating point numbers: 1.0, 1.8, 3.141562, 9.0, 12.579, 1278.0, -5.0, - 9.4, -107.0, -107.356, etc.

float-variables_vertex-academy

As you can see from the table:

  1. float can possess a value between -3.4E + 38 and 3.4E + 38 and it takes up 4 bytes of memory
  2. double can possess a value between -1.7E + 308 and 1.7Е + 308 and it takes up 8 bytes of memory

Remember:

  • Integer numbers must be written with periods instead of commas. For example, “1,5” is wrong; “1.5” is correct.
  • float determines the value of short precision. This means that variables of this type are convenient when you don’t need a special accuracy for a fraction. For example, a sum of money.
  • double provides a double exactness, which is how it got its name.

Before you use a variable, you need to declare it. The float and double variables are declared as follows:

float_variable_declaration

Some examples of variable declaration:

float money; // to declare “money” as a variable of a float type

float wage; // to declare “wage”as a variable of the float type

float c; // to declare “c” as a variable of the float type

double stat; // to declare “stat” as a variable of the double type


Symbols: char

char-variable_vertex-academy

The char data type uses the Unicode encoding in Java. As we can see from the table, this type has a minimum value of 0 and a maximum value of 65536 and takes up 2 bytes of memory. Negative values do not exist. In fact, a variable of the char data type stores not a symbol, but its numerical code from Unicode table, so we can perform integer operations with symbols.

Before you use a variable, you need to declare it.  Char variables are declared just like the others:

char_variable_declaration

Some examples of variable declaration:

char y; // to declare “y” as a variable of the char type

char f; // to declare “f” as a variable of the char type


Logical variables: boolean

boolean_variable_vertex-academy

Logical/boolean values can possess only one of two possible values: true or false

Boolean-Picture-Vertex-Academy-1

A little bit later, when we touch on the theme of selection construct, you’ll understand when this variable type is used. Until then, all you need to know is that such a data type exists.

Before you use a variable, you need to declare it.  Boolean variables are declared like the previous variables:

boolean_variable_declaration

Examples of variable declaration:

boolean check1; // to declare “check1” as a variable of the boolean type

boolean check2; // to declare “check2” as a variable of the boolean type


Well, now you need to know what variable types do:

variables-Java-vertex-academy

What else do you need to know?

  1. You need to know that these variable types fall into the category of primitive variable types. So if you ever hear the terms “primitive data types” or “primitives,” you'll know that they're talking about these 8 variable types: byte, short, int, long, float, double, char, boolean.
  2. There is one more variable type, which is called String. We will cover later it in the article: “How to Assign a Value to a Variable.”
  3. In Java, it is very important to format the code correctly. Yes, you not only need to write a program which works, but it also has to be properly formatted. There exist a whole set of rules called Java Code Conventions. You can download it right now and learn it. But right now we're only going to cover a part of the Code Conventions, specifically the rules of name writing for variables. So let’s examine them!

Variables-Rules-Vertex-Academy

5 Rules for Selecting Names for Variables:

Rule 1 – Variables have to be written in Latin characters!

For example:

int st;

int width;


Rule 2 – The name of a variable should be, if possible, self-explanatory

For example:

int s; // You can call it this, but it will be really hard to read the code if there are a lot of code lines and none of the variables are self-explanatory.

int size; // Compared to the above example, this one is much more self-explanatory. As the name implies, this variable is responsible for the size of something.


Rule 3 – What symbols can we not use to start the name of a variable?

We can start with:

  • Any Latin character
  • Such symbols as $ or _

We cannot start with:

  • Numbers

We've listed some examples of correct and incorrect variables below:

Right:

  • int square;
  • int $money;
  • int width;
  • int boxSize;
  • double sum;
  • double sumJune;

Wrong:

  • int 2square;
  • int 101dalmatians;

Rule 4 – If a variable name consists of two or more words, it has to be written in CamelStyle

We've already discussed what CamelStyle is.


Rule 5 – You cannot use any of these 54 words when you name variables:

abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, 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, String, super, switch, synchronized, this, throw, throws, transient, true, try, void, volatile, while

 These 54 words carry a special importance in writing code in Java. In time you will understand when each word is used. Moreover, after you finish this article, you will have already learned the meaning of the words which we have highlighted in blue:

abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, 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, String, super, switch, synchronized, this, throw, throws, transient, true, try, void, volatile, while


LET’S SUMMARIZE:

There are 4 groups of variables:

  1. Variables of an integer type (among these are byte, short, int, long)
  2. Floating point variables (among these are float, double)
  3. Symbols (char)
  4. Logic variables (boolean)

variables-Java-vertex-academy

These 8 variable types (byte, short, int, long, float, double, char, boolean) fall into the category of primitive variable types.

Before you use a variable, you need to declare it:

variable_declaration_vertex-academy

5 rules for writing the names of variables

  1. A variable has to be written in Latin characters.
  2. A variable name should be, if possible, self-explanatory.
  3. If a variable consists of two or more words, it has to be written in CamelStyle (another definition – CamelCase)
  4. There are 54 words which you cannot use when you name variables.
  5. Variable names:
  • can start with any Latin character and symbols like $ and _
  • cannot start with numbers

There is one more type of variables, which is called String. We will cover it in our article “How to Assign a Value to a Variable.”

You can find more articles in our Java Tutorial for Beginners. 

FacebooktwittertumblrFacebooktwittertumblr

FacebooktwittertumblrFacebooktwittertumblr
Самоучители--узнать детальнее--