: sunny: Something like a box to put data in : sunny: You can name the box freely : sunny: In order to use variables, you need to declare that you will use variables from now on.
Example.java
public class Main {
public static void main(String[] args) {
int number;
number = 10;
System.out.println(number);
}
}
int number; → We have prepared a box named number!
number = 10; → Please put 10 in the box called number!
System.out.println(number); → Please output the contents of number!
console.
10
It will be displayed. I'll explain the contents in a little more detail: smile:
int number; Variables are declared with this int number ;. An int is called a ** type (or data type) ** and represents the type of data. The number is ** variable name ** and you can name the variable freely.
number is just a variable name that I have arbitrarily named, for example age or day. There are some rules even if you can name it freely
-Do not use words that are already built into java, such as int and public. ・ Basically, use English nouns that start with lowercase letters (Romaji, Kanji, and numbers that start with numbers are not recommended) ・ Avoid single-character variable names ・ When connecting multiple words, capitalize the beginning of the second word (myAge, etc.)
Is that the main rule? I'll talk about types later: smile:
number = 10; I put a value of 10 in the variable name number Putting values and characters in variables is called ** assignment **.
As an aside, if this = is attached, it is easier to read from the left. Put the value 10 in the box with the variable name number. Like. I think you learned at school in the sense that = is equal In programming, == means equal. When ==, read from the right: smile:
System.out.println(number); The variable name is in parentheses. Call what is in this variable name. This is called ** acquisition **.
Example.java
public class Main {
public static void main(String[] args) {
int number;
number = 10;
System.out.println(number);
number = 20;
System.out.println(number);
}
}
Substitute 10 for variable number as above Then I assigned 20 to the variable number again. How will it be displayed? : thinking:
console.
10
20
The variable number was assigned 20 and overwritten. If you assign a value to a variable that already has a value like this The old value disappears and the new value is overwritten: smile:
Example.java
int number;
number = 10;
Earlier, as mentioned above, the variable declaration and variable assignment were entered over two lines. Of course this is fine, but there is a way to put it all together on one line.
Variable initialization.
Type variable name=Data to be substituted;
By doing so, you can fill in one line at a time. Let's put together the code above: smile:
Example.java
int number = 10;
Assigning a value at the same time as declaring a variable in this way is called variable initialization **.
I think the word int was displayed before the variable name earlier. This int is simply an integer type of data type.
int number;
I mean An integer will be placed in the box called variable name number. that is how it is.
That is, decimals and letters cannot fit in this box. Only integers. If you want to assign decimals or letters, you need to use a different type.
Decimal numbers and character types will be explained later. I'll explain the integer type earlier.
I mentioned earlier that int is an integer in terms of data type. It must be an integer, but there are many types of integers alone ...: sweat_smile:
Model name | Range of storable integers | Memory consumption |
---|---|---|
byte | -Integer between 128 and 127 | 1 byte |
short | -Integer between 32768 and 32767 | 2 bytes |
int | -2147483648〜2147483647 | 4 bytes |
long | -9223372036854775808〜9223372036854775807 | 8 bytes |
If it is a test score, it is 0 to 100 points, so if you use byte, you can consume 1 byte of memory, right: smile: However, if the total score of 5 subjects is short, it seems that the memory consumption will be 2 bytes.
But then do I have to change the model name every time because I care about the memory consumption? It becomes: thinking :.
Basically int is fine unless you're dealing with very large numbers: smile: I don't know if it's an old PC, but modern PCs have a lot of memory, so I think it's necessary to use them strictly properly: smile:
By the way, when using long, add L or l after the number (10000L, 10000l, etc.)
There are two types of types that handle decimal numbers. float and double. What's the difference? Speaking of which, the memory consumption is different. The range of decimals that can be stored is also different, but I will omit it because the explanation is a bit cumbersome: sweat_smile: I think it will come out after a little research: smile:
Model name | Memory consumption | How to fill in |
---|---|---|
float | 4 bytes | Add F or f after the number |
double | 8 bytes | Add D or d after the number, but you can omit the basics |
Double is more rigorous than float. What does it mean to be strict ... Because the personal computer only knows the numbers, it cannot understand the decimal point. Therefore, instead of the decimal calculation that we calculate, the decimal is expressed by another calculation method so that the personal computer can understand it, but since the calculation method expresses the approximate value, a slight error will inevitably occur. It will end up.
It's usually okay because it's a very small error, but be aware that if it piles up, it can lead to big problems.
By the way, it's okay to use double normally: smile:
Use a type called boolean to represent a boolean value. It is used when there is a choice between Maru or X, true or false.
If you want to handle only one character, use the type called char. Only one character, full-width or half-width. ** Use'' when using ** (Example:'Mountain', '1',' A', etc.) ** Until now, the characters used "" (double quotation marks) Use'' (single quotes) when using char. ** **
Use String if you want to handle more than one character. Enclose the characters in "" (double quotation marks).
Variables can be overwritten, as described in Overwriting Variables. However, there are times when you do not want it to be overwritten. In that case, use ** constant **. If you use a constant, you don't have to worry about overwriting the value.
Declaration of constants.
final type constant name=initial value;
I think it's kind of complicated ... Once created, it's not complicated at all: smile: By the way, the constant name must be entered in all capital letters.
Example.java
public class Main {
public static void main(String[] args) {
final int NUMBER = 10;
System.out.println(NUMBER);
}
}
console.
10
I think it's similar to how to fill in variable initialization Just add final to the very beginning and capitalize the constant name: smile: By the way, if you try to overwrite the constant, a compile error will occur and you will not be able to overwrite it: smile:
Recommended Posts