java基础语法

来源:互联网 发布:苹果离线翻译软件 编辑:程序博客网 时间:2024/06/07 10:30

java基础语法知识

简单地java小程序      helloworld

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("HelloWorld!");
    }
}


1.标示符-关键字(简单地东西就不在这里展示了 ,浪费大家的时间)


2.数据类型转换

 *boolean类型不可以转换为其他的数据类型

 *其他类型见相互转换遵循以下规则

     *容量小的类型自动转换为容量大的类型

        *byte,short,char>int>long>float>double

        *byte,short,char之间不会相互转换,三者计算时首先回转换为int类型

     *容量大的类型转换容量小的类型,需要加上强制转换符,但可能造成精度降低或溢出,用时需注意

     *多种类型混合运算时,系统首先自动的将所有数据转换成容量最大的哪一种数据类型,然后在运算

     *实数类型默认为double

     *整数类型默认为int


3.运算符

     *算数运算符:+,-,*,/,%,++,--

     *关系运算符:>,<,>=,<=,==,!=

     *逻辑运算符:!,&,|,^,&&,||

     *赋值运算符:=

     *扩展赋值运算符:+=,-=,*=,/=

     *字符串连接运算符:+


4.语句

    *if条件分支语句

        *if语句

        *if-else语句

        *if-else   if-else语句

//if语句
public class Textif {
    public static void main(String[] args) {
        
        int i = 20;
        if (i<20) {
            System.out.println("<20");
        }
        else if (i<40) {
            System.out.println("<40");
        }
        else if (i<60) {
            System.out.println("<60");
        }
        else
        System.out.println(">=60");
        }
}

   *switch开关语句

//stwitch语句
public class Textswitch {
    public static void main (String args[]) {
        int i = 8;
        switch (i) {
            case 8 :            
              System.out.println("a");
              break;
            case 3 :
              System.out.println("b");
              break;
            case 2 :
              System.out.println("c");
              break;
            default :                    
              System.out.println("error");
        }
    }
}

       

   *循环语句

         *for循环

//for循环语句    计算10以内阶乘相加
public class Textfor {
    public static void main(String args[]) {
        long result = 0;
        long f = 1 ;
        for (int i = 1; i <= 10; i++) {
            f = f * i;
            result += f;
        }
        System.out.println("result=" + result);
    }
}

          *while循环

          *do-while循环


//while语句
public class Textwhile {
    public static void main (String args[]) {
        int i = 0;
        while(i<10) {
            System.out.print(i);
            i++;
        }
    
        i=0;
        do {
            i++;
            System.out.print(i);
        }
        while (i < 10);
    }
}
    
       

   *break和continue语句

//break语句
public class Textbreak {
    public static void main (String args[]) {
        int stop  = 4;
        for (int i = 1; i<=10; i++) {
            if (i == stop) break;
            System.out.println("i=" +i);
        }
    }
}



//continue语句
public class Textcontinue {
    public static void main (String args[]) {
        int skip = 4;
        for (int i = 1; i <= 5; i++) {
            if (i == skip) continue;
            System.out.println("i=" +i);
        }
    }
}





0 0