Java For Android - 语言基础


对象将它的状态存储在变量中。

变量 是一个由标志符命名的数据项。

变量有三个属性:

1. 名称: 程序通过变量的名称引用变量的值。

2. 数据类型:  每个变量都必须有一个数据类型。变量的数据类型决定此变量可以包含的值 以及可以在它上面执行的操作 。

3. 作用范围: 可以在其中使用此变量的简单名称来引用它的程序区域。

Java支持的原始数据类型 及默认值:

Data Type Default Value (for fields,not for local variables)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object)   null
boolean false

与原始类型相比,引用类型变量的值是对由此变量代表的一个或一组值的引用 (也就是地址),也就是C语言中的指针(内存地址 )。

在final变量被初始化后,它的值不能被修改。

声明变量时要显示地设置变量的名称和类型。

原始类型的变量包含一个值。

引用类型的变量包含对一个或一组值的引用。

操作符 在一个、两个或三个操作数上执行一种操作,并返回一个值。

Java中含有的操作符,如下表: 

Operator Precedence
Operators Precedence
postfix expr ++ expr --
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

表达式 是一系列变量、操作符和方法调用,用于求出单个值。

语句 组成了一个完整的执行单元。

The   if   statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code   only if   a particular test evaluates to   true .  


The   switch   statement can have a number of possible execution paths. A   switch  works with the   byte ,   short ,   char , and   int   primitive data types. It also works with   enumerated types , the String   class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

The   while   statement continually executes a block of statements while a particular condition is   true . Its syntax can be expressed as:

  1. while (expression)   
  2. {   
  3.      statement(s)   
  4. }  

The   for   statement provides a compact way to iterate over a range of values.  The general form of the for   statement can be expressed as follows:

  1. for (initialization; termination; increment)   
  2. {   
  3.     statement(s)   
  4. }   

An unlabeled   break   statement terminates the innermost   switch ,   for ,   while , or   do-while   statement, but a labeled   break  terminates an outer statement.

  1. class BreakWithLabelDemo   
  2. {   
  3.     public static void main(String[] args)   
  4.     {   
  5.         int[][] arrayOfInts = { { 32873589 }, { 12107620008 },   
  6.                 { 62212777955 } };   
  7.         int searchfor = 12;   
  8.         int i;   
  9.         int j = 0;   
  10.         boolean foundIt = false;   
  11.         search: for (i = 0; i < arrayOfInts.length; i++)   
  12.         {   
  13.             for (j = 0; j < arrayOfInts[i].length; j++)   
  14.             {   
  15.                 if (arrayOfInts[i][j] == searchfor)   
  16.                 {   
  17.                     foundIt = true;   
  18.                     break search;   
  19.                 }   
  20.             }   
  21.         }   
  22.         if (foundIt)   
  23.         {   
  24.             System.out.println("Found " + searchfor + " at " + i + ", " + j);   
  25.         }   
  26.         else  
  27.         {   
  28.             System.out.println(searchfor + " not in the array");   
  29.         }   
  30.     }   
  31. }  
The continue   statement skips the current iteration of a forwhile  , or do-while  loop.

The   return   statement exits from the current method, and control flow returns to where the method was invoked.

相关内容