Home> Java> javaTutorial> body text

Primitive types vs references in Java and the immutability of Strings

WBOY
Release: 2024-09-11 06:43:32
Original
192 people have browsed it

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

Theprimitive typeshave 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 calledreference. 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!

source:dev.to
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!