惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
B
Blog
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
爱范儿
爱范儿
博客园_首页
博客园 - 聂微东
量子位
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
The Cloudflare Blog
T
Tailwind CSS Blog
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC

博客园 - hone

管理 服务器相关 总统和亿万富翁经典语录(转载) js学习笔记 最近学习计划 VC控制Excel 真乱(转自csdn) 沟通(网上搜集) 工作 无题 将文档发送到打印机(From MSN) 经典68个故事 Manually Invalidating All Stale Sessions java的zip压缩(转自Jeet) 图形相关 在Oracle9i中计算时间差 六度分隔 经典故事 新年竟然第一篇?!
java学习
hone · 2007-04-05 · via 博客园 - hone

Java does not have explicit pointers; in a sense, all variables that refer to objects are pointers. When you assign between two objects of the same type, you actually assign a reference to the object on the right-hand side. To create a new instance of an object, you need to call one of its constructors:

myObject a, b;

= b;                 // reference

= new myObject(b);   // create a new object

In Java, the + operator can concatenate strings together. A sequence such as the following

String greeting = "Hello";
greeting 
= greeting + " there";

 is legal. Because the original String that greeting points to cannot be modified, the concatenation actually involves the creation of a new String, which greeting is then set to point to. Therefore, the reference to the original "Hello" string is removed, which eventually causes it to be destroyed.
The concatenation statement also involves some more behind-the-scenes magic by the compiler. It creates a temporary StringBuffer, then calls the StringBuffer.append() method for each expression separated by a + sign, then calls StringBuffer.toString() to convert it back to the result String. As with the automatic creation of String objects from constant strings, this is a special case on the part of Java, but is there because string concatenation is so useful.

Arrays can also hold objects, so you can declare the following:

MyObject[] objarray;


This would then be created as follows (this could be combined with the declaration):

objarray = new MyObject[5];

It is important to note that this creates only the array. You still need to create the five objects:

for (k = 0; k < 5; k++{

    objarray[k] 
= new MyObject();

}

To create subarrays, create an array where each element is an array. The first array can be declared and created in one step

int[][] bigArray = new int[6][];

and then each subarray needs to be created (each one can be a different length, in fact):

for (m = 0; m < 6; m++{

    bigArray[m] 
= new int[20];

}

You can initialize arrays when they are declared:

short[][] shortArray = 123 },  4 },  5 , 6 } };


After that, shortArray[0] would be an array of three elements,
shortArray[1] would be an array of one element,
and shortArray[2] would be an array of two elements.