[Java] Data type ①-Basic type
Java variables
- Variable declaration: Notifies the name of the variable to Java and allocates an area to store the value in memory.
- ʻInt data (= initial value) `
- Multiple variables can be declared together
- ʻInt data1, date2 (= initial value) `
- Try to set the initial value
int data1= 108;
String message = “Hello World”;
- Identifier
- UCS2 is used inside Java.
- All Unicode characters are available
- Case Sensitive
- Variable names are basically readable and ideally readable in English
*
if (obj.isCompleted()){
- By convention, variables are ** camelClass **, classes are ** Pascal **, constants are ** uppercase + underscore notation **
constant
- ** Variables that cannot be reassigned **
//Bad example
int price = 1000;
double sum = price * 1.08;
- By using constants, the meaning of the calculation formula is easy to understand and it is easy to change it.
- I want to avoid typing a character string directly in the code → Put it out as a constant
//Refactoring example
final double TAX = 1.08;
int price = 1000;
double sum = price * TAX;
System.out.println(sum);
Java data type
- Statically typed language: Determines the type when declaring a variable
- ** Basic type **: The value itself is stored
- ** Reference type **: Stores information that indicates the storage location of the value. Java is basically a reference type
- There are basic types and reference types that correspond to all
- boolean: Basic type Boolean: Reference type
- int: Basic type Integer: Reference type
Integer type: Use basic int
- byte, char: Store byte data
- short: Represents a small integer
- long: Only when dealing with a numerical range that cannot be supported by int
Floating point type: use basic double
-
Occupies 64bit memory
-
For floating-point types, the jump width of the value increases as the absolute position increases.
-
IEEE 754 defines the value retention format
-
Normalized, decomposed into mantissa and exponent format and managed
123.75_{(10)}=1111011.11_{(2)}=1.11101111*2^{6}_{(2)}
-
$ 1.23 * 10 ^ 1 $ ~ $ 1.24 * 10 ^ 1 $ and $ 1.23 * 10 ^ {100} $ ~ $ 1.24 * 10 ^ {100} $ will make a difference! !!
Character type
- ** Char type **: 16bit --Unicode (UCS2) 1 character
- ** String type **: Variable length, multiple of 2
Boolean value (boolean, logical type)
- Cannot be converted to a numeric type (true = 1, false = 0, etc.)
Type inference
var variable name = initial value
var i = 48;
- Data type can be omitted
- The compiler automatically infers the shoulder from the assigned value and determines = type inference
- The above example infers an int
- Initial value cannot be omitted
Cannot use var on variable without initializer
- Cannot declare multiple
- Only local variables (variables declared in methods) that cannot be used in field declarations
What is a literal?
- ** The value itself that can be stored in the data type, the representation method **
- Integer literals, floating point literals, truth literals, etc., depending on the data type
Integer literal
- Decimal / 16-base / 8-base / 2-base literal
Floating point literal
- $ E
$
- 1.14142e-5 → 0.0000114142
Type suffix
- If you do not use numeric literals, integers are ints and floating point numbers are double.
- Used when you are in trouble if the data type is decided arbitrarily
- Add a shoulder suffix to a numeric literal
System.out.println (2147483648L); // in the long range
Numerical separator (delimiter)
- To improve the readability of characters with a large number of digits
var pi = 3.141_592_653_59;
- Note that methods that receive numeric strings (such as Integer.parseInt) cannot recognize separators.
Character literal
- Expressed as a single quote
- unicode characters are multiplied by hexadecimal character code in
\ u3042
format.
String literal
- Expressed in double quotes
- Use escape sequences when you want to put double quotes inside
System.out.println("You are \"GREATE\" !");
System.out.println("Welcome to wherever you are \nThis is your life, you made it this far");
var str = "Welcome, you got to believe " + "That right here, right now"; //Separated for readability improvement
System.out.println(str);
Type conversion
Statically typed but type conversion allowed
- ** Dilation **
- int to long
- Note that conversion from an integer to a floating point may cause mold loss.
- ** Reduced conversion (cast, explicit conversion) **
- Explicitly indicate the intention of conversion
- Note that the sign may change due to the conversion between int and short.
int i = 10;
// byte b = i ; //NG
byte b = (byte)i ; //OK