当前位置: 移动技术网 > IT编程>开发语言>Java > 1.基础语法

1.基础语法

2019年09月07日  | 移动技术网IT编程  | 我要评论

1.1.helloworld

新建helloworld.java文件

// 定义一个类helloworld,类名必须与文件名相同
public class helloworld {
    public static void main(string[] args) {
        system.out.println("hello, world!");
    }
}

编译和运行

javac helloworld.java     //编译

java helloworld           //运行

1.2.关键字和标识符

(1)所有关键字

 

 (2)标识符

是指在程序中,我们自己定义的内容,比如类名,方法名和变量名等等

命名规则

  • 可以包含英文字母、数字、下划线和$符合
  • 不能以数字开头
  • 不能是关键字

命名规范

  • 类名:首字母大写,后面每个单词首字母大写
  • 变量名和方法名:首字母小写,后面每个单词首字母大写

1.3.变量

基本数据类型

  • 整数:byte short int long
  • 浮点数:float double
  • 字符型:char
  • 布尔类型:boolean

变量的定义

public class variable {
    public static void main(string[] args){
        int i = 12;
        system.out.println(i);
        
        float f = 0.5f;
        system.out.println(f);
        
        char c = 'b';
        system.out.println(c);
    }
}

1.4.数据类型转换

自动转换

  • 将取值范围小的类型自动提升为取值范围大的类型
  • byte short char运算时直接提升为int
public class datatype {
    public static void main(string[] args){
        int i = 5;
        byte b = 2;
        // byte类型比int小,做运算的时候会自动把byte类型转换成int类型
        int j = i + b;
        system.out.println(j);
    }
}

强制转换

  • 将取值范围大的类型,强制转换成取值范围小的类型
  • 可能造成数据损失精度和数据丢失
public class datatype {
    public static void main(string[] args){
        double i = 2.33;
        system.out.println(i);    //2.33
        //把double类型强制转换成int类型
        int j = (int) 2.33;
        system.out.println(j);    //2
    }
}

1.5.流程控制语句

判断语句

public class demoifelse {
    public static void main(string[] args){
        int score = 74;
    
        if(score<0 || score>100){
            system.out.println("数据错误");
        }else if(score>=80 && score<=100){
            system.out.println("优秀");
        }else if(score>=60 && score<80){
            system.out.println("及格");
        }else{
            system.out.println("不及格");
        }
    }
}

选择语句

public class demoswitch {
    public static void main(string[] args){
        int i = 2;
        
        switch(i){
            case 1:
                system.out.println("星期一");
                break;
            case 2:
                system.out.println("星期二");
                break;
            case 3:
                system.out.println("星期三");
                break;
            default:
                system.out.println("数据错误");
                break;
        }
    }
}

1.6.循环语句

public class demofor{
    public static void main(string[] args){
        //for循环
        for(int i=1;i<=10;i++){
            system.out.println(i);
        }
        //while循环
        int j = 1;
        while(j<=10){
            system.out.println(j);
            j++;
        }
        
        //do..while循环
        int a = 1;
        do{
            system.out.println(a);
            a++;
        }while(a<=10);
    }
}

1.7.方法

package derek.day04.demo02;

public class demomethod {
    public static void main(string[] args) {
        system.out.println(sum(2, 3));
    }

    public static int sum(int a, int b) {
        int result = a + b;
        return result;
    }
}

1.8.数组

package derek.day04.demo02;

public class demoarray {
    public static void main(string[] args) {
        int[] arra = new int[] {1,2,3,4,5};
//        int[] arra = {1,2,3,4,5}
        int num = arra[1];
        system.out.println(num);   //2

        int[] arrb = new int[3];
        arrb[0] = 11;   //添加元素
        arrb[1] = 22;
        arrb[2] = 33;
        //数组长度
        system.out.println(arrb.length);   //3
        //数组的遍历
        for (int i = 0; i < arrb.length; i++) {
            system.out.println(arrb[i]);
        }
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网