您的位置:首页 > 编程语言 > Java开发

1、java容易忘记的基础语法知识

2017-08-10 16:21 513 查看
1、final声明常量,常量全是大写,一旦定义不能改变
final String LOVE = 'haa'

2、比较运算符在java中:
> 、 < 、 >= 、 <= 只支持左右两边操作数是数值类型
== 、 != 两边的操作数既可以是数值类型,也可以是引用类型

3、获取用户输入的值
3.1 import java.util.Scanner
3.2 创建Scanner对象
Scanner input = new Scanner(System.in);
int num = input.nextInt();#字符串:input.next()   浮点:input.nextDouble() ...

4、数组
4.1 定义数组
int[] scores = { 78, 93, 97, 84, 63 };
等价于
int[] scores = new int[]{ 78, 93, 97, 84, 63 };
int scores[] = { 78, 93, 97, 84, 63 };
int scores[] = new int[3];
4.2 遍历数组foreach:
for(int score : scores){
System.out.println(score);
}
4.3 二维数组
4.3.1 :定义二维数组
int[][] tmp = new int[2][3]
int tmp[][] = new int[2][3]
int[][] tmp = {{1,2},{3,4}...}
4.3.2 赋值
x[0][1] = 12
4.3.3 遍历二维数组:
int[][] scores ={{3,4},{1,2}};
for(int[] score : scores){
for(int x : score){
System.out.println(x);
}
}
4.4 数组常用属性
4.4.1 length: int[] scores = { 78, 93, 97, 84, 63 }; scores.length
4.5 数组常用方法
import java.util.Arrays
4.5.1 : Arrays.sort(arr)
4.5.2 : Arrays.toString(arr):
int[] scores = new int[]{ 78, 93, 97, 84, 63 };
System.out.print(Arrays.toString(scores)); //[78, 93, 97, 84, 63]

5、方法
5.1 方法声明:
无返回值:public void test(int a,String[] b){} ,如果声明了void,方法体中就不能有return
有返回值:public int test(int a,String[] b){ return 2} ,如果生命了int,返回值就一定要是int

5.2 方法重载:同一个类中,方法名相同,参数个数不同或位置不同或类型不同。根据参数个数决定调用哪个方法
public void test(){}
public void test(int a){}
public void test (String a){}
public void test(int a ,int b){}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: