[Java] Data type ①-Basic type

Java variables

constant

//Bad example
int price = 1000;
double sum = price * 1.08;
//Refactoring example
final double TAX = 1.08;
int price = 1000;
double sum = price * TAX;
System.out.println(sum);

Java data type

Integer type: Use basic int

Floating point type: use basic double

Character type

Boolean value (boolean, logical type)

Type inference

What is a literal?

Integer literal

Floating point literal

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 

Recommended Posts

[Java] Data type ①-Basic type
Java basic data types
Java learning memo (data type)
Basic data type and reference type
About Java basic data types and reference type memory
Basic data types and reference types (Java)
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
Java basic data types and reference types
Java type conversion
[JAVA] Stream type
Java basic grammar
[Java] Enumeration type
Java basic grammar
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java double type
Java basic grammar
Java basic grammar
Java exercises [Basic]
Java date data type conversion (Date, Calendar, String)
Use PostgreSQL data type (jsonb) from Java
[Personal memo] Java data type is annoying
[Java] Data type / string class cheat sheet
[Basic knowledge of Java] About type conversion
java basic knowledge memo
Java basic date manipulation
[Processing x Java] Data type and object-oriented programming
Java basic naming conventions
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
Java learning memo (basic)
Organized memo in the head (Java --Data type)
[Java] Main data types
[Java] About enum type
[Java] Basic method notes
Java review ③ (Basic usage of arrays / reference type)
Basic Java OOps concepts
[Java] Date type conversion
Java variable declaration, initialization, data type (cast and promotion)
Java review ① (development steps, basic grammar, variables, data types)
Java basic learning content 7 (exception)
[Java] List type / Array type conversion
Basic Authentication with Java 11 HttpClient
Try functional type in Java! ①
Java programming (variables and data)
Java basic learning content 5 (modifier)
[Java] Precautions for type conversion
Importing Excel data in Java 2
[Java] Type conversion speed comparison
[Java] Thymeleaf Basic (Spring Boot)
Implement Basic authentication in Java
Java study # 7 (branch syntax type)
Import Excel data in Java
Java addition excel data validation
Java for beginners, data hiding
Importing Excel data in Java 3
Java Primer Series (Type Conversion)
Java Basic Learning Content 8 (Java API)