


How Does Java's Array Assignment Syntax Differ Inside and Outside of Declarations?
Array Assignment Syntax Outside of Declarations
Java's array initialization syntax differs depending on whether it's used in a declaration or an assignment.
Initializing with Array Literals
When declaring an array, array literals can be used to initialize its elements:
AClass[] array = {object1, object2};
Initializing with the new Operator
Outside a declaration, arrays can be initialized using the new operator, followed by the element count and subsequent assignments:
AClass[] array = new AClass[2]; array[0] = object1; array[1] = object2;
Syntax Restrictions: Assigning Array Literals to Non-Declared Arrays
However, attempting to assign an array literal to a non-declared array variable is prohibited:
AClass[] array; ... array = {object1, object2}; // Blocked by Java
This restriction is imposed by Java for reasons that may not be immediately apparent.
Workaround: Using new Operator with Array Literals
As a workaround, the following syntax can be used:
AClass[] array; ... array = new AClass[]{object1, object2};
This approach combines the array creation and initialization into a single statement while adhering to Java's syntax rules.
Example
For example, consider the following code snippet:
public void selectedPointsToMove(cpVect coord) { if (tab == null) { if (arePointsClose(coord, point1, 10)) { cpVect[] tempTab = {point1}; tab = tempTab; } else if (arePointsClose(point2, coord, 10)) { cpVect[] tempTab = {point2}; tab = tempTab; } else { cpVect[] tempTab = {point1,point2}; tab = tempTab; } } }
Without the workaround, the code would require additional statements to initialize tempTab before assigning it to tab.
The above is the detailed content of How Does Java's Array Assignment Syntax Differ Inside and Outside of Declarations?. For more information, please follow other related articles on the PHP Chinese website!

Deployment method of external configuration files of SpringBoot3 project In SpringBoot3 project development, we often need to configure the configuration file application.properties...

Configuration method for converting Apache's .htaccess configuration to Nginx In project development, you often encounter situations where you need to migrate your server from Apache to Nginx. Ap...

JavaWeb application performance optimization: An exploration of the feasibility of Dao-level entity-class caching In JavaWeb application development, performance optimization has always been the focus of developers. Either...

Solving double integrals under polar coordinate system This article will answer a question about double integrals under polar coordinates in detail. The question gives a point area and is incorporated...

How to ensure the uniqueness of script tasks and monitor their operating status in a high concurrency environment? This article will explore how to ensure an outbound foot in a cluster environment...

Regarding how subclasses set private properties by inheriting the setName method of the parent class. In programming, especially in object-oriented programming languages such as Java, subclasses and...

How to solve the problem of username and password authentication failure when connecting to local EMQX using EclipsePaho's MqttAsyncClient? Using Java and Eclipse...

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. ...


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool