java学习笔记1-thinking in java

来源:互联网 发布:java构造方法有几种 编辑:程序博客网 时间:2024/05/17 01:01
//1.暂停输出五秒钟
//Property.java
import java.util.*;
/*
多行注释
*/


/**
文档注释
*/
public class Property { 
public static void main(String[] args) {
Systemout.println(new Date());
Properties p = SystemgetProperties();
p.list(Systemout);
Systemout.println("---MemoryUsage:");
Runtime rt = Runtime.getRuntime();
Systemout.println("Total Memory = " + rt.totalMemory() 
                 + "FreeMemory = " + rt.freeMemory());
 
try{
Thread.currentThread().sleep(5 * 1000);
} catch(InterruptedException e) {}
}
}


//2.对象操作,其实是句柄操作
//:Assignment.java
// Assignment with objects is a bit tricky
package c03;


class Number {
int i;
}


public class Assignment {
public static void main(String[] args){
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
Systemout.println("1:n1.i" + n1.i + ",n2.i" + n2.i);
n1 = n2;
Systemout.println("2:n1.i" + n1.i + ",n2.i" + n2.i);
n1.i = 27;
Systemout.println("3:n1.i" + n1.i + ",n2.i" + n2.i);
}
}


//3.别名
//:PassObject.java
//Passing objects to methods can be a bit tricky
class Letter {
char c;
}


public class PassObject {
static void f(Letter y){
y.c = 'z';
}
public static void main(String[] args) {
Letter x = new Letter();
x.c = 'a';
Systemout.println("1:x.c:" + x.c);
f(x);
Systemout.println("2:x.c:" + x.c);
}
}


//4.运算符
//:MathOps.java
// Demonstrates the mathmatical operators
import java.util.*;


public class MathOps {
// Create a short hand to save typing:
static void prt(String s){
Systemout.println(s);
}
// short hand to print a string and an int:
static void pInt(String s,int i){
prt(s + " = " + i);
}
// short hand to print a string and a float:
static void pFlt(String s , float f){
prt(s + " = " + f);
}
public static void main(String[] args){
// Create a random number generator,
// seeds with current time by default:
Random rand = new Random();
int i,j,k;
// '%' limits maximum value to 99:
j = rand.nextInt() % 100;
k = rand.nextInt() % 100;
pInt("j",j);
pInt("k",k);
i = j + k;pInt("j+k",i);
i = j - k;pInt("j-k",i);
i = j * k;pInt("j*k",i);
i = k / j;pInt("k/j",i);
i = k % j;pInt("k%j",i);
float u,v,w;
v = rand.nextFloat();
w = rand.nextFloat();
u = v + w;pFlt("v+w",u);
u = v - w;pFlt("v-w",u);
u = v * w;pFlt("v*w",u);
u = v / w;pFlt("v/w",u);
u = v % w;pFlt("v%w",u);
  u += v;pFlt("u += v",u);
u -= v;pFlt("u -= v",u);
u *= v;pFlt("u -= v",u);
u *= v;pFlt("u -= v",u);
}
}


//5.自动递增和递减
// ++A:先执行运算,再生成值
// A++:先生成值,再执行运算
//:AutoInc.java
// Demonstrates the ++ and -- operators 


public class AutoInc {
public satic void main(String[] args) {
int i = 1;
prt("i:" + i);
prt("++i:" + ++i);
prt("i++:" + i++);
prt("i:" + i);
prt("--i:" + --i);
prt("i--:" + i--);
prt("i:" + i);
}
static  void prt(String s){
Systemout.println(s);
}
}


//6.关系运算符
//:Equivalence.java
public class Equivalence {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
Systemout.println(n1==n2);
}
} // 对象内容相同,但是句柄不同,此时应该用方法equals()
//:EqualsMethod.java
public class EqualsMethod {
public static void main(String[] args){
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
Systemout.println(n1.equals(n2));
}
}


//EqualsMethod2.java
class Value {
int i;
}
public class EqualsMethod2 {
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
Systemout.println(v1.equals(v2));
}
} //得到的是false,因为equals()默认行为是比较句柄,除非在自己的新类中改变它
//大多数Java类库都实现了equals(),所以它比较的是对象的内容而非句柄


//7.逻辑运算符
// 不可将一个非布尔值当做布尔值在逻辑表达式中使用


//8.造型运算符(必须最左侧)
/*
  void casts() {
 int i = 200;
 long l =(long)i;
 long l2 = (long)200;
  }
*/


// 字面值
// Lirerals.java


class Literals {
char c = 0xffff; // max char hex(十六进制) value 
byte b = 0x7f; // max byte hex value
short s = 0x7fff; // max short hex value
int i1 = 0177; //Octal(八进制)
long n1 = 200l; //long suffix(后缀)
float f1 = 1f; //float suffix
float f2 = 1e-45f; //10 to the power(如果没有f就会出错,因为指数默认作为double处理)
double d1 = 1d; //double suffix
double d2 = 47e47d; //10 to the power
}
// java没有sizeof()运算符,因为所有数据类型在所有机器的大小都是相同的
// 布尔值除了测试他为真还是为假,不可进行其他任何类型运算





0 0
原创粉丝点击