About Java data types (especially primitive types) and literals

We have started studying for the Oracle Java Programmer, Silver SE11 certification. I would like to summarize the data types that came out in that. We would appreciate it if you could let us know if you have any suggestions, such as mistakes.

Primitive type

Mold Classification
boolean Logical type True/False
char Character type 16-bit Unicode,¥u0000~¥uFFFF
byte Integer type 8-bit integer,-128~127
short Integer type 16-bit integer,-32768~32767
int Integer type 32-bit integer,-2147483648~2147483647
long Integer type 64-bit integer,-9223372036854775808~9223372036854775807
float Floating point type 32ビット単精度Floating point type
float Floating point type 64ビット倍精度Floating point type

Reference type

Reference types include class types, interface types, and array types.

Primitive type literal

Literals are the values you write in your source code. By default, Java literals are int for integers, double for floating point numbers, boolean for booleans, and char for characters.

When you want to specify the data type (integer, floating point number)

If you want to specify the data type, use the suffix at the end of the value or the prefix at the beginning of the value.

--Long type: Add L or l after the value. --Float type: Add F or f after the value.

It's a notation method rather than a type ...

--Write in binary ... Add 0b to the beginning of the value. --Write in octal number ... Add 0 to the beginning of the value. --Describe in hexadecimal ... Add 0x to the beginning of the value.

Not in byte type or short type.

Numerical notation using underscore (_)

It seems that it was introduced from Java SE 7. This is just to improve visibility. However,

-Cannot be written at the beginning and end of literals -Cannot be written before or after the symbol

Cannot be written at the beginning and end of a literal Cannot be written before or after the symbol

Concrete example

Main.java



    double a = 123_4567.89;
    System.out.println(a);

If you do, it will be displayed as 1234567.89. It seems to be okay at all even if it is not separated by 3 digits.

Main.java



    double a = 123_4567_.89;
    System.out.println(a);

If you do, an error will occur.

Character literal

The char type is a data type that represents one character, and must be enclosed in single quotation marks such as'a'. By the way, the character string must be enclosed in double quotation marks like "neko".

In Unicode notation, it is written in 4 hexadecimal digits after the \ u prefix.

Main.java


    char a = '\u1111';
    System.out.println(a);

Was output. I can't read w

that? By the way, what about the String type?

It is a form that uses the java.lang.String class that belongs to the java.lang package. As a special case, you can use a String instance by enclosing it in "" without using the new operator.

References

A refreshing introduction to Java Thorough capture Java SE11 Silver problem collection

Recommended Posts

About Java data types (especially primitive types) and literals
About Java primitive types and reference types
About Java basic data types and reference type memory
Basic data types and reference types (Java)
Java basic data types and reference types
About Java literals
[Java] Main data types
[Java] Variables and types
Java basic data types
Equivalence comparison of Java wrapper classes and primitive types
A memorandum about table data types and commands (Rails)
Java programming (variables and data)
About Java class loader types
I investigated Java primitive types
[Java] About String and StringBuilder
About Java Packages and imports
Java Learning 1 (learning various data types)
About Java static and non-static methods
[Java] Basic types and instruction notes
About fastqc of Biocontainers and Java
Java Primer Series (Variables and Types)
[Java beginner] About abstraction and interface
Java primitive types, reference types, Immutable, Mutable
This and that about Base64 (Java)
[Java] Exception types and basic processing
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
Java starting from beginner, variables and types
[About JDBC that connects Java and SQL]
[Introduction to Java] Variable declarations and types
Rails: A little summary about data types
[Introduction to Java Data Structures] Create your own fast ArrayDeque for primitive types
[Rails] I learned about database data type types!
[Processing x Java] Data type and object-oriented programming
About Java interface
[Java] About arrays
A memo about the types of Java O/R mappers and how to select them
Going back to the beginning and getting started with Java ① Data types and access modifiers
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
About Java class
Java and JavaScript
About Java arrays
XXE and Java
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java commands
[Java] Types of comments and how to write them
Java variable declaration, initialization, data type (cast and promotion)
Vectorize and image MNIST handwritten digit image data in Java
Java review ① (development steps, basic grammar, variables, data types)
[Java] About Objects.equals () and Review of String comparisons (== and equals)
I summarized the types and basics of Java exceptions
About CLDR locale data enabled by default from Java 9
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
About Java setters and getters. <Difference from object orientation>
About synchronized and Reentrant Lock (Java & Kotlin implementation example)