2.1 用引用操纵对象【String】
- 遥控器(引用)- 电视机(数据)
- 创建String引用,如果向无对象的引用发送信息会返回一个运行时错误,所以安全的做法是:创建一个引用的同时便进行初始化
- String s = “asdf”(Java特性:字符串可以用带引号的文本初始化)
- New关键字:“给我一个新对象”
- String s = new String(“asdf”);
- 存储到什么地方:(1)寄存器:位于处理器内部,最快,数量有限;(2)堆栈:位于RAM,但通过堆栈指针可以从处理器那里获得直接支持。堆栈指针若向下移动,则分配新的内存;若向上移动,则释放那些内存。仅次于寄存器的快速有效的分配存储方式。创建程序时,Java系统必须知道存储在堆栈内所有项的确切生命周期。(3)堆:一种通用的内存池(位于RAM),用于存放所有的Java对象。不同于堆栈的好处是,编译器不需要知道存储的数据在堆里存活时间。(4)常量存储:代码内部,安全不被改变;(5)非RAM存储
- 特例:基本类型:不用new来创建变量,而是创建一个并非是引用的“自动”变量。这个变量直接存储“值”,并置于堆栈中,高效。
- Java中的数组:确保初始化,而且不能在它的范围之外被访问。范围检查是以每个数组上少量的内存开销和运行时的下标检查为代价。当创建一个数组对象时,实际上就是创建了一个引用数组,并且每个引用都会自动被初始化null。一旦Java看到null,就知道这个引用还没有指向某个对象,在使用前必须为其指定一个对象,否则运行时报错。
2.3 永远不需要销毁对象
- 作用域(scope):决定了在其内定义的变量名的可见性和生命周期。花括号决定。
- 对象的作用域:由new创建的对象,只要你需要,就会一直保留下去。→垃圾回收器
- 基本成员默认值
- 方法:名称,参数,返回值,和方法体
- 名字可见性:包名小写;存活在自己的名字空间内。
- 运用其他构件【import】
- static 关键字:当声明一个事物是static时,就意味着这个域或方法不会与包容它的那个类的任何对象实例关联在一起。即使从未创建某个类的任何对象,也可以调用其static方法或者访问其static域。
2.11 练习
E01:
package
hh_test;
public class E01{
int i;
char c;
public E01(){
System.out.println("i = " + i);
System.out.println("c =[" + c + ']');
}
public static void main(String[] args){
new E01();
}
}/*output
public class E01{
int i;
char c;
public E01(){
System.out.println("i = " + i);
System.out.println("c =[" + c + ']');
}
public static void main(String[] args){
new E01();
}
}/*output
i = 0
c =[]
*///:~
c =[]
*///:~
Key :初始值:int 是0, char 是空格
E02:
package
hh_test;
public class E02{
public static void main(String[] args){
System.out.println("Hello,World!");
}
}/*output
public class E02{
public static void main(String[] args){
System.out.println("Hello,World!");
}
}/*output
Hello,World!
*///:~
*///:~
E03:
package
hh_test;
public class E03{
public static void main(String[] args){
E03 a = new E03();
}
}///:~
public class E03{
public static void main(String[] args){
E03 a = new E03();
}
}///:~
E04:
package hh_test;
public class E04{
int i;
double b;
boolean d;
public static void main(String[] args){
E04 a = new E04();
a.i = 1;
a.b = 1.1;
a.d = true;
}
}///:~
E05:
package
hh_test;
public class E05{
int i;
double b;
boolean d;
public static void main(String[] args){
E05 a = new E05();
a.i = 1;
a.b = 1.1;
a.d = true;
System.out.println("a.i = " + a.i);
System.out.println("a.b = " + a.b);
System.out.println("a.d = " + a.d);
}
}/*output
public class E05{
int i;
double b;
boolean d;
public static void main(String[] args){
E05 a = new E05();
a.i = 1;
a.b = 1.1;
a.d = true;
System.out.println("a.i = " + a.i);
System.out.println("a.b = " + a.b);
System.out.println("a.d = " + a.d);
}
}/*output
a.i = 1
a.b = 1.1
a.d = true
a.b = 1.1
a.d = true
*///:~
E06:
package
hh_test;
public class E06{
String s = "Hello, World!";
int storage(String s){
return s.length() * 2;
}
void print(){
System.out.println("storage(s) = " + storage(s));
}
public static void main(String[] args){
E06 a = new E06();
a.print();
}
}/*output
public class E06{
String s = "Hello, World!";
int storage(String s){
return s.length() * 2;
}
void print(){
System.out.println("storage(s) = " + storage(s));
}
public static void main(String[] args){
E06 a = new E06();
a.print();
}
}/*output
storage(s) = 26
*///:~
*///:~
E07:
package hh_test;
class StaticTest{
static int i = 1 ;
}
public class E07{
static void increment(){ StaticTest.i++; }
public static void main(String[] args){
E07 a = new E07();
a.increment();
E07.increment();
increment();
}
}///:~
E08:
package
hh_test;
public class E08{
static int i = 1;
public static void main(String[] args){
E08 a1 = new E08();
E08 a2 = new E08();
System.out.println(a1.i + " == " + a2.i);
a1.i++;
System.out.println(a1.i + " == " + a2.i);
}
}/*output
public class E08{
static int i = 1;
public static void main(String[] args){
E08 a1 = new E08();
E08 a2 = new E08();
System.out.println(a1.i + " == " + a2.i);
a1.i++;
System.out.println(a1.i + " == " + a2.i);
}
}/*output
1 == 1
2 == 2
*///:~
2 == 2
*///:~
E09:
package hh_test;
public class E09{
public static void main(String[] args){
Byte by = 1;
byte bt =by;
System.out.println("byte = " + bt);
Short sh = 1;
short s = sh;
System.out.println("short = " + s);
Integer in = 1;
int i = in;
System.out.println("int = " + i);
Boolean bo = false;
boolean b = bo;
System.out.println("boolean = " + b);
Character ch = 'h';
char c = ch;
System.out.println("char = " + c);
Float fl = 1.1f;
float f = fl;
System.out.println("float= " + f);
Double db = 1.1d;
double d = db;
System.out.println("double= " + d);
}
}/*output
byte = 1
short = 1
int = 1
boolean = false
char = h
float= 1.1
double= 1.1
*///:~
short = 1
int = 1
boolean = false
char = h
float= 1.1
double= 1.1
*///:~
E09:
//{Args: A B C}
package hh_test;
public class E10{
public static void main(String[] args){
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
}//这道题运行有点问题
package hh_test;
public class E10{
public static void main(String[] args){
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
}//这道题运行有点问题
后面的题跟API文档有关暂且略过。