Basic data types and reference types (Java)

Basic data type

Mold Contents
char 16-bit Unicode character data
boolean Boolean value(true or false)
byte 8-bit signed integer
short 8-bit signed integer
int 32-bit signed integer
long 64-bit signed integer
float 32-bit signed floating point number
double 64-bit signed floating point number
public class Main1 {
    public static void main(String[] args) {
        // (1)Primitive type declaration and initialization
        int a = 0;
        int b = 1;
        System.out.println("a: " + a);
        System.out.println("b: " + b);

        // (2)Primitive type assignment
        b = a;
        System.out.println("a: " + a);
        System.out.println("b: " + b);

        // (3)Primitive type assignment
        a = 2;
        System.out.println("a: " + a);
        System.out.println("b: " + b);
    }
}
public static void main(String[] args) {
    int a = 1;
    int b = a;
    b = 2;
    System.out.println(a);
    System.out.println(b);
}
1
2

Reference type

public static void main(String[] args) {
    int[] a = {1, 2, 3};
    int[] b = a;
    b[0] = 5;
    System.out.println(a[0]);
    System.out.println(b[0]);
}
5
5

The elements of the array are irrelevant because both arrays a and b represent the same memory address.

For class

/**
 *Class representing a circle
 */
public class Circle {
    /**Field representing the radius*/
    public int radius;
}
public class Main2 {
    public static void main(String[] args) {

        // (4)Reference type instantiation and field assignment
        Circle c1 = new Circle();
        c1.radius = 0;
        Circle c2 = new Circle();
        c2.radius = 1;
        System.out.println("c1.radius: " + c1.radius);
        System.out.println("c2.radius: " + c2.radius);

        // (5)Reference type assignment
        c2 = c1;
        System.out.println("c1.radius: " + c1.radius);
        System.out.println("c2.radius: " + c2.radius);

        // (6)Assign to a reference type field
        c1.radius = 2;
        System.out.println("c1.radius: " + c1.radius);
        System.out.println("c2.radius: " + c2.radius);
    }
}
c1.radius:0
c2.radius:1
c1.radius:0
c2.radius:0
c1.radius:2
c2.radius:2

reference

http://teachingprogramming.net/archives/647 https://nobuo-create.net/sanshougata/

Recommended Posts

Basic data types and reference types (Java)
Java basic data types and reference types
About Java basic data types and reference type memory
Java basic data types
Basic data type and reference type
[Java] Basic types and instruction notes
About Java primitive types and reference types
[Java] Exception types and basic processing
[Java] Data type ①-Basic type
[Java] Main data types
[Java] Variables and types
Java programming (variables and data)
Java Learning 1 (learning various data types)
Java review ① (development steps, basic grammar, variables, data types)
Java Primer Series (Variables and Types)
Java primitive types, reference types, Immutable, Mutable
Java reference mechanism (stack and heap)
Java array variables are reference types
Java pass by value and pass by reference
Reading and writing Java basic files
[Java] Difference between assignment of basic type variable and assignment of reference type variable
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
Java starting from beginner, variables and types
[Note] Cooperation between Java and DB (basic)
[Introduction to Java] Variable declarations and types
Page number logic and reference code (java)
Java basic grammar
Java basic grammar
Java8 method reference
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java and JavaScript
XXE and Java
Java basic grammar
java8 method reference
JAVA reference materials
My Java reference
Java exercises [Basic]
[Java] Personal summary of classes and methods (basic)
[Processing x Java] Data type and object-oriented programming
Java review ③ (Basic usage of arrays / reference type)
Feel the basic type and reference type easily with ruby
Getters and setters (Java)
[Java] Types of comments and how to write them
[Java] Thread and Runnable
Java variable declaration, initialization, data type (cast and promotion)
Java true and false
java basic knowledge memo
Vectorize and image MNIST handwritten digit image data in Java
[Java] String comparison and && and ||
Java basic date manipulation
Going back to the beginning and getting started with Java ① Data types and access modifiers
Java basic naming conventions
Java learning memo (basic)
Java --Serialization and Deserialization
[Java] Arguments and parameters
I summarized the types and basics of Java exceptions
Feel the basic type and reference type easily with ruby 2
timedatectl and Java TimeZone