数据类型
java是一门强类型语言,不同与灵活的go
- primitive type
(1) ex. int long float
(2) Boolean - reference type
(1) class
(2) interface
(3) array
随记1:
int i = 10;//10进制,10
int i1 = 010;//8进制,8
int i2 = 0x10;//16进制,16
int i3 = 0b10;//2进制,2
随记2:
引出 两者的储存问题
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);//false
随记3.
格式转换问题:
大到小->(强制,损失精度)
小到大-> (自动)
随记4.
变量是最基本的储存单元,其中包括 变量名, 变量类型,作用域()->{
实例变量(全局变量,局部变量)
类变量(ex.static)->加载static的时间和类一样的,比类实例化的方法存在要早
}
随记5.
Scanner s = new Scanner(System.in);//1.创建一个对象
if(s.hasNext()){
String str = s.next();//2.接受且变为字符串
}
s.close();//3.关闭
//next() 和 nextLine() 区别
next()
1)读到有效字符后才能结束输入
2)有效输出之前的空白,可以将其自动去掉
3) 不能接收 空的字符串
nextLine()
- 以enter为结尾的,输出 结尾之前所有的内容
- 可以获得 空
随记6.
// 方法的格式ex.
//修饰符 返回值类型 方法名 ( 参数类型 参数名称)
public class demo {
public static void main(String[] args) {
}
public static int add(){}
拓展:如何实现“java值传递” 和 Java里有引用传递吗?
随记7.
探讨方法的重载
随记8.
–>递归练习
随记9.
int[] nums = new int[10];
数组的 1.可以有几个2.储存分配3.优缺点(待补充)
随记10.
数组的四个基本特点
- 其长度是确定的,数组一旦被创建,其长度就不可变
- 其元素必须是相同的类型,不可出现混合的类型
- 数组中的元素可以是任何数据类型,包括基本类型和引用类型
- 数组变量属于引用类型,数组也可以看作是对象,数组中的每个元素相当于该对象的成员变量。数组本身就是对象,Java中的对象在堆中,因此数组无论保存原始类型还是其他对象类型,数组本身是在堆中的
//静态初始化
int[] a = {1,2,3,4,5};
//动态初始化
int[] b = new int[10];
有什么区别呢?
随记11.
非静态方法实例化()
Sbb sbb = new Sbb();
sbb.name = "bbs";
sbb.say();
随记12.
随记13.
继承,重写
随记14.
public class demo01 {
//2
{
System.out.println("匿名代码块");
}
//1(只执行一次)
static {
System.out.println("静态代码块");
}
//3
public demo01(){
System.out.println("构造方法");
}
public static void main(String[] args) {
demo01 demo = new demo01();
}
}