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:
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 (i <p>In this example, even though we know that "2 </p><h2> Memory addresses </h2> <p>The second data type in Java is called <strong>reference</strong>. 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.</p> <p> Reference types are classes, interfaces, enums and objects, in general. </p> <p>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.</p> <p>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.</p> <p>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.</p> <p>Wrappers basically have the same name as the primitive variable, however, with the first letter capitalized:</p>
- 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 + " é 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).
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 } }
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!

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools
