第2章 一切都是对象
Java基于C++,但Java更面向对象,在Java中一切都是对象。
2.1 用引用操作对象
public class hello
{
public static void main(String args[])
{
String s;
System.out.println(s);
}
}
报错:
hello.java:6: 错误: 可能尚未初始化变量s
System.out.println(s);
2.2 必须由你创建所有对象
public class hello
{
public static void main(String args[])
{
String s=new String("12");
System.out.println(s);
}
}
五个存放数据的地方:
1.寄存器:最快,位于CPU内部,但数量有限,对程序员透明
2.堆栈,位于RAM(随机访问存储器random access memory):通过堆栈指针从CPU 获得直接支持。堆栈指针下移,分配内存;堆栈执政上移,释放内存。 快速有效,仅次于寄存器。
3.堆:位于RAM,用于存放所有的 Java对象,相比堆栈 ,堆的分配存储具有很大 的灵活性。
4.常量存储:一般直接放入程序代码中
5.非RAM存储:例如 流
对象存储在 堆 里,基本类型(int ,double,String)存储在堆栈中。
C++的数组就是内存块。
2.3 永远不需要销毁对象
一、作用域决定其内的变量的可见性和生命周期
public class hello
{
public static void main(String args[])
{
int x=11;
{
int y=22;
System.out.println(x); //输出11
System.out.println(y); //输出22
}
System.out.println(x); //输出11
//System.out.println(y); //这一段不注释的话报错。
}
}
二、对象的作用域。
public class hello
{
public static void main(String args[])
{
{ String s=new String("123");}
System.out.println(s);
}
}
报错:
hello.java:6: 错误: 找不到符号
System.out.println(s);
^
符号: 变量 s
位置: 类 hello
1 个错误
原因:引用s在作用域的终点消失。但是,s所指向的String对象仍让继续占据内存空间,我们无法在这个作用域之后访问这个对象。这时候,Java的垃圾回收器辨别不会使用的对象,让后销毁释放内存。
三、类
class TypeName{}
TypeName a=new TypeName();
四、数据类型和方法
Java的工作是定义类,产生类的对象,发送消息个这些对象。
public class hello
{
public static void main(String args[])
{
class test{
int i;
double j;
}
test t=new test();
t.i=1;
t.j=2;
System.out.println(t.i+" "+t.j);
}
}
输出:1 2.0
注意,这里输出的是2.0,原因是基类中j是double型。这里的例子test中只有数据成员,不包括方法。
五、方法,返回值,参数列表
有一个方法f(),不带任何参数,返回值类型为int,有一个对象名为a的对象,调用方法:int i=a.f();返回值a.f()的类型必须与i的类型int一致。
方法:
返回值类型 方法名(参数列表)
{方法体};
例子:
public class hello
{
public static void main(String args[])
{
class test
{
int sz(String s)
{
return s.length()*2;
}
}
test t=new test();
int s=t.sz("12345678");
System.out.println(s);
}
}
结果:16
六、static的用法
static声明的数据成员和方法在内存中只有一个,无论类创建了多少个对象。
非static字段每个对象内存中都有一个。
七、Java程序
//hello.java
import java.util.*;
public class hello
{
public static void main(String args[])
{
System.out.println("hhhhhh");
System.out.println(new Date());
}
}
结果:
hhhhhh
Sat Jun 21 17:35:00 CST 2014
//:object/hello.java
import java.util.*;
public class hello
{
public static void main(String args[])
{
System.getProperties().list(System.out); //JDK的属性
System.out.println(System.getProperty("user.name")); //查询用户名
System.out.println(System.getProperty("java.library.path")); //查询java.library.path
}
}
结果:
太长了~~
【用户名】
/usr/java/packages/lib/i386:/lib:/usr/lib
第3章 操作符
3.4 赋值
class test
{
int i;
}
public class hello
{
public static void main(String args[])
{
test t1=new test();
test t2=new test();
t1.i=111;
t2.i=222;
System.out.println(t1.i+" "+t2.i);
t1=t2; //这里会产生严重的后果,t1会引用t2的对象,原先t1引用的对象会被垃圾回收器销毁,改为t1.i=t2.i则无此问题
System.out.println(t1.i+" "+t2.i);
t1.i=333;
System.out.println(t1.i+" "+t2.i);
}
}
结果:
111 222
222 222
333 222
class test
{
char c;
}
public class hello
{
static void f(test t)
{
t.c='A';
}
public static void main(String[] args)
{
test t=new test();
t.c='B';
System.out.println(t.c);
f(t);
System.out.println(t.c);
}
}
结果:
B
A
注意:char的字符只有一个,这样定义char c='AB';是错误的。
3.5 算术运算符
import java.util.*;
public class hello
{
public static void main(String[] args)
{
Random rand=new Random();
int i=rand.nextInt(10);
double j=rand.nextDouble(); //随机数在0.0~1.0之间
System.out.println(i+" "+j);
}
}
结果:0 0.5139524879833011
public class hello
{
public static void main(String[] args)
{
int a=8;
int x=-a;
System.out.println(x);
}
}
结果:-8
3.7 关系操作符
boolean型
public class hello
{
public static void main(String[] args)
{
Integer i=new Integer(99);
Integer j=new Integer(99);
System.out.println(i==j);
System.out.println(i!=j);
}
}
结果:
false
true
为什么?
==和!=比较的是对象的引用。虽然跟i和 j的对象内容相同,但是两者的引用却不同,意思是两者在内存中的位置不同。
引用:
public Integer(int value)
Integer
object that represents the specified
int
value.
-
Parameters:
-
value
- the value to be represented by theInteger
object.
public class hello
{
public static void main(String[] args)
{
Integer i=new Integer(99);
Integer j=new Integer(99);
System.out.println(i.equals(j)); //比较两者的内容
}
}
结果:true
class test
{
int i;
}
public class hello
{
public static void main(String[] args)
{
test t1=new test();
test t2=new test();
t1.i=t2.i=99;
System.out.println(t1.equals(t2));
}
}
结果:false
参考:http://blog.csdn.net/barryhappy/article/details/6082823
3.8 逻辑操作符
import java.util.*;
public class hello
{
public static void main(String[] args)
{
Random rand=new Random();
int i=rand.nextInt(99);
int j=rand.nextInt(99);
System.out.println(i+" "+j+" "+(i==j));
}
}
结果:60 12 false
在hello类中添加方法,然后在main()函数中调用方法。
import java.util.*;
public class hello
{
static boolean test1(int val)
{
System.out.println("test1("+val+")");
return val<8;
}
public static void main(String[] args)
{
System.out.println(test1(9));
}
}
结果:
test1(9)
false
3.9 直接常量
int 用16进制表示。
public class hello
{
public static void main(String[] args)
{
int i=0xFFFF;
char c=0xFFFF;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toBinaryString(c));
}
}
结果:
1111111111111111
1111111111111111
可参考:http://blog.csdn.net/swit1983/article/details/4427119
3.12 三元操作符if-else
条件运算符conditional operator:http://zh.wikipedia.org/zh-cn/%E6%9D%A1%E4%BB%B6%E8%BF%90%E7%AE%97%E7%AC%A6
public class hello
{
static int compare(int x,int y)
{
return x>=y?x:y;
}
public static void main(String[] args)
{
int i=0;
int j=9;
System.out.println(compare(i,j));
}
}
结果:9
3.13 字符串操作符+和+=
public class hello
{
public static void main(String[] args)
{
int i=0;
int j=1;
int k=2;
String s="aaaaa";
System.out.println(s+i+j+k);
s+="bbbbb";
System.out.println(s);
}
}
结果:
aaaaa012
aaaaabbbbb
3.15 类型转换操作符
public class hello
{
public static void main(String[] args)
{
int i=99;
double j=99.00;
System.out.println((double)i+" "+(int)j);
}
}
结果:99.0 99
public class hello
{
public static void main(String[] args)
{
double i=99.4;
double j=99.5;
System.out.println((int)i+" "+(int)j); //四舍五入的用法
System.out.println(Math.round(i)+" "+Math.round(j));
}
}
结果:
99 99
99 100
第四章 控制执行流程
有返回值计算大小
public class hello
{
static int ifelse(int a,int b){
int max=0;
if(a>=b)
max=a;
else if(b>a)
max=b;
return max;
}
public static void main(String[] args)
{
System.out.println(ifelse(100,999));
}
}
结果:999
public class hello
{
static int compare(int x,int y)
{
if(x>=y)
return x;
else
return y;
}
public static void main(String[] args)
{
System.out.println(compare(1,2));
}
}
结果:2
利用return返回值。
无返回值计算大小
public class hello{
static int max=0; //max放在方法test里面会报错
static void test(int x,int y){
if(x>y)
max=x;
else if(x<y)
max=y;
}
public static void main(String[] args)
{
test(1,2);
System.out.println(max);
}
}
public class hello
{
static boolean test(){
boolean b=Math.random()<0.5;
System.out.println(b);
return b;
}
public static void main(String[] args)
{
while(test())
{
System.out.println("testtest");
}
}
}
random一个随机数,区间 [0,1),如果随机数<0.5,输出true并输出testtest,一直循环;如果随机数>=5,只输出一个false ,while()循环体语句不输出。
public class hello
{
public static void main(String[] args)
{
for(char c=0;c<128;c++)
if(Character.isLowerCase(c))
System.out.println("the value of "+c+" is "+(int)c);
}
}
结果:
the value of a is 97
the value of b is 98
the value of c is 99
the value of d is 100
the value of e is 101
the value of f is 102
the value of g is 103
the value of h is 104
the value of i is 105
the value of j is 106
the value of k is 107
the value of l is 108
the value of m is 109
the value of n is 110
the value of o is 111
the value of p is 112
the value of q is 113
the value of r is 114
the value of s is 115
the value of t is 116
the value of u is 117
the value of v is 118
the value of w is 119
the value of x is 120
the value of y is 121
the value of z is 122
用isLowerCase()方法检查字符是否为小写字母,是的话输出。
import java.util.*;
public class hello
{
public static void main(String[] args)
{
Random rand=new Random();
int a[]=new int[10];
double d[]=new double[10];
for(int i=0;i<10;i++)
d[i]=rand.nextDouble();
for(int i=0;i<10;i++)
a[i]=rand.nextInt(100);
for(double x:d)
System.out.println(x+" ");
for(int y:a)
System.out.println(y+" ");
}
}
结果:
0.9349957297537359
0.45534728046826367
0.14347562886058995
0.20728509550669916
0.3676952326758317
0.8437695597256385
0.2816159603241202
0.27034262478599047
0.7912814034374865
0.846955152892147
95
79
1
59
51
15
51
92
56
92
随机生成int 型,区间[100,0]的10个数,赋值给长度为10的int数组,将数组元素一个一个输出。
随机生成doubel型,区间[0,1)的10个数,赋值给长度为10的double数组,将数组元素一个一个输出。
import java.util.*;
public class hello
{
public static void main(String[] args)
{
String s="abbcccrrrrrttttttt";
for(char c:s.toCharArray())
System.out.print(c+" ");
}
}
结果:
a b b c c c r r r r r t t t t t t t
将一个字符串的所有字符,存储在一个数组中,然后遍历输出。
public class hello
{
public static void main(String[] args)
{
for(int i=0;i<100;i++)
{
if(i==50) break; //exit the loop
if(i%9!=0) continue;
System.out.println(i+" ");
}
System.out.println("ttttttttttttttttttttttttt");
int i=0; //想输出0的话int i=-1;
while(true)
{
i++;
int j=i*10;
if(j==1000) break;
if(i%9!=0) continue;
System.out.println(i);
}
}
}
结果:
0
9
18
27
36
45
ttttttttttttttttttttttttt
9
18
27
36
45
54
63
72
81
90
99