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
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.
/**
*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
http://teachingprogramming.net/archives/647 https://nobuo-create.net/sanshougata/
Recommended Posts