Java中的基本类型:
在程序设计中经常用到一系列基本类型,它们需要特殊对待。尅把它们想象成“基本”类型。之所以特殊对待,是因为new将对象存储在“堆”里,故用new创建一个对象——特别是小的,简单的变量,往往不是很有效。i对于这些类型,Java采取与C和C++相同的办法。也就是说,不用new来创建变量,而是创建一个并非是引用的“自动”变量。这个变量直接存储“值”,并置于堆栈中,因此更加高效。
Java中的数组:
当创建一个数组对象时,实际上就是创建了一个引用数组,并且每个引用都会自动被初始化为一个特定值,该值拥有自己的关键字null,一旦java看到null,就知道这个引用还没有指向某个对象。在使用任何引用前,必须为其指定一个对象,如果试图使用一个还是null的引用,在运行时将会报错。因此,常犯的数组错误在Java中就可以避免。
编码风格:Java更倾向于“驼峰风格”。
“=”:赋值的是
引用
Java中的迭代
while
public class WhileTest {
static boolean condition(){
boolean result = Math.random() < 0.99;
System. out .println(result + "." );
return result;
}
public static void main(String[] args){
while (condition())
System. out .println("Inside 'while.'" );
System. out .println("exited 'while'." );
}
}
do-while
do
statement
while(boolean-expression);
for(initialization; Boolean-expression; step)
public class ListCharacters {
public static void main(String[] args){
for (char c = 0; c < 128; c++)
if (Character.isLowerCase(c))
System. out .println(" character: " + c);
}
}
foreach用法
public class ForEachString {
public static void main(String[] args){
for (char c : "An American Swallow".toCharArray())
if (c != ' ' )
System. out .println(c + " " );
}
}
break 和 continue
public class ContinueAndBreak {
public static void main(String[] args){
for(int i = 0; i < 100; i++)
{
if(74 == i) break;
if(i % 9 != 0) continue;
System.out.print(" "+i);
}
System.out.println();
int[] arrage = new int[100];
for(int i = 0; i < 100; i++)
arrage[i] = i;
for(int i : arrage)
{
if(74 == i) break;
if(i % 9 != 0) continue;
System.out.print(" "+i);
}
}
}
输出: 0 9 18 27 36 45 54 63 72
0 9 18 27 36 45 54 63 72