For those who have just started learning programming including the Java language, and those who have already learned it, for review I'm writing to learn variable declaration, initialization, and data types.
[Introduction to Java Table of Contents] This time it's ** about variables and types **.
・ Variables and types ← Now here ・ Type conversion -Variable Scope -String operation -Array operation ・ Operator ・ Conditional branch (in preparation) ・ Repeat processing (in preparation) ・ About class (in preparation) ・ Abstract class (in preparation) ・ Interface (in preparation) ・ Encapsulation (in preparation) ・ About the module (in preparation) -Exception handling ・ About lambda expression ・ About Stream API
What is a variable? `A container for holding data used in the program. ``
To distinguish it from other variables
In the Java language, the following should be noted when declaring variables:
-What kind of values are stored, such as numbers and character strings ・ What kind of name should I use? ・ What is the first value to be stored?
Data type variable name;
Must be.
(The data type will be described later)
Main.java
class Main {
public static void main(String args[]) {
//Variable declaration
int num;
//I get an error when I try to output
System.out.println(num); // The local variable num may not have been initialized
}
}
This error is a variable declaration but not initialized, Occurs because you used a variable that has no value set.
After making data type variable name;
Variable name = value;
Must be.
Main.java
class Main {
public static void main(String args[]) {
//Variable declaration
int num;
//Variable initialization(Substitute 10 for num)
num = 10;
//output
System.out.println(num); // 10
}
}
Data type variable name = value;
It is also possible to.
Main.java
class Main {
public static void main(String args[]) {
//Variable declaration and initialization
int num = 10;
//output
System.out.println(num); // 10
}
}
-Only the following characters can be used as the first character. Alphabet (a ~ z, A ~ Z), dollar sign ($), underscore (_)
・ Numbers can be used after the second character.
-Case sensitive is strictly distinguished.
-There is no limit on the number of characters (length).
-Null, true, false cannot be used.
・ Reserved words cannot be used.
Reserved words are names that are already used in the Java language. Example: if, while, switch, etc.
Details about reserved words: Official document
In Java, you had to specify the data type
when declaring a variable.
The data types are roughly classified as follows.
・ Basic data type (primitive type)
・ Reference type
Let's look at each.
Mold | size | Range of values |
---|---|---|
boolean | 1 bit | true/Either false |
Main.java
class Main {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
System.out.println(a); // true
System.out.println(b); // false
}
}
We are dealing with integer data.
Mold | size | Range of values |
---|---|---|
byte | 8 bits | -128〜127 |
short | 16 bit | -32768~32767 |
int | 32 bit | -2147483648~2147483647 |
long | 64-bit | -9223372036854775808~9223372036854775807 |
Main.java
System.out.println("Minimum value of byte= " + Byte.MIN_VALUE); // Minimum value of byte= -128
System.out.println("Maximum value of byte= " + Byte.MAX_VALUE); // Maximum value of byte= 127
System.out.println("Minimum value of short= " + Short.MIN_VALUE); // Minimum value of short= -32768
System.out.println("Maximum value of short= " + Short.MAX_VALUE); // Maximum value of short= 32767
System.out.println("Minimum value of int= " + Integer.MIN_VALUE); // Minimum value of int= -2147483648
System.out.println("Maximum value of int= " + Integer.MAX_VALUE); // Maximum value of int= 2147483647
System.out.println("Minimum value of long= " + Long.MIN_VALUE); // Minimum value of long= -9223372036854775808
System.out.println("Maximum value of long= " + Long.MAX_VALUE); // Maximum value of long= 9223372036854775807
The relationship is byte <short <int <long
.
By the way, if the minimum and maximum values are exceeded, the values will be inverted. (Overflowing digits)
Main.java
System.out.println("Minimum value of previous byte= " + Byte.MIN_VALUE); //Minimum value of byte= -128
byte minByte = Byte.MIN_VALUE;
minByte--;
System.out.println("And the minimum value of byte= " + minByte); //Minimum value of byte=127 ← inverted
System.out.println("Maximum value of previous byte= " + Byte.MAX_VALUE); //Maximum value of byte= 127
byte maxByte = Byte.MAX_VALUE;
maxByte++;
System.out.println("And the maximum value of byte= " + maxByte); //Maximum value of byte= -128 ← inverted
It is used when calculating integers.
Main.java
int a = 1;
int b = 1;
int sum = a + b;
System.out.println(sum); // 3
When storing an integer that exceeds the int type in a variable, add "L" or "l" at the end.
Main.java
//I get an error
long longNumber = 214748364712342; // The literal 214748364712342 of type int is out of range
long longNumber = 214748364712342L; //Must be suffixed with "L" or "l".
System.out.println(longNumber); // 214748364712342
We are dealing with numerical data including a decimal point.
Mold | size | Range of values |
---|---|---|
float | 32 bit | ±1.40239846×10^-45~±3.40282347×10^38 |
double | 64-bit | ±4.94065645841246544×10^-324~±1.79769313486231570×10^308 |
Main.java
System.out.println("Minimum value of float= " + Float.MIN_VALUE); // Minimum value of float= 1.4E-45
System.out.println("Maximum float= " + Float.MAX_VALUE); // Maximum float= 3.4028235E38
System.out.println("Minimum value of double= " + Double.MIN_VALUE); // Minimum value of double= 4.9E-324
System.out.println("Maximum value of double= " + Double.MAX_VALUE); // Maximum value of double= 1.7976931348623157E308
It is used when doing a small number of calculations.
Main.java
//int type division
int a = 10;
int b = 3;
int result1 = a / b;
System.out.println(result1); // 3
//Float type division
//Floating point numbers are recognized as double type by default
// float c = 10.0; // Type mismatch: cannot convert from double to float
float c = 10F; //Therefore, when assigning to a float type variable, add F or f at the end.
float d = 3f; //Same as above
float result2 = c / d;
System.out.println(result2); // 3.3333333
//Double type division
double e = 10.0D; //Add D or d to the end
double f = 3.0; //However, as mentioned above, it is recognized as dobule type by default, so there is no problem even if it is omitted.
double result3 = e / f;
System.out.println(result3); // 3.3333333333333335
It is used to represent one character.
Data type | size | Range of values |
---|---|---|
char | 16 bit | 16-bit character code |
It is used to handle one character.
Enclose in single quotes. It must be one character.
Main.java
//I get an error
char a = 'Ah'; // Invalid character constant
char a = "Ah"; // Type mismatch: cannot convert from String to char
char a = 'Ah';
System.out.println(a); //Ah
All types except basic data types, including classes, arrays, interfaces, etc., are reference types.
The variable does not hold the value itself, but the place where the value is located. Reference type because it refers to where it is stored.
Test.java
public class Test {
public int number = 0;
}
Main.java
class Main {
public static void main(String args[]) {
Test test = new Test();
test.number = 10;
System.out.println(test.number); // 10
Test test2 = new Test();
test2.number = 10;
System.out.println(test2.number); // 10
test = test2; //The value of test2 is not assigned to test, but the place where the value of test2 is placed.
test.number = 99; //Therefore, if you change test to 99 here, it will be changed to test2.
System.out.println(test.number); // 99
System.out.println(test2.number); // 99
}
}
test = test2;
The value of test2 is not assigned to test, but the place where the value of test2 is placed
is assigned.
test.number = 99; Therefore, if you change test to 99, it will be changed to test2.
You can treat the value of the basic data type as a reference type.
Although it is a reference type, it is the same as the basic data type and its value does not change. The value itself is assigned, not the place where the value is placed.
Main.java
class Main {
public static void main(String args[]) {
Integer num = 10;
Integer num2 = 10;
System.out.println(num); // 10
System.out.println(num2); // 10
num = num2;
num = 99;
System.out.println(num); // 99
System.out.println(num2); // 10
String greet = "Good morning";
String greet2 = "Hello";
System.out.println(greet); //Good morning
System.out.println(greet2); //Hello
greet = greet2;
greet = "Good Morning";
System.out.println(greet); // Good Morning
System.out.println(greet2); //Hello
}
}
You learned about variable declaration, initialization, and data types. Next time, I will delve into Type conversion.