package com.org.lxh;

import java.math.BigDecimal;

/**
* Java基础知识
* @author hemmingway <hemmingway@163.com>
*
*/
public class HelloWord {

/**
* 6. final参数可以修改吗? 答案是否定的
* @param x
* @return
*/
public int addOne(final int x){
//return ++x; //改变final参数的值,出错
int xx = x;
return ++xx;
}


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello word!");

//1.变量和常量的区别
final int CONST = 10; //定义并初始化常量
int num = 100; //定义并初始化变量
System.out.println("变量num的初始值是: " + num);

num = 180;

System.out.println("变量num的值改变以后是: " + num);

System.out.println("常量CONST的值是: " + CONST);

//CONST = 200; //error


//2.Java的默认浮点类型是double, 下面的语句是错误的
//float pi = 3.1415926;
float pi = (float) 3.1415926;
double pi2 = 3.1415926;
System.out.println("pi常量为:" + pi + ", 2*pi=" + 2 * pi2);


//3. "short s=1; s=s+1;" 能否正确编译,答案是否, Java的默认数字的int类型
short s = 1;
//s = s + 1;
s = (short) (s + 1);


//4. short与char的取值范围是多少?
//short是-32768~32767
//char是0~65535

//5. char变量能否保存一个汉字? 能
char ch = '我';
System.out.println("char变量能否保存一个汉字? 能, ch= " + ch);

ch = 0x5323; //从Unicode字符映射表中查出的汉字的
System.out.println("char变量改变以后, ch= " + ch);

//6. final参数可以修改吗? 答案是否定的, 见上面的addOne()函数


//7.private, protected, public 不可以修饰局部变量
//private String s = "";

//8. 表达式3-2.6==0.4输出什么?
System.out.println(3-2.6); //输出0.3999999999999999
System.out.println(3-2.6==0.4); //输出false

//9. 使用 java.math.BigDecimal类进行浮点数计算
BigDecimal x = new BigDecimal("2");
BigDecimal y = new BigDecimal("5.6");
BigDecimal z = x.subtract(y);
double value = z.doubleValue();
System.out.println("BigDecimal value is : " + value);

//10. 自增与自减运算符的区别
int a = 10;
int b = 10;
int c = 10;
int d = 10;
a++;
++b;
c--;
--d;
System.out.println("a++运算后a的值是:" + a);
System.out.println("++b运算后b的值是:" + b);
System.out.println("c--运算后c的值是:" + c);
System.out.println("--d运算后d的值是:" + d);

//当参与表达式运算后显现区别
a = 3;
b = 5;
c = 0;
c = b++ *5 / a++;
System.out.println("a=" + a + "\tb=" + b + "\tc=b++ *5 / a++ = " + c);

c=b++ + ++a;
System.out.println("a=" + a + "\tb=" + b + "\tc=b++ + ++a = " + c);

c=a++ - ++b;
System.out.println("a=" + a + "\tb=" + b + "\tc=a++ - ++b = " + c);


//11. 用有效率的方法计算2乘以16等于几? << >> >>>
int val = 2<<4;
System.out.println("用位运算计算2乘以16,int val = 2<<4等于 " + val);

//12. &与&&区别
//位运算符 & | ^
//逻辑运算符&& || ~


//13. =与==区别
//赋值运算符 = += -= *= /= %= &= ^=
//关系运算符 > < >= <= == !=


//14. 表达式9/2与9/2.0的结果是否相等?
int val1 = 9/2; //int
double val2 = 9/2.0; //double
System.out.println("9/2=" + val1 + " 9/2.0="+val2);


//15. 符合运算符注意范围
short money1 = 30000;
int money2 = 35000;
money1 += money2; //注意money1溢出,造成损失
//money1 = money1 + monet2; //这样会提示错误信息,修正
System.out.println(money1);

//16.小心乘法的溢出
long lnum = 2147483647*10;
System.out.println("long lnum = 2147483647*10溢出值为:" + lnum);

lnum = 2147483647L*10;
System.out.println("lnum = 2147483647L*10结果不会溢出了:" + lnum);

//17. 计算2的n次幂
int result = 1<<20;
System.out.println("2的20次幂为:" + result);


}

}




hello word!

变量num的初始值是: 100

变量num的值改变以后是: 180

常量CONST的值是: 10

pi常量为:3.1415925,2*pi=6.2831852

char变量能否保存一个汉字?能, ch= 我

char变量改变以后, ch= 匣

0.3999999999999999

false

BigDecimal value is : -3.6

a++运算后a的值是:11

++b运算后b的值是:11

c--运算后c的值是:9

--d运算后d的值是:9

a=4   b=6   c=b++ *5 / a++ = 8

a=5   b=7   c=b++ + ++a = 11

a=6   b=8   c=a++ - ++b = -3

用位运算计算2乘以16,int val = 2<<4等于 32

9/2=4    9/2.0=4.5

-536

long lnum = 2147483647*10溢出值为:-10

lnum = 2147483647L*10结果不会溢出了:21474836470

2的20次幂为:1048576