Home Java javaTutorial Primitive types vs references in Java and the immutability of Strings

Primitive types vs references in Java and the immutability of Strings

Sep 11, 2024 am 06:43 AM

In Java we have two types of data (or variables): primitives and non-primitives (also called references).

The primitive types have their literal values ​​stored in the Stack, temporary and short-term storage memory, managed by the Java Virtual Machine (JVM). [read more about memory types here]

Primitive variables are divided into four groups:

1. Integer types: used to store integers (without decimal part). They are: byte, short, int, long. Long has the letter "L" or "l" at the end of the number, for differentiation.

2. Floating point types:: Used to store numbers with decimal part (real numbers). They are: float, double. The float has the letter "F" or "f" at the end of the number, for differentiation.

3. Character type: Used to store single characters (such as letters, digits or symbols): char. They are initialized with single quotes '', instead of double "".

4. Boolean type: Used to store logical values ​​(true or false): bool

See the table below for the range of values ​​that each type has, in addition to their "default" values:

Tipos primitivos vs referências em Java e a imutabilidade das Strings
In scientific format, E represents an exponent. For example, 1.23E+10 is equal to 1.23 x 10^10

What is a default value? It is the value that the variable will assume if it has not been initialized. To assume this value, however, it needs to be global or constant (final).

public final boolean isTrue;
Copy after login

In this line of code, the variable "isTrue" was not initialized, but the compiler will not present an error, as it will consider the default value "false" for the Boolean variable.

Here, an important warning: if the scope of the variable is local, that is, if it has been declared within a function, we, programmers, will be forced to assign a value to it. Otherwise, there will be a compilation error.

public void teste(){
        int i = 2;
        int j;

        if (i < 10){
            j = 5;
        }

        System.out.println(j);
    }
Copy after login

In this example, even though we know that "2 < 10" returns "true", the compiler, which never executes the codes it translates during its process, does not know that the condition is true and that the primitive variable "j" will always be initialized. This way, when trying to run the code, a compilation error will appear: "error: variable j might not have been initialized".

Memory addresses

The second data type in Java is called reference. These variables store a reference, that is, the memory address of an object, instead of storing its value directly, as occurs with primitive types. This storage occurs in Heap memory.

Reference types are classes, interfaces, enums and objects, in general.

Here, an addendum. The String that we use widely in our codes is a class, not a primitive type. Note that even the name is capitalized, as is the naming convention for classes in Java.

The String even has methods, such as length(), which returns the size of the text stored in it, charAt(int index), which returns the index of a character in the text, or substring(int beginIndex, int endIndex), which returns a piece of a string.

But, if you want to make manipulating primitive data easier, Java allows it too. For this, it has the Wrapper class, which already comes with a series of built-in methods to work with the basic types.

Wrappers basically have the same name as the primitive variable, however, with the first letter capitalized:

  • Byte to byte
  • Shorts for shorts
  • Integer to int
  • Long to long
  • Float to float
  • Double to double
  • Character to char
  • Boolean to boolean
public class WrapperExample {
    public static void main(String[] args) {
        String numeroStr = "123";
        Integer num1 = Integer.parseInt(numeroStr);
        Integer num2 = 200;

        int resultadoComparacao = Integer.compare(num1, num2);

        if (resultadoComparacao < 0) {
            System.out.println(num1 + " é menor que " + num2);
        } else if (resultadoComparacao > 0) {
            System.out.println(num1 + " é maior que " + num2);
        } else {
            System.out.println(num1 + " é igual a " + num2);
        }
    }
}
Copy after login

In this example code, the int wrapper is used to convert a string into a number (Integer.parse) and then compare it with another number (Integer.compare).

String, however, has a particularity that other classes do not have. She is immutable.

Let's reflect through this basic example:

public class Main {
  public static void main(String[] args) {

    String text1 = "Hello";
    String text2 = text1;

    System.out.println(text1); //output: Hello
    System.out.println(text2); //output: Hello

    text1 = "Weird";
    System.out.println(text1); //output: Weird
    System.out.println(text2); //output: Hello

    text2 = "World";
    System.out.println(text1); //output: Weird
    System.out.println(text2); //output: World

    TestClass test1 = new TestClass("propertyValue");
    TestClass test2 = test1;

    System.out.println(test1.getProperty()); //output: propertyValue
    System.out.println(test2.getProperty()); //output: propertyValue

    test2.setProperty("newValue");

    System.out.println(test1.getProperty()); //output: newValue
    System.out.println(test2.getProperty()); //output: newValue   
  }

}
Copy after login

In this case, note that, even though the String "text2" points to "text1", changes in "text2" did not reflect changes in "text1". Now, when the Object "test2", which pointed to "test1" had a property changed, this change was reflected in "test1" too.

嘿,但是引用变量不是存储内存地址而不是文字值吗?是的,他们存储它。 Java 语言开发人员决定让 String 变量保持不可变。这意味着,一旦定义, String 对象的值就不能被另一个对象间接更改。

因此,在示例中,我们不会更改 text1 先前引用的对象的值(因为 String 是不可变的)。相反,我们创建一个值为“Weird”的新 String 对象,并使 text1 指向这个新对象。 Text2 仍将指向原始的“Hello”对象,这就是它保留值“Hello”的原因。

简而言之,为字符串分配新值不会修改现有对象的值,它只是更改对新对象的引用。

自定义类的对象,例如 TestClass,是可变的。 test1 和 test2 引用都指向同一个对象,因此更改其中一个对象的状态会影响另一个对象。

The above is the detailed content of Primitive types vs references in Java and the immutability of Strings. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles