Variable types in Java. Creation of variables

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Variable-visualization-Vertex-Academy

What is a variable in Java?

Often you can find such a definition of variables:

  • Variable is a container, that can store a value for further execution in a program.

Do you remember it from the school program? y = x + 1

And the value of Y variable changes depending on which value the variable X possesses.

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 the variables serve the same purpose as in the y = x + 1 example from school. They fulfil a function of a container for different values, which can be substituted to a variable. In the above example – to 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: Overall there are 8 types of variables (byte, short, int, long, double, char, boolean). Highly respected Bruce Eckel allocates the 9th type, so called void type (“null” value). But as is customary this article will cover only 8 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 variables of an integer type.

Variables of an 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 the value from -128 to 127 and takes 1 byte in memory
  2. short possesses the value from -32768 to 32767 and takes 2 bytes in memory
  3. int possesses the value from -2147483648 to 2147483647 and takes 4 bytes in memory
  4. long possesses the value from -9223372036854775808 to 9223372036854775807 and takes 8 bytes in memory

You could respond: “Very well. If byte, short, int and long are responsible for all integral values, which type should I choose?”. Do you remember math problems from childhood? They may be very helpful for the explanation of for what byte, short, int and long are responsible.

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 the whole apples. It is not assumed that the apples can be divided into pieces.

So if we would write a Java code which would suggest a solution of this problem, we would use variables of an integer type. It remains only to understand, which type we would use: byte, short, int or long?

You should always take into account the context:

  1. If we are certain that even if we change the values, for example Daniel would have 50 apples and Amber would have 30 apples, the solution of this problem will be less than 127 apples, then we can safely use the byte type.
  2. If we know that the problem situation can be changed and Daniel would have, for example, 10 000 apples and Amber would have 7000 apples, we won’t be able to use the byte type anymore, since this is clearly out of acceptable range, which is 127. It means that we should use the short type, whose maximum permitted value is 32767.
  3. Let’s say Daniel and Amber own apple orchards throughout the country and they have more than 32767 apples. In this case we need to use the int type. By the way, int stands for integer.
  4. Well, we can assume that Daniel and Amber are “apple giants” and they own all apple orchards in the world, then the value can be much higher even than the maximum value of int, which is 2147483647. It this case you need to use the long type.

However, you will use the int type in programming more often than other types. This is the most common integer type. Moreover, the int type in Java is the default type for integer types. You will understand what does it mean from the practice.

Prior to using a variable, you need to declare it. A variable is declared as follows:

int_declaration_vertex-academy

Following are examples of variable declaration:

byte apples;

short apples;

int apples;

long apples;

It’s necessary to say that if a name of a variable consists of two or more words it should be written in one word, just like camel “humps”. This style of word 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. What is more, the first word starts with the low-case letter and all first letters of the subsequent words are capital. Now you know, that this style of name writing is called CamelStyle. We even have a separate article dedicated to this topic 🙂

Well, it’s time to understand for what the floating point variables are responsible.


Floating point variables: float, double

Again, let’s refer to the example of the school math problem.

Problem 2:

Apples-Float-Vertex-AcademyDaniel has 3.5 apples; 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-integral numbers.  That means that we cannot use the integer type: we won’t be able to use byte, short, int or long types. Remember, as soon as it comes to integer numbers, it means that you should use float or double. 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 range from -3.4E + 38 to 3.4E + 38 and thus takes 4 bytes in memory
  2. double can possess the value from -1.7E + 308 to 1.7Е + 308 and takes 8 bytes in memory

Remember:

  • Integer numbers should be written with points instead of commas, as some of you may be used to since school days. 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 favorable when you don’t need a special accuracy for a fractional, for example for quantity of money.
  • double provides a double accuracy, which we can understand from its name.

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

float_variable_declaration

Following are examples of variable declaration:

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

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

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

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


Symbols: char

char-variable_vertex-academy

The char data type uses the Unicode encoding in Java. This type has a minimum value of 0 and maximum value of 65536 and takes 2 bytes in 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.

Prior to using a variable, you need to declare it.  Char variables are declared just as before:

char_variable_declaration

Following are examples of variable declaration:

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

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


Logical variables: boolean

boolean_variable_vertex-academy

The logical or boolean values can possess only one of two possible values: true or false. As you can see the Java programming language is very precise: either true or false.

Boolean-Picture-Vertex-Academy-1

A little bit later, when we will touch on the theme of selection construct, you’ll understand where this variable type is used. Until then you should just remember about the existence of this data type, but for now it will be a “dark horse” for you.

Prior to using a variable, you need to declare it.  Boolean variables are declared just as before:

boolean_variable_declaration

Following are examples of variable declaration:

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

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


Well, now you know for what the variable types are responsible:

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. Therefore, if you will hear “primitive data types” or “primitives” you will know that this is about 8 variable types: byte, short, int, long, float, double, char, boolean.
  2. 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”.
  3. In Java it is very important to format the code properly. Yes, you need not only to write a program which is able to work, but also it needs to be properly formatted. There are a set of rules called Java Code Convention. So you can download it right now and start to learn it. But this time we will cover only a part of the Code Conventions, namely the rules of name writing for variables. So let’s remember them!

Variables-Rules-Vertex-Academy

5 rules for selecting names for the variables:

Rule 1 – Variables are written only in Latin characters!

For example:

int st;

int width;


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

For example:

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

int size; // This variant is much more self-explanatory in comparison to the first one. As the name implies, the variable is responsible for the size of something.


Rule 3 How do we can or cannot start a name of a variable?

It can start with:

  • Any Latin character
  • Such symbols as $ or _

It cannot start with:

  • Numbers

We listed some right and wrong examples below:

Right:

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

Wrong:

  • int 2square;
  • int 101dalmatians;

Rule 4 – Variable name, consisting of two or more words, should be written in Camelstyle.

What is the CamelStyle we have already discussed in this article.


Rule 5 – You cannot use these 54 words in variable names:

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 have a special meaning in Java code. In time you will understand where each word is used. Moreover, after this article you will know the meaning of some of these words, which we 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-academyThese 8 variable types (byte, short, int, long, float, double, char, boolean) fall into the category of primitive variable types.

Prior to using a variable, you need to declare it:

variable_declaration_vertex-academy

5 rules of name writing for variables

  1. Variable can be written only in Latin characters!
  2. Variable name should be, if possible, self-explanatory.
  3. If a variable consists of two or more words, it should be written in CamelStyle (another definition – CamelCase)
  4. There are 54 words, which you cannot use in variable names.
  5. Variable names:
  • can start with any Latin character, symbols $ 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”.

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Leave a Reply

Your email address will not be published.

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

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