练习1:比较两个数据是否相等 参数类型分别为两个byte类型,两个int类型,两个short类型,两个long类型,并在main方法中测试。 练习2:模拟输出语句中的 println 方法效果,传递什么类型的数据就输出什么类型的数据,只允许定义一个方法名myPrint 。 // byte short int long float double char boolean // String // 在调用输出语句的时候,println方法其实就是进行了多种数据类型的重载形式。 鼠标放在println上方,按住ctrl键,点击鼠标左键查看源代码。
时间: 2024-03-25 08:38:35 浏览: 117
练习1:
```
public class CompareData {
public static void main(String[] args) {
byte b1 = 10, b2 = 10;
int i1 = 100, i2 = 200;
short s1 = 100, s2 = 100;
long l1 = 100000L, l2 = 200000L;
System.out.println(compare(b1, b2));
System.out.println(compare(i1, i2));
System.out.println(compare(s1, s2));
System.out.println(compare(l1, l2));
}
public static boolean compare(byte a, byte b) {
return a == b;
}
public static boolean compare(int a, int b) {
return a == b;
}
public static boolean compare(short a, short b) {
return a == b;
}
public static boolean compare(long a, long b) {
return a == b;
}
}
```
练习2:
```
public class MyPrint {
public static void main(String[] args) {
myPrint(10);
myPrint(10L);
myPrint(3.1415926);
myPrint("hello");
}
public static void myPrint(byte a) {
System.out.println(a);
}
public static void myPrint(short a) {
System.out.println(a);
}
public static void myPrint(int a) {
System.out.println(a);
}
public static void myPrint(long a) {
System.out.println(a);
}
public static void myPrint(float a) {
System.out.println(a);
}
public static void myPrint(double a) {
System.out.println(a);
}
public static void myPrint(char a) {
System.out.println(a);
}
public static void myPrint(boolean a) {
System.out.println(a);
}
public static void myPrint(String a) {
System.out.println(a);
}
}
```
阅读全文