This article was written for people who were programming in other languages and would like to program in Java. This time, I will explain about variables.
A variable is an area that stores data. Roughly speaking, it's like a box where you can put values.
Listed for each variable type.
Data type name | name | Memory consumption | range |
---|---|---|---|
byte | Byte type | 1 byte | -128~127 |
short | Short integer type | 2 bytes | -32768~32767 |
int | Integer type | 4 bytes | -2147483648~2147483647 |
long | Long integer type | 8 bytes | -9223372036854775808~ 9223372036854775807 |
float | Single precision floating point type | 4 bytes | ±3.40282347E+38~ ±1.40239846E-45 |
double | Double precision floating point type | 8 bytes | ±1.79769313486231570E+308~ ±4.94065645841246544E-324 |
Data type name | name | Memory consumption | range |
---|---|---|---|
char | Character type | 2 bytes | ¥u0000~¥uFFFF |
String | String type | 4 bytes(8 bytes for 64-bit JVM) | Reference type |
Data type name | name | Memory consumption | range |
---|---|---|---|
boolean | Boolean type | 1 byte | true(true)/false(false) |
Variables require a variable name and a data type name. The format is "Data type name (half-width space) variable name;". Example)
example.java
int i;
String s;
If you just declare it, only the value determined for each type will be entered. You need to enter the value you want to enter. The format is "variable name = desired value;". Initialization can be performed at the same time as the declaration. In that case, write "Data type name (half-width space) variable name = value you want to enter;".
example.java
int i;
String s;
i = 1;
s = "str string";
char c = 'Ah';
However, please be careful because the value that can be entered is fixed for each type. Also, you have to declare it first. bad example)
bad_example.java
int i;
String s;
i = "str string"; //String type to integer type
s = 2432; //String type to number type
c = 'Ah'; //No declaration
This is a summary of the contents of [Introduction to Java] Cast (type conversion), upcast, and downcast. Type conversion is the conversion of one type to another. Some are declared to be automatic and some are declared. First of all, the example below is automatic and easy to understand.
example.java
int i = 2;
float f = i;
System.out.println("int i = " + i);
System.out.println("float f = " + f);
System.out.println (); prints the characters in (). When you run this program int i = 2 float f = 2.0 Is output. In this case, i and f are treated as string types. In other words, it has been converted to a String type. Also, int type i can be assigned to float type f. This is because the value of i is automatically changed to float type. The current example is a type conversion (cast) that is performed automatically.
Next, if you do it yourself. Here's an example of what it's like to do it yourself.
example.java
String s1 = "9";
String s2 = "1";
System.out.println(s1 + s2);
When you run this program 91 Is output. Because it is a String type, the character string "9" and the character string "1" are combined into one character string. However, if you want to calculate and get 10 here, write as follows.
example.java
String s1 = "9";
String s2 = "1";
System.out.println(Integer.parseInt(s1) + Integer.parseInt(s2));
Integer.parseInt (); is a process to change the character string in () to int type. This makes it possible to calculate the values of s1 and s2 as int type, so the output result is 10 It will be.
In the case of number type, (type name) value can be exchanged.
number.java
int i = 1;
short s = (short)i;
float f = 2.4;
long l = (i+s+f);
System.out.println(l); //Output result = 5
Use String.valueOf () ;. Or connect it with a character string with +.
number.java
int i = 3;
System.out.println(String.valueOf(i)+2); //Output result = 32
System.out.println("number" + i); //出力結果=number3
Except for int type [Data type name (first letter only uppercase)] .parse Data type name (first letter only uppercase); int type Described as Integer.parseInt (character) ;.
number.java
String s = 03;
int i = Integer.parseInt(s);
int l = Long.parseLong(s);
System.out.println(i); //Output result = 3
System.out.println(l); //Output result = 3
There are the following points to note about type conversion. -Since type conversion only specifies the type of the received value, the value of the original variable is not changed even if the variable name is entered. -If the received value does not match the person you want to convert, an error will occur. -As a result of conversion, the value may change before and after conversion.
Example)
bad_example.java
String s1 = "sss9";
String s2 = "1";
String s3 = "03";
System.out.println(Integer.parseInt(s1) + Integer.parsparseInt(s2); //"sss"Error because the part is not an integer
System.out.println(Integer.parseInt(s3)); //Is output as 3. 0 disappears
System.out.println(s3); //03 is output. Integer on the top line.parseInt(s3)Is not affected by.
Recommended Posts