代码改变世界

我的JAVA学习(1)

2011-03-12 11:03  Rollen Holt  阅读(592)  评论(0编辑  收藏  举报

我的第一个代码:

  1: 
  2: public class BasicTypeChange 
  3: {
  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类型强转成float
 36: 		long l = (long) d;						//将double类型强转成long
 37: 		int i = (int) d;							//将double类型强转成int
 38: 		short s = (short) d;						//将double类型强转成short
 39: 		byte b = (byte) d;						//将double类型强转成byte
 40: 		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: 
images (10)