This article is a record of what I understand for those who are studying Java.
In my article, the main Java features are summarized in the list below.
-Variables, types, and type conversions Current page -Variable Scope ・ Character string operation (in preparation) ・ Array operation (in preparation) ・ Operator (in preparation) ・ Conditional branch (in preparation) ・ Repeat processing (in preparation) -Exception handling ・ About class (in preparation) ・ Abstract class (in preparation) ・ Interface (in preparation) ・ Encapsulation (in preparation) ・ About the module (in preparation)
In addition, the following articles are referred to in describing this article. https://www.sejuku.net/blog/22805
When declaring a variable in Java, it is necessary to describe which data type the variable is.
Entry example
int suuzi; //Numeric type
boolean dotti; //boolean type true or false
String mozi; //Character type
System.out.println(mozi); //Trying to look inside mozi
However, this description is a declaration and has not been initialized. An error will occur if there are variables that cannot be initialized in Java.
Error statement
error:The variable mozi may not have been initialized
int suuzi; //Numeric type
boolean dotti; //boolean type true or false
String mozi; //Character type
suuzi = 10;
dotti = true;
mozi = "letter";
System.out.println(suuzi); //Try to look inside the numbers
System.out.println(dotti); //Trying to look inside dotti
System.out.println(mozi); //Trying to look inside mozi
If you set the contents for the above, it will be initialized and no error will occur even if you try to refer to it.
Run
10
true
letter
It is also possible to write the above two steps together.
int suuzi = 10; //Numeric type
boolean dotti = true; //boolean type true or false
String mozi = "letter"; // letter型
System.out.println(suuzi); //Try to look inside the numbers
System.out.println(dotti); //Trying to look inside dotti
System.out.println(mozi); //Trying to look inside mozi
If you write the type variable name = contents as above, you can declare and initialize at the same time.
Run
10
true
letter
Up to this point, we have been talking about declarations and initialization, but from now on, we will describe types. There are two main types, basic type and reference type. Let's look at each.
Integer type We are dealing with integer data. It is easy to understand if you think that the larger the size, the more numerical values can be handled.
Data type | size | Range of values |
---|---|---|
byte | 1byte | -128~127 |
short | 2byte | -32768~32767 |
int | 4byte | -2147483648~2147483647 |
long | 8byte | -9223372036854775808~9223372036854775807 |
Looking at this table, I think there is an opinion that only longs with a large size should be used, but since the amount of bytes listed in the table is used regardless of the size of the value, it can be seen that a small value can be entered. I want to keep the number of bytes as small as possible. When I write it, there are many ints. If you find that it is not enough, it will be long. If it is byte or short, there are few, and I think that an error such as not entering a value may occur.
Floating point type A number that includes a decimal point.
Data type | size | Range of values |
---|---|---|
float | 4byte | ±3.40E38~±1.40E45 |
double | 8byte | ±4.94E-324~±1.79E+308 |
Character type We are dealing with letters. The basic type char is used to handle one character.
Data type | size | Range of values |
---|---|---|
char | 2byte | 2byte character code |
Boolean type (boolean type) We are dealing with truthful logical information. Contains either true or false.
Data type | size | Range of values |
---|---|---|
boolean | 1bit | true or false |
It is classified into these four. Another characteristic of the basic type is that the variable itself contains a value.
Types other than the above, basically String Array class Is targeted. Unlike the basic type, this does not contain the value in the variable itself, but the place where it is stored. I think it's easy to think of the reference type analogy as an array, so https://www.javadrive.jp/start/array/index3.html Let's follow the code in the link above.
class Main{
public static void main(String args[]){
int n[];
int m[];
n = new int[2];
System.out.println(n);
n[0] = 10;
m = n;
System.out.println(m);
System.out.println("n[0] = " + n[0]);
System.out.println("m[0] = " + m[0]);
}
}
When you run
[I@69379752
[I@69379752
n[0] = 10
m[0] = 10
It will be displayed. What is displayed in the top two is the place where the reference type is entered. The fact that they are the same means that they are looking at the same thing, so n and m are looking at the same array.
That's all for the basic type and reference type. Next, I will describe the cast.
These data types can also be converted and passed. I actually described it and saw it.
int i = 30;
System.out.println(i);
With the above description
30
Is displayed,
int i = 30;
System.out.println((double)i);
When executed with
30.0
It will be. Since the integer type has a decimal point, it has changed to a floating point type (double).
However, please note that there are some cases that cannot be converted.
class Main{
public static void main(String args[]){
String str = "letter";
System.out.println((int)str);
}
}
When you run
error:Incompatible type:Unable to convert String to int:
System.out.println((int)str);
^
It will be displayed. It is basically not possible to make a reference type a base type.
In terms of numerical type, it is also related to the size relationship.
class Main{
public static void main(String args[]){
int i = 2147483647; //Maximum value of int
System.out.println(i);
i += 1;
System.out.println(i);
}
}
When you run
2147483647
-2147483648
And it overflows because it exceeds the maximum value of int. It
class Main{
public static void main(String args[]){
int i = 2147483647; //Maximum value of int
System.out.println(i);
System.out.println((long)i + 1);
}
}
Here, i is cast to make it a long that contains many numbers, and then 1 is added. When you do this
2147483647
2147483648
1 can be added without overflowing like.
This may also suddenly result in an unintended error, so I will also explain it here. When I calculate the type of byte or short, it suddenly becomes an int type.
class Main{
public static void main(String args[]){
byte b1 = 10;
byte b2 = 10;
byte kekka = b1 * b2;
System.out.println(kekka);
}
}
test.java:8:error:Incompatible type:Int to byte conversion that can lose precision
byte kekka = b1 * b2;
^
Especially in the code, although there is no description such as conversion to int, an error of conversion from int to byte appears. Let's change the type of the kekka variable we receive to int to see if the type has really changed.
class Main{
public static void main(String args[]){
byte b1 = 10;
byte b2 = 10;
int kekka = b1 * b2;
System.out.println(kekka);
}
}
100
I was able to output. I calculated with byte and byte, but you can see that the result is int. Be careful when you receive the numbers.
This article delves into a lot about molds. As for types, Java is always mentioned, so I want to make sure that I understand it.
Recommended Posts