java foreach 语法是在jdk1.5时加入的新特性,主要是当作for语法的一个增强,那么它的底层到底是怎么实现的呢?因为面试时被问到,所以在这边做一个记录。
首先来看看foreach能够使用的类型是什么:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAawAAABkCAIAAABVZpOWAAAREUlEQVR4nO1dz48bRRae/8o+QIQ4zQVOwJJISDvjUcgoWbEhnQvsLsqy60hLFMNKIDFwtyVIOIC0UiCekIC1ewElsdECTgbsnQVbhJUjkIlE1Huo7ur3ql51tz3ttNv9ffoO456q6qrqqq9f/Xq95gMAAJQYa3lnAAAAIE9ABAEAKDUOJIJvO/DZZ59llT8AAICFYiEi+M4770AHAQAoBDIQQfvizz///O6770IHAQBYfmQjgn/evav48d49ahJmlMk02PXW1tbWvN2s0tvbWV9bW1vf2csqwZWEq5bU9Qwfx0KQdZsBiomMRVDpoO8wEkOoxkdhd6Ndz9k+efQgKmvQezvrJE3VI2lau16iwMndW84Vv11akFgxhU2PTBKZMf3sRdC+S5BWcpNxw/GAIIKA7ysR/OCDDy5IeP/99+MjGyJ46JFH1R/+zCK4ZrdGueGm0U9T5DIUQTlXKZITQGPNJ6MGMklktvQXYAlad8lABOd7QEBJsOb7/v7+viiC+/v78ZFFSzC1CEaNMlS29Z092q1soyAISK7t7XimJch00ttNEEH2T/JD54NmTscliVm3Y9fsQqpQVizJzuJRdO5IUCOEaAwaYYwsh6HN8kZPIKbszloyRVC6o0/0LShUvF0sGm5zVLWYIJVa5+BibX3nthQSKDSC4fClS5cMBbx06VJi5INZgsLb3ttltoVpFMS90BNF0EIaERQimLmS5Sw2C2LPNAtrRQmrZl1VVdu4kZiIEMa2panor5MOTv52WayuWmIi6Lij26pPPXqds6qlBI2kvF2jvUV/SyGBIiMQwbt37168eFEr4MWLF+/evZsYOTsRlC75cXN7tKlHDZaZOkY8C6lEkNlOrj7KbmeZF+s7e6J8m9ckcWVRgixJN3LUmFM3eBqRBc5fAEJFGOm7aoled92RXeeB4qZCLCN8jqp21o4R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9XBmHtWEJFo8QwVVgOAQ2ZvqnA2KCSa4Npq1GhgsjS+0A1S2C1BK0HPlRd14cgVNut2MIsCiECK4IzeGwNAWmSGcGM10dXmoHqDPNCcoiaGsoRHAlCBFcEVIRrHf9wK8qXwJWIe1PBlfDfYIZDIeX2AFqrT2NXx0WvJky5bX9pNKbDlsOH/rgkhMiuCIM5SBahYjMGb43kIwuI0XIck5wWR2g8jD27IHkzdS94GMlGH05AAsjxSJEcBXo8j2l1y4DB87WwqWxnDJ/BlbUAeqylQtcBCGCYAZcVQeouZcLfACECIIH4qo6QF2ecoGLJkQQBMFSEyIIgmCpuWbunwUAACgTIIIAAJQaEEEAAEoNiCAAAKUGRBAAgFIDIggAQKkBEQQAoNSACAIAUGpABAEAKDUgggAAlBoQQQAASg2IIAAApQZEEACAUgMiCABAqQERBACg1IAIAgBQakAEAQAoNSCCAACUGhBBAABKDYggAAClBkQQAIBSAyIIAECpAREEAKDUgAgCAFBqQAQBACg1IIIAAJQa/welqTtOBEDJzwAAAABJRU5ErkJggg==" alt="" />
编译器报错原因写的很清楚了,要求:数组或java.lang.Iterable。
看下jdk源码对Iterable接口的声明:
/** Implementing this interface allows an object to be the target of
* the "foreach" statement.
* @since 1.5
*/
public interface Iterable<T> { /**
* Returns an iterator over a set of elements of type T.
*
* @return an Iterator.
*/
Iterator<T> iterator();
}
实现此接口,允许成为foreach语法的target。
那数组呢?数组没有实现为什么也可以用呢?
那是因为遍历数组时,会转换为对数组中的每一个元素的循环引用,相当于for语法循环遍历一样。
那么为什么是数组或者实现了这个接口,就能实现遍历呢?其实是因为编译器的原因,在编译中的语义分析过程中,有一个解除语法糖的操作,(语法糖是啥?可以理解成编译器为方便开发人员开发,会对特定代码做一些特殊处理,方便开发人员使用,除了foreach,java中还有泛型、装箱、拆箱、变长字符串等)。
- 对于list编译器会调用Iterable接口的 iterator方法来循环遍历数组的元素,iterator方法中是调用Iterator接口的的 next()和hasNext()方法来做循环遍历。java中有一个叫做迭代器模式的设计模式,这个其实就是对迭代器模式的一个实现。
- 对于数组,就是转化为对数组中的每一个元素的循环引用