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. The long has the letter "L" or "l" at the end of the number, for differentiation.
2. Floating point types: : Used to store numbers with a 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 supports, in addition to their "default" values:
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;
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 (iIn this example, even though we know that "2
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:
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 " é maior que " num2); } else { System.out.println(num1 " é igual a " num2); } } }
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).
A String, however, has a particularity that other classes do not have. It 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 } }
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.
Hey, but don't reference variables store memory addresses, instead of literal values? Yes. They store it. What happens is that the Java language developers made the decision to leave String variables immutable. This means that once set, the value of a String object cannot be changed indirectly by another object.
In the example, therefore, we are not changing the value of the object that text1 previously referenced (since String is immutable). Instead, we are creating a new String object with the value "Weird" and making text1 point to this new object. Whereas text2 will still point to the original "Hello" object and that's why it keeps the value "Hello".
In short, assigning a new value to a string does not modify the value of the existing object, it just changes the reference to a new object.
Objects of custom classes, such as TestClass, are mutable. Both test1 and test2 references point to the same object, so changing the state of one of them reflects on the other.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3