Java——基本语法

switch (表达式)
表达式的取值:byte , short , int , char
JDK5 以后可以是枚举
JDK7 以后可以是String
default  的位置可以出现在switch语句任意位置


C/C++ bool ;  java  boolean ;

方法定义及格式:
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2…) {
                        函数体;
                        return 返回值;
}

键盘录入数据:
导包(位置放到class定义的上面)
   import java.util.Scanner;
创建对象
   Scanner sc = new Scanner(System.in);
接收数据
   int x = sc.nextInt();

java语言中,类里面要在 static static main 入口方法里面调用其他方法,其他方法必须要是 public static

数组概念:
数组的定义格式:
格式1:数据类型[] 数组名;
格式2:数据类型 数组名[];
数组的初始化:
格式1:数据类型[] 数组名 = new 数据类型[]{元素1,元素2,…};
int[] arr = new int[]{1,2,3}; 等价于 int[] arr = {1,2,3};     //数据还是在堆上
格式2:数据类型[] 数组名 = new 数据类型[数组长度];
int[] arr = new int[3];     //数组变量在栈上,空间在堆上
数组索引越界:ArrayIndexOutOfBoundsException  ,访问到了数组中的不存在的索引时发生。
空指针异常,   NullPointerException                       ,数组引用没有指向实体,却在操作实体中的元素时。
int[] e={1,2,3,4};  System.out.println(e[5]);   //ArrayIndexOutOfBoundsException
e=null;   System.out.println(e[1]);               //NullPointerException



File.java
class Example
{
        public static void main() { };
        public static Func1()  { };
        public static Func2()  { };
}
java文件编译后生成的文件名是.java文件里的类名;
编译后是Example.class
函数进栈顺序是main()->Func1()->Func2();
所以main函数中能调用到后面定义的函数;












猜你喜欢

转载自www.cnblogs.com/meihao1203/p/9181538.html