There are two syntaxes for defining arrays in Java:
type arrayName[]; type[] arrayName;
type is any data type in Java, including basic types and combined types, arrayName is the array name, [must be a legal identifier, [] indicates that the variable is an array type variable. For example:
int demoArray[]; int[] demoArray;
There is no difference between these two forms, and the usage effect is exactly the same. Readers can choose according to their own programming habits.
Java does not allocate memory for array elements when defining an array, so there is no need to specify the number of array elements, that is, the array length, in [ ]. Moreover, for an array defined above, we cannot access any of its elements. We must allocate memory space for it. At this time, we need to use the operator new, The format is as follows:
arrayName=new type[arraySize];
Among them, arraySize is the length of the array, and type is the type of the array.
For example:
demoArray=new int[3];
Allocate the memory space occupied by 3 int-type integers for an integer array.
Usually, you can allocate space while defining, the syntax is:
type arrayName[] = new type[arraySize];
For example:
int demoArray[] = new int[3];
Initialization of arrays
You can initialize the array while declaring it (static initialization), or you can initialize it after the declaration (dynamic initialization). For example:
// 静态初始化 // 静态初始化的同时就为数组元素分配空间并赋值 int intArray[] = {1,2,3,4}; String stringArray[] = {"Java", "http://www.java.com", "一切编程语言都是纸老虎"}; // 动态初始化 float floatArray[] = new float[3]; floatArray[0] = 1.0f; floatArray[1] = 132.63f; floatArray[2] = 100F;
Related learning recommendations: java basic tutorial
The above is the detailed content of How to define java integer array. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.