您的位置:首页 > 编程语言 > Java开发

Java基础(极客)——01、Java编程基础知识入门:变量与数据类型

2015-05-26 11:21 1061 查看
/**

* 4、用变量简化计算

* 【示例-1】保存中间结果,用分步骤计算简化复杂运算

计算(sqrt(20)+sqrt(10)/(sqrt(20)-sqrt(10))=

要求:显示结果保留小数点后1位,即对小数点后第2位四舍五入。

上面式子的意思:20的平方根+10的平方根/20的平方根—10的平方根

*

*/

public class BianLiangDemo1 {

public static void main(String[] args) {

double sqrt20 = Math.sqrt(20);//20的平方根

double sqrt10 = Math.sqrt(10);//10的平方根

double result = (sqrt20 + sqrt10) / (sqrt20 - sqrt10);//算出结果

result = Math.round(result * 10) / 10.0;

System.out.print("(sqrt(20)+sqrt(10)/(sqrt(20)-sqrt(10))=");

System.out.print(result);

}

}

/**

*5、用变量保存多种类型的数据

*

*/

public class BianLiangDemo2 {

public static void main(String[] args) {

//定义变量,并初始化

String name = "张飞";

char sax = '男';

int age = 35;

float height = 1.99f;

String type = "豪放的";

System.out.println("我叫" + name + "性别" + sax + "年龄" + age + "身高" + height + "我的性格是" + type);

//给变量重新赋值

name = "王菲";

sax = '女';

age = 30;

height = 1.6f;

type = "娇小的";

System.out.println("我叫" + name + "性别" + sax + "年龄" + age + "身高" + height + "我的性格是" + type);

}

}

/**

*6、变量的作用域

*作用域就是大括号

*

*/

public class BianLiangDemo3 {

public static void main(String[] args) {

mathod3();

}

public static void mathod1() {

{

String name = "张飞";

// String name = "王菲";变量名重复

}

}

public static void mathod2() {

{

String name = "张飞";

System.out.println(name);

}//大括号中的内容被执行完了就会被虚拟机回收

String name = "王菲";

System.out.println(name);

}

public static void mathod3() {

String name = "王菲";

System.out.println(name);

{

//String name = "张飞";重命名变量

System.out.println(name);

}//大括号中的内容被执行完了就会被虚拟机回收

}

}

/**

*7、基本数据类型的包装类

* 八中数据类型都有对应的包装类,名称几乎就是对基本类型第一个字母大写

*

*/

public class BianLiangDemo4 {

public static void main(String[] args) {

//验证包装类中的一些常亮和方法

System.out.println(Byte.MIN_VALUE + "~" + Byte.MAX_VALUE);//byte的最小值和最大值

System.out.println(Byte.parseByte("100") + 10);

System.out.println(Integer.MIN_VALUE + "~" + Integer.MAX_VALUE);//int的最大值和最小值

System.out.println(Integer.parseInt("100") + 20);

System.out.println(Integer.toBinaryString(16));//把十进制转化成二进制

System.out.println(Integer.toHexString(16));//把十进制转化成十六进制

}

}

/**

*8、二进制补码

*

*/

public class BianLiangDemo5 {

public static void main(String[] args) {

}

}

/**

*9、整数类型

*byte short int long

*默认是int类型

*

*/

public class BianLiangDemo6 {

public static void main(String[] args) {

method1();

}

public static void method1() {

long value1 = 2000000000;//java默认是int当超出int的取值范围,则用long;

long value2 = 3000000000L;

}

public static void method2() {

}

}

/**

10、浮点类型

*

*/

public class BianLiangDemo7 {

public static void main(String[] args) {

}

}

/**

11、字符类型

* java底层用用一个16位的整数来处理字符类型,该数值是一个字符的unicode编码值

* 占两个字节,16位

*

**/

public class BianLiangDemo8 {

public static void main(String[] args) {

// unicode码对应的0~127的字符

for (int i = 0; i < 127; i++) {

System.out.println(i + ":" + (char) i);

}

}

}

/**

12、转义符:\

"\n"表示换行

**/

public class BianLiangDemo9 {

public static void main(String[] args) {

}

}

/**

13、布尔类型的概念和用法

**/

public class BianLiangDemo10 {

public static void main(String[] args) {

boolean isPass = true;

System.out.println("考试通过" + isPass);

isPass = false;

System.out.println("考试通过" + isPass);

isPass = 65 >= 60;

System.out.println("考试通过" + isPass);

}

}

/**

14、小类型向大类型转换

自动转换不需要手动写代码操作

**/

public class BianLiangDemo11 {

public static void main(String[] args) {

mathod3();

}

public static void mathod1() {

//大类型转换成小类型

int i = -2;

long l = i;

System.out.println(i + ":" + Integer.toBinaryString(i));

System.out.println(l + ":" + Long.toBinaryString(l));

/*打印出来的结果:

* -2:11111111111111111111111111111110

-2:1111111111111111111111111111111111111111111111111111111111111110

*/

}

/**

* int转为float精度丢失

*/

private static void mathod2() {

int i = 0x1000001;//16进制

float f = i;

System.out.println("二进制:" + Integer.toBinaryString(i) + "十进制" + i);

System.out.println("二进制:" + Integer.toBinaryString((int) f) + "十进制" + f);

/*打印结果二进制和十进制都丢了最后一位

* 二进制:1000000000000000000000001十进制16777217

二进制:1000000000000000000000000十进制1.6777216E7

*/

}

/**

* long转为dounle精度丢失

*/

private static void mathod3() {

long l = 0x20000000000001L;

double d = l;

System.out.println("二进制:" + Long.toBinaryString(l) + "十进制" + l);

System.out.println("二进制:" + Long.toBinaryString((long) d) + "十进制" + d);

/*

* 打印结果丢失最后一位

* 二进制:100000000000000000000000000000000000000000000000000001十进制9007199254740993

二进制:100000000000000000000000000000000000000000000000000000十进制9.007199254740992E15

*/

}

}

/**

16、大类型向小类型转换

大类型转换成效类型时要叫做强制转换简称强转

强转是要注意边界数风险问题(就是大类型的数据超过小类型的取值范围)

**/

public class BianLiangDemo12 {

public static void main(String[] args) {

//把int转化成byte

int i = 12;

byte b = (byte) i;

}

}

/**

17、int类型与char类型的转换

java对char类型的数据在底层是按int来处理的,只是显示的时候任然给我们看到的是字符型

8钟基本数据类型除了布尔之外都可相互转化。

**/

public class BianLiangDemo13 {

public static void main(String[] args) {

method2();

}

/**

* java对char类型的数据在底层是按int来处理的,只是显示的时候任然给我们看到的是字符型

*/

private static void method1() {

char c = 65;

int i = 'B';

System.out.println(c + "," + i);

}

/**

* 字符类型的数据可进行数值计算

*/

private static void method2() {

char c = 'A';

int i = c + 1;

c = (char) (c + 1);

System.out.println(c + ":" + i);

}

}

/**

* 18、通过args数组获取数据

*/

public class BianLiangDemo14 {

public static void main(String[] args) {

int a = Integer.parseInt(args[0]);

int b = Integer.parseInt(args[1]);

System.out.println("a+b=" + (a + b));

System.out.println("a-b=" + (a - b));

System.out.println("a*b=" + (a * b));

System.out.println("a/b=" + (a / b));

/*

*测试是我时候选择run右边的倒三角-----Run Configurations

*选中Java Application中的随便项目,邮件new加入项目

*点击Arguments 在program arguments中输入两个数字比如11 12

*点击run就运行了

*

*

*/

}

}

/**

* 19、通过Scanner从控制台获取数据

1、String next();

作用:接收控制台输入的一个字符串。

2、String nextLine();

作用:接收控制台输入的一个字符串。

3、int nexInt();

作用:接收控制台输入的一个int类型的数据。

4、double nextDouble();

作用:接收控制台输入的一个double类型的数据。

5、boolean nextBoolean();

作用:接收控制台输入的一个boolean类型的数据。

*/

public class BianLiangDemo15 {

public static void main(String[] args) {

}

}

/**

20、控制台输入数据的案例

*/

public class BianLiangDemo16 {

// 通过控制台输入一个人的姓名、性别、年龄、身高和性格,然后让该人和大家做自我介绍。

public static void main(String[] args) {

//创建Scanner对象

Scanner scanner = new Scanner(System.in);

//向控制台输出文本

System.out.println("姓名:");

//接收输入姓名的字符串

String name = scanner.next();

System.out.println("性别:");

char sex = scanner.next().charAt(0);

System.out.println("年龄:");

int age = Integer.parseInt(scanner.next());

System.out.println("身高:");

double height = Double.parseDouble(scanner.next());

System.out.println("性格:");

String type = scanner.next();

System.out.println("我叫:" + name + "我是" + sex + "年龄" + age + "身高" + height + "性格" + type);

}

}

/**

* 21、变量的自加、自减、自乘、自除运算

*/

public class BianLiangDemo17 {

public static void main(String[] args) {

mathod4();

}

/**

* 自加

*/

private static void mathod1() {

//int自增

int i = 0;

while (i < 5) {

System.out.println(i + "");

i++;

}

//自增

int j = 0;

j = 0 + 1;

/* 简写形式:++1先加在执行,1++先执行再加

* i+=n;等效于i=i+n;

*/

//字符串自加

String s = "张飞";

s = s + "aaa";

System.out.println(s);

//简写形式

String str = "张飞";

str += "bbb";

System.out.println(str);

}

/**

* 自减

*/

private static void mathod2() {

//int自增

int i = 4;

while (i > 0) {

System.out.println(i + "");

i--;

}

//自增

int j = 0;

j = 0 - 1;

/* 简写形式:--1先减在执行,1++先执行再减

* i-=n;等效于i=i-n;

*/

}

/**

* 自乘

*/

private static void mathod3() {

/* i*=n;

* 等效于

* i=i*n;

*

*

*/

int i = 5;

while (i < 30) {

System.out.println(i);

i *= 2;

}

}

/**

* 自除

*/

private static void mathod4() {

/* i/=n;

* 等效于

* i=i/n;

*

*

*/

int i = 50;

while (i > 0) {

System.out.println(i);

i /= 2;

}

}

}

/**

* 22、交换两个变量的值

*/

public class BianLiangDemo18 {

public static void main(String[] args) {

method2();

}

/**

* 交换两个变量的值,借助中间变量

*/

private static void method1() {

//要是ab的是互换可以借助的三个变量

int a = 10;

int b = 100;

int c = a;

a = b;

b = c;

System.out.println("a=" + a + ":" + "b=" + b);

}

/**

* 交换两个变量的值,不借助中间变量

*/

private static void method2() {

int a = 10;

int b = 100;

a = a + b;//结果是a=a+b,b=b a=110

b = a - b;//结果是a=a+b,b=a 110-100

a = a - b;//a=b,b=a; 110-10

System.out.println("a=" + a + ":" + "b=" + b);

}

}

/**

* 23、Java的错误类型

*/

public class BianLiangDemo19 {

public static void main(String[] args) {

}

}

/**

*24、Debug调试

*/

public class BianLiangDemo20 {

public static void main(String[] args) {

//要是ab的是互换可以借助的三个变量

int a = 10;

int b = 100;

a = b;

b = a;

System.out.println("a=" + a + ":" + "b=" + b);

}

}

源码下载:
http://download.csdn.net/detail/zhaihaohao1/8741715
视频下载:
http://c38.yunpan.360.cn/my/index/#%2F%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%2Fjava%2F
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐