JavaRush /Java Blog /Random EN /Java Magic: Declaring and Initializing Variables
Зеленая лягушка
Level 26
Москва

Java Magic: Declaring and Initializing Variables

Published in the Random EN group

Introduction

The dull surrounding reality is of no interest to anyone, so let's talk about the enchanting world of magic and the rules for casting spells that realize our wildest fantasies. The world of magic is immaterial and allows you to create any entity. All you need is the “power of thought” and you can create anything you can. The fantastic nature of what is created is limited only by the abilities of logical and abstract thinking. Well, you need to know a little about the dark art of necromancy and mathematics. Although you may not know the secrets of this area of ​​the illusory, you can spy on them from necromancers and mathematicians. The number of magical languages ​​is “very large and varied.” And we will analyze the rules of one of them - Java. From the Ligurian language it is translated as the Adventures of Jupiter and Valkyrie Adventures.

Announcement

In the world of magic, firstly, consciousness is primary, not matter. And secondly, any conceivable entity can exist. Therefore, the creation of an entity begins with the assertion that it exists. This is called an announcement. And since a magician can do anything with a created entity at any time, including changing its value, declared entities are called variables. Since even a novice magician can come up with a lot of things, entities are distinguished and systematized using types. There are types created by the Creator, they cannot be changed and they are called primitive. All other types are created by the magician himself and they are called object types.

Variable name

Each variable must have a true name. Actually, only names distinguish the entities created by the magician. The names of the people must be composed of magical symbols, considered letters and numbers in the esoteric-astrological Unicode system. For example, the underscore character _ is considered a letter, so it can be used in variable names. A name can only begin with a magic symbol, which is considered a letter. The first letter must be lowercase. The Interworld Association of Mages and Adepts of Necromancy (MAMAN) has adopted some informal rules of good form for composing variable names. Namely, the name must be meaningful and can consist of several words. When merging several words, the first letter of each word (except the first) must be capitalized. For example, myLittlePony. This style is called "camel hump". And this is because it was invented after the sacrifice of a camel.

Variable type

In the magic of Java, all variables are strongly typed. This means that every variable has a type and it must be specified explicitly when declaring it. I repeat that there are primitive and object types. Primitive - eight. Because eight is the devil's number. It represents 2 to the 4th power and is divisible without a remainder by any number from 1 to 8 inclusive, except prime numbers. Primitive types: boolean, char, byte, short, int, long, float, double. Object types are invented by the magician himself and they are written with a capital letter. For example – MySweetPony. Many thousands of object types (classes) have already been written by the magicians who stood at the origins of Java and are part of MAMAN. This collection is called the Java API and you can use classes (types) from it in your magic without any restrictions.

Announcement

So, the declaration occurs by specifying the type of the variable and its name. Examples:
boolean isPony;
String ponyName;
int ponyTaleSize;
Pony sweetieBelle;
A semicolon at the end of each line is required. It ends every expression in Java. In each of these examples, we declare the variable to exist and indicate what type it is. But at this stage of witchcraft, the variables do not yet have content (meaning). They exist only as a pure, not an embodied idea. Filling variables with content (assigning values ​​to them) will be discussed in the section on initialization.

Ad Tricks

There are a few hidden magic tricks you need to know. Or at least remember them. You can declare several variables of the same type in one expression. Example:
boolean isPony, isPonyReady;
String ponyName, ponyColor, ponyMood;
int ponyTaleSize, ponyLegSize; ponyManeSize
Pony sweetieBelle, rainbowDash, twilightSparkle;
Variables can be declared anywhere in the class, but before they are used in expressions, since class expressions are executed sequentially from top to bottom. You should also remember about such an important sorcery as the scope of variables. The scope determines from which part of the class the declared variable can be accessed. This is a separate topic, but in short, there are two rules. Variables declared in a class and not inside methods, loops, or code blocks are visible throughout the class. Variables declared inside methods, loops, and code blocks are visible only within the corresponding method, loop, and code block. Best regards, Green Frog. PS (to be continued)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION