实例2 自动提升
public class BasicTypeChange{ //创建一个修饰基本数据类型转换的类
public static void main(String[] args) {
byte b = 12;
char c = 'b';
short s = 13;
int i = 567;
long l = 678756;
float ft = 23.67f;
double db = 6.124d;
//result声明为其他类型会出错,除非进行类型转换
double result = (ft * b) + +(l * ft) + (i / c) - (db * s);
System.out.print(" 结果 :" + result+" = ");
System.out.println((ft * b) + " + " + (l * ft) + " + " + (i / c) + " - "
+ (db * s)); //输入经过运算获得的结果
}
}
实例3 自动转换
public class BasicAutoChange {
// 基本类型的自动转换
public static void main(String[] args) {
char c = 'a';
byte b = 44;
short s0 = b;
int i0 = s0;
int i1 = c;
long l = i0;
float f = l;
double d = f;
float fl = 1.7f;
double db = fl;
System.out.println("fl = " + fl + "; dou = " + db);
// 一个数从一种类型转换成另外一种类型,再转换回来时,值还是一样。
fl = (float) db;
System.out.println("fl = " + fl + "; dou = " + db);
}
}
实例4 常用基础类型之强制转换
public class BasicForceChange {
public static void main(String[] args) { // 基本类型的强制转换
double db = 143.426d;
float f = (float) db; //将double类型强转成float
long l = (long) db; //将double类型强转成long
int i = (int) db; //将double类型强转成int
short s = (short) db; //将double类型强转成short
byte b = (byte) db; //将double类型强转成byte
System.out.print("d = " + db + "; f = " + f + "; l = " + l);
System.out.println("; i = " + i + "; s = " + s + "; b = " + b);
db = 5127.87d;
// 下面的转换进行截断操作
b = (byte) db;
System.out.println("d = " + db + "; b = " + b);
}
}
实例5 算术运算符
public class OperatorSign {
public static void main(String[] args) {
int a = 3, b = 4;
int intA = a / b; // 根据基本类型自动转换规则,除数与被除数都是整数,
float intB = a / b;
float intC = a / (b * 2.0f); // 对于声明为float类型的数字,数字后加f,
double intD = a / (b * 2.0d); // 对于声明为double类型的数字,数字后加d.
int a1 = 10;
int b1 = 3;
double c = 2;
System.out.println("(intA=a/b),intA=" + intA);
System.out.println("(intB=a/b),intB=" + intB);
System.out.println("(intC=a/(b*2.0f)),intC=" + intC);
System.out.println("(intD=a/(b*2.0d)),intD=" + intD);
System.out.println(a1 + "/" + b1 + "=" + (a1 / b1));
System.out.println(a1 + "%" + b1 + "=" + (a1 % b1));
System.out.println(a1 + "/" + c + "=" + (a1 / c));
System.out.println(a1 + "%" + c + "=" + (a1 % c));
int x = 12;
int a2 = x + x++; //定义一个自增的变量
System.out.println("a2=" + a2);
System.out.println("x=" + x);
int b2 = x++ + x;
System.out.println("b2=" + b2);
System.out.println("x=" + x);
int c2 = x + x--; //定义一个自减的变量
System.out.println("c2=" + c2);
System.out.println("x=" + x);
int d = x + --x;
System.out.println("d=" + d);
System.out.println("x=" + x);
}
}
实例6 关系运算符
public class RelationTest {
public static void main(String[] args) {
boolean i,j,k,x, y, z; //定义6个布尔型的常量
int a = 11;
int b = 2;
i = a>=b;
j = a<=b;
k = a==b;
x = a > b;
y = a < b;
z = a != b;
System.out.println("x=" + x);
System.out.println("y=" + y);
System.out.println("z=" + z);
System.out.println("i=" + i);
System.out.println("j=" + j);
System.out.println("k=" + k);
}
}
实例7 逻辑运算符
public class LSign {
public static void main(String[] args) {
boolean a, b,c, d, e ;
a = 'a' > 'b';
b = 'R' != 'r';
c = !a;
d = a && b;
e = a || b;
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println("e=" + e);
}
}
实例8 位运算符
public class ByteOperation {
public static void main(String[] args) {
int a = 32;
int b = 12;
int x = a & b;
int y = a | b;
int z = a ^ b;
System.out.println("输出的结果是:"+a + "&" + b + "=" + x);
System.out.println("输出的结果是:"+a + "|" + b + "=" + y);
System.out.println("输出的结果是:"+a + "^" + b + "=" + z);
}
}
实例9 移位运算符
public class Bit {
public static void main(String[] args) {
int a = 35;
int b = 2;
int c = a << b;
int d = a >> b;
int e = a >>> b;
System.out.println(a + "<<" + b + "=" + c);
System.out.println(a + ">>" + b + "=" + d);
System.out.println(a + ">>>" + b + "=" + e);
}
}
实例10 转型运算符
public class Type {
public static void main(String[] args) {
int a;
double b;
a = (int) 46.5 + (int) 14.7;// 强制转型可能引起精度丢失
b = (double) a;
System.out.println("a=" + a);
System.out.println("b=" + b);
}
}
实例11 常量与变量
class TestClass {
final String string = "你好!"; // 声明常量并赋值
final double dou = 3.14; // 声明常量并赋值
}
public class Control_10 {
int number = 0; // 定义int型变量number
public void print(Object obj) { // 创建print方法,其作用是将传入的Object型参数输出
System.out.println("对象Object:" + obj);
}
public void print(TestClass tc) { // 创建print方法,其作用是将传入的TestClass型参数输出
System.out.println("类TestClass:" + tc);
}
private void showObject() { // 显示实例化对象的信息
TestClass tc = new TestClass(); // 将对象实例化
System.out.println("TestClass.string= " + tc.string); // 输出TestClass对象的string属性值
System.out.println("TestClass.dou= " + tc.dou); // 输出TestClass对象的fPi属性值
}
public static void main(String[] args) {
Control_10 c10 = new Control_10(); // 创建Control_10类对象
c10.number = 3; // 给number变量赋值
System.out.println("number变量的值为: " + c10.number);
c10.showObject(); // 调用方法showObject()
c10.print(null); // 调用方法run()
}
}
实例12 各种进制的转换
import java.util.Scanner;
public class TextZhuanhuan { // 定义一个进制转换的类
public static int ZhuanToTen(int a, String str) { // 其它进制转成十进制
double d = 0; // 声明转换后的数值
String subString;
// 根据字符串的长度循环获得单个元素
for (int i = 0; i < str.length(); i++) {
subString = str.substring(i, i + 1); // 将字符串按循环截取
if (a == 16) { // 判断是否是十六进制
subString = sixteenToNumber(subString); // 将字母转换成数字
}
d += Integer.parseInt(subString) // 返回转换的结果
* Math.pow(a, str.length() - i - 1);
}
return (int) d;
}
public static String TenToNumber(int a, String str) { // 十进制转成其他进制
int current = Integer.parseInt(str); // 将字符转换成整数
String opResult = "";
// 判断转换后的数制是否是16进制
if (a == 16) {
// 判断传入的数是否大于16,大于则逢16进一
while (current >= a) {
opResult += sixteenNumberToChar(current % a);// 将数字转换成字母
current /= a;
}
if (current != 0)
opResult += sixteenNumberToChar(current);
} else {
// 判断传入的值是否大于转换后的数制
while (current >= a) {
opResult += current % a;
current /= a;
}
if (current != 0)
opResult += current;
}
String riResult = ""; // 倒序二进制字符串
// 根据二进制的转换方式进行循环输出
for (int i = opResult.length() - 1; i >= 0; i--) {
riResult = riResult + opResult.substring(i, i + 1);
}
return riResult;
}
public static String sixteenToNumber(String s) { // 十六进制字母对应数字
String num = "";
if (s.equals("A") || s.equals("a"))
num = "10";
else if (s.equals("B") || s.equals("b"))
num = "11";
else if (s.equals("C") || s.equals("c"))
num = "12";
else if (s.equals("D") || s.equals("d"))
num = "13";
else if (s.equals("E") || s.equals("e"))
num = "14";
else if (s.equals("F") || s.equals("f"))
num = "15";
else
num = s;
return num;
}
public static String sixteenNumberToChar(int num) { // 十六进制数字对应字母
String c = "";
if (num == 10)
c = "A";
else if (num == 11)
c = "B";
else if (num == 12)
c = "C";
else if (num == 13)
c = "D";
else if (num == 14)
c = "E";
else if (num == 15)
c = "F";
else
c = String.valueOf(num);
return c;
}
public static void main(String[] args) { // java程序的主入口处
String number; // 要转换的数
int a, b; // a表示转换前的进制,b表示转换后的进制
String result = ""; // 经过数制转换后的结果
String stop = "";
Scanner read = new Scanner(System.in); // 得到用户输入的值
do {
System.out.println("输入三个整数:待转换的数据 要转换之前的进制 要转换后的进制");
number = read.next();
a = read.nextInt();
b = read.nextInt();
stop = "Quit";
} while (stop != "Quit");
try {
if (a != 10) { // 判断转换前的数制是否是十进制
String temp = String.valueOf(ZhuanToTen(a, number)); // 转换成十进制的数
result = String.valueOf(TenToNumber(b, temp)); // 十进制转换成其它进制
} else {
result = String.valueOf(TenToNumber(b, number)); // 十进制转换成其它进制
}
System.out.println(a + "进制的数:" + number + ",转换成" + b + "进制的数为:"
+ result);
} catch (Exception e) {
System.out.print("转换失败,请正确输入!");
System.exit(-1);
}
}
}
实例13 Java中的进制与移位运算符
public class Control_12 {
public static void main(String[] args) {
System.out.println("对Int型数据进行移位的例子如下:\n");
Random rand = new Random(); // 实例化一个Random对象
int a = rand.nextInt(); // 生成一个int型的随机数变量a
int b = rand.nextInt(); // 生成一个int型的随机数变量b
// 以下方法均调用IntleftMove方法
IntleftMove("-1", -1);
IntleftMove("+1", +1);
int constant = 256987454; // 定义一个整型变量
IntleftMove("Constant", constant);
int negative = -256987454; // 定义一个整型变量
IntleftMove("Negative", negative);
IntleftMove("a", a);
IntleftMove("~a", ~a);
IntleftMove("-a", -a);
IntleftMove("b", b);
IntleftMove("a & b", b & b);
IntleftMove("a b", a);
IntleftMove("a ^ b", a ^ b);
IntleftMove("a << 3", a << 3);
IntleftMove("i >> 3", a >> 3);
IntleftMove("(~a) >> 3", (~a) >> 3);
IntleftMove("a >>> 3", a >>> 3);
IntleftMove("(~a) >>> 3", (~a) >>> 3);
System.out.println("\n对long型数据进行移位的例子如下:\n");
long c = rand.nextLong(); // 生成一个long型的随机数变量c
long d = rand.nextLong(); // 生成一个long型的随机数变量d
LongleftMove("-1L", -1L);
LongleftMove("+1L", +1L);
long Lconstant = 8454564564684456954L; // 定义一个长整型变量
LongleftMove("Lconstant", Lconstant);
long Lnegative = -8454564564684456954L; // 定义一个长整型变量
LongleftMove("Lnegative", Lnegative);
// 以下方法均调用LongleftMove方法
LongleftMove("c", c);
LongleftMove("~c", ~c);
LongleftMove("-c", -c);
LongleftMove("d", d);
LongleftMove("c & d", c & d);
LongleftMove("c d", c | d);
LongleftMove("c ^ d", c ^ d);
LongleftMove("c << 3", c << 3);
LongleftMove("c >> 3", c >> 3);
LongleftMove("(~c) >> 3", (~c) >> 3);
LongleftMove("c >>> 3", c >>> 3);
LongleftMove("(~c)>>> 3", (-c) >>> 3);
}
static void IntleftMove(String s, int i) { // 定义一个静态方法,带两个参数
System.out.println("int var="+s + "=" + i + ", 其二进制代码如下: ");
for (int j = 31; j >= 0; j--)
if (((1 << j) & i) != 0) { // 判断变量b左移一位与a变量做"与"操作,看是否不等于0
System.out.print("1"); //如果不等于0,则输出1
} else {
System.out.print("0"); //否则输出0
}
System.out.println("");
}
static void LongleftMove(String s, long l) { // 定义一个静态方法,方法中带两个参数
System.out.println("long var= "+s+"=" + l + ", 其二进制代码如下: ");
for (int i = 63; i >= 0; i--) {
if (((1L << i) & l) != 0) {
System.out.print("1");
} else {
System.out.print("0");
}
}
System.out.println("");
}
}