1、八种最基本的数据类型byte,short,int,long,float,double,char,boolean。String是一个对象而不是基本数据类型
2、length,length(),size()区别
- 数组用length
- 字符串String用length()
- 泛型集合用size()
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
String[] strings = {"h","e","l","l","o"};
String string = "hi";
ArrayList list = new ArrayList<>();
list.add("how");
list.add("are");
list.add("you");
System.out.println(strings.length);
System.out.println(string.length());
System.out.println(list.size());
}
}
3、数组
声明数组时不能指定其长度(数组中元素的个数)
type var[]; 或type[] var;
使用关键字new创建数组对象
String[] string = new String[5];
初始化
- 动态初始化:数组定义、为数组分配空间和赋值分开进行
- 静态初始化:在定义数组的同时就为数组元素分配空间并赋值
- 默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素根据成员变量的类型被初始化(在上一篇文章中已经介绍了八种基本类型的默认初始值)
数组初始化之后不能改变数组长度
//动态初始化
int a[];
a = new int[3] ;
a[0] = 0;
a[1] = 1;
a[2] = 2;
String b[] ;
b = new String[3] ;
b[0] = new String("g");
b[1] = new String("o");
b[2] = new String("d");
//静态初始化
String[] c = {"h","e","l","l","o"};
int[] arr = new int[]{1,2,3};
String d[] = {new String("I"),new String("love"),new String("u")};
//默认初始化
int e[] = new int[6];
一些容易犯的错误和注意点
- 在数组声明中包含数组长度永远是不合法的!如:int[5] arr; 。因为,声明的时候并没有实例化任何对象,只有在实例化数组对象时,JVM才分配空间,这时才与长度有关。
- 在数组构造的时候必须指定长度,因为JVM要知道需要在堆上分配多少空间。如:int[] arr = new int[];是不合法的。
- 对象类型数组中的引用被默认初始化为null。如:Date[] day= new Date[10]; 相当于从day[0]到day[9]都这样被自动初始化为day[i] = null;
4、四类八种基本类型
- 逻辑型:boolean
- 文本型:char
- 整数型:byte、short、int、long
- 浮点型:float、double、
5、运算符
- 算术运算符:+、-、*、/、%、++、–
- 关系运算符:==、!=、>、>=、<、<=
- 逻辑运算符:&&、||、!
- 位运算符:&、|、~、^、>>、<<、>>>
- 赋值运算符:=、+=、-=、*=、/=、%=、<<=、>>=、&=、^=、|=
- 条件运算符: ( ? : ): 也叫三元运算符
- instanceof 运算符:该运算符用于操作对象实例,检查该对象是否是一个特定类型(类类型或接口类型)
( Object reference variable ) instanceof (class/interface type)
String name = "Jin";
boolean result = name instanceof String; // 由于 name 是 String 类型,所以返回真
注:如果被比较的对象兼容于右侧类型,该运算符仍然返回true。
6、关键字
abstract | assert | boolean | break | byte | case |
---|---|---|---|---|---|
catch | char | class | const | continue | default |
do | double | else | enum | extends | final |
float | for | goto | if | implements | import |
instanceof | int | interface | long | native | new |
package | private | protected | public | return | strictfp |
short | static | super | switch | synchronized | this |
throw | throws | transient | try | void | volatile |
while |
7、在java中,负责对字节代码解释执行的是虚拟机
java是由java源代码编译成字节码存放在 .class文件中的. 而执行时,是由java虚拟机(jvm)读取字节码文件翻译并执行的. 所以java是解释性的