Java基础1

1、所有类型的对象都可以采用toString()方法而转化为String类型

String val = o.toString() // o is Object
char grade = val.charAt(0);


2、break and continue语句

  当在while、 do/while、或switch结构中执行break语句时,程序将立即从该结构中退出,继续执行结构之后的语句。

  break语句的常规用法是从一个循环中提前退出,或者跳出一个switch结构的剩下部分。终止for结构。

  continue:当在while、do/while、for结构中执行continue语句时,会跳过该结构的其余部分,继续执行下一次循环。

3带标记的break and contiue

     break 语句只能从封闭的while、do/while、for或switch结构中立即退出。为退出嵌套的结构集合,可以使用带标记的break语句。当在while、do/while、for或switch结构中执行该语句时,可从这些结构的任意多层封闭结构中立即退出。看下面例子:

public void paint(Graphics g)
    {
        int xPos, yPos = 0;
        stop:{//label compound statement
            for( int row = 1; row <= 10; row++)
            {
                xPos = 25;
                yPos += 15;
                
                for(int column = 1; column <= 5; column++)
                {
                    if(row == 5 )
                        break stop; // jump to stop label
                    g.drawString("*", xPos, yPos);
                    xPos += 7;
                }
            }
                    yPos += 15;
                    g.drawString("Loops terminated normally", 25, yPos);
        }// end of label
        yPos += 15;
        g.drawString("End of the paint method", 25, yPos);
        
    }

 

 

 

posted @ 2013-03-28 11:03  wiessharling  阅读(134)  评论(0编辑  收藏  举报