Java primitive types, reference types, Immutable, Mutable

Understand Java primitive types, reference types, Immutables, and Mutables together

I'm studying Java Silver, but I was confused because this area came out apart, so I tried to summarize my own understanding.

Primitive type

Type Value Bit
boolean Boolean value 1
byte integer 8
short integer 16
char letter 16
int integer 32
float Floating point 32
long integer 64
double Floating point 64

Reference type

This mainly includes classes, Strings and StringBuilder.

Let's try Let's actually check it.

Main.java


class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		StringBuilder sb = new StringBuilder("apple");
		sample(sb);
		System.out.println(sb); // "apple orange"
		
		StringBuilder ssb = new StringBuilder("apple");
		sample5(ssb);
		System.out.println(ssb); // "apple"
		
		String s = "apple";
		String t = sample2(s);
		System.out.println(s); // "apple"
		System.out.println(t); // "apple orange"
		
		int i = 1;
		sample3(i);
		System.out.println(i); //1

		
	}	
		public static StringBuilder sample(StringBuilder sb) {
		    return sb.append(" orange");
		}
		
		public static String sample2(String s) {
		    return s+" orange";
		}
		
		public static int sample3(int i) {
		    return i+10;
		}
	    
	    public static StringBuilder sample5(StringBuilder sb) {
	        return new StringBuilder(sb+" orange");
	    }
}

Immutable vs Mutable What I would like to confirm here is the difference between Immutable String and Mutable StringBuilder even if they have the same reference type.

In the code below, "apple orange" is included for sb, which means that the StringBuilder passed as an argument is passed as a reference, not the value "apple", but the address of sb. The value of sb itself has changed.

        StringBuilder sb = new StringBuilder("apple");
        sample(sb);
        System.out.println(sb); // "apple orange"

    public static String sample2(String s) {
            return s+" orange";
        }

On the other hand, String is also a reference type, but the value of String passed as an argument has not changed. why. This is because StringBuilder is Mutable and String is Immutable. For String, the value once assigned at initialization cannot be changed after that, so if you try to change the original value of String with + etc., a new instance is returned internally, and the reference destination assigned first The value of does not change. If you want to use the changed value, you have no choice but to create a new String variable and assign it to it as shown below.

        String s = "apple";
        String t = sample2(s);
        System.out.println(s); // "apple"
        System.out.println(t); // "apple orange"

        public static String sample2(String s) {
            return s+" orange";
        }

As I mentioned earlier, int is a primitive type, so it goes without saying. Since it is not the referenced address that is passed to the argument, but the actual value, only the value is returned for the value passed to the argument, so the original value does not change.

Recommended Posts

Java primitive types, reference types, Immutable, Mutable
About Java primitive types and reference types
I investigated Java primitive types
Basic data types and reference types (Java)
Java array variables are reference types
Java basic data types and reference types
Java8 method reference
java8 method reference
JAVA reference materials
My Java reference
About Java data types (especially primitive types) and literals
mutable and immutable objects
About Java basic data types and reference type memory
Java Primitive Specialization Summary
[Java] Main data types
Equivalence comparison of Java wrapper classes and primitive types
[Java] Variables and types
Java basic data types
Generate Stream from an array of primitive types in Java
About Java class loader types
Java Learning 1 (learning various data types)