. 例 1-1 class DoubleVariableDemo{ // 定义一个类 public static void main(String args[]){ double dRadius=; // 定义一个双精度型变量, 表示是一个双精度的数 double dArea=*dRadius*dRadius; // 定义一个变量, 表示 pi 是数学中一个常量。 (" 半径为 2 的圆的面积: "+dArea); // 输出圆的面积}}例 1-2 class BooleanConstantDemo{ public static void main(String args[]){ final boolean BFLAG=true; // 定义一个布尔型的常量 if(BFLAG) //当 BFLA G为 tur e 时执行下面的语句 ("Always Print Out Me!"); Else// 当为 false 时执行下面的语句 ("Never Print Out Me!"); // 输出}}例 1-3 class DecisionDemo{ public static void main(String args[]){ short x=1,y=2,z; // 错误代码//z=x+ y; // 正确代码 z=(short)(x+y); // 强制类型转换 (z); }}例 1-4 IntegerConstantDemo . java class IntegerConstantDemo{ public static void main(String args[]){ final byte nMaxValue_1=100; // 定义一个字节型常量 final short nMaxValue_2=200; // 定义一个短整型常量 final int nMaxValue_3=300; // 定义一个整型常量 final long nMaxValue_4=400; // 定义一个长整型常量 (" 整数常量的 nMaxValue_1 值"+nMaxValue_1);// 输出-1 的值 (" 整数常量的 nMaxValue_2 值"+nMaxValue_2);// 输出-2 的值 (" 整数常量的 nMaxValue_3 值"+nMaxValue_3);// 输出 3 的值. (" 整数常量的 nMaxValue_4 值"+nMaxValue_4); // nMaxValue_1 = nMaxValue_1 + 1; 不能更改常量的值}}例 1-5 class DataConversionDemo{ public static void main(
java入门基础代码 来自淘豆网m.daumloan.com转载请标明出处.