我的JAVA学习(1)
2011-03-12 11:03 Rollen Holt 阅读(592) 评论(0) 编辑 收藏 举报我的第一个代码:
1:2: public class BasicTypeChange3: {4: public static void main(String [] agrs)5: {6: System.out.println("I Love You");
7: }8: }9:
基本类型:
1: //package com.zf.s2; //创建一个包
2: public class BasicTypeChange { //修饰基本数据类型转换的类3: private void typeAutoUpgrade() { //基本类型的自动提升4: byte b = 44;
5: char c = 'b';
6: short s = 1024;
7: int i = 40000;
8: long l = 12463l;
9: float f = 35.67f;
10: double d = 3.1234d;
11: //result声明为其他类型会出错,除非进行类型转换
12: double result = (f * b) + +(l * f) + (i / c) - (d * s);
13: System.out.print(" 结果 :" + result+" = ");14: System.out.println((f * b) + " + " + (l * f) + " + " + (i / c) + " - "15: + (d * s)); //输入经过运算获得的结果
16: }17: private void autoChange() { //基本类型的自动转换18: char c = 'a';
19: byte b = 44;
20: short s0 = b;
21: int i0 = s0;
22: int i1 = c;
23: long l = i0;
24: float f = l;
25: double d = f;
26: float fl = 1.7f;
27: double dou = fl;
28: System.out.println("fl = " + fl + "; dou = " + dou);29: // 一个数从一种类型转换成另外一种类型,再转换回来时,值还是一样。
30: fl = (float)dou;
31: System.out.println("fl = " + fl + "; dou = " + dou);32: }33: private void forceChange() { //强制类型转换34: double d = 123.456d;
35: float f = (float) d; //将double类型强转成float36: long l = (long) d; //将double类型强转成long37: int i = (int) d; //将double类型强转成int38: short s = (short) d; //将double类型强转成short39: byte b = (byte) d; //将double类型强转成byte40: System.out.print("d = " + d + "; f = " + f + "; l = " + l);41: System.out.println("; i = " + i + "; s = " + s + "; b = " + b);42: d = 567.89d;43: // 下面的转换首先进行截断操作,将d的值变为567,因为567比byte的范围256还大,
44: // 于是进行取模操作,567对256取模后的值为55
45: b = (byte) d;
46: System.out.println("d = " + d + "; b = " + b);47: }48: public static void main(String[] args) { //Java程序主入口方法49: BasicTypeChange change = new BasicTypeChange();//实例化对象50: change.typeAutoUpgrade(); //调用基本类型的自动提升方法
51: change.autoChange(); //调用基本类型的自动转换方法
52: change.forceChange(); //调用强制类型转换方法
53: }54: }55:
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================