public static void main(String[] args) { int i=mathod("1",1); System.out.println(i); } public static int mathod(String s,int t) { System.out.println("函数1的输出语句"); return t; }
时间: 2024-01-27 20:03:18 浏览: 22
在这段代码中,`main` 函数调用了 `mathod` 函数并将其返回值赋值给 `i`,然后将 `i` 打印到控制台。`mathod` 函数接受一个字符串 `s` 和一个整数 `t`,并在函数体内打印一行文本并返回 `t`。因此,这段代码的输出结果是:
```
函数1的输出语句
1
```
其中第一行是 `mathod` 函数的输出,第二行是 `main` 函数打印的变量 `i` 的值。
相关问题
public class VariableDemo { int a = 111; final int b = 222; public void display() { System.out.println("执行display()"); int a = 333; System.out.println(a); System.out.println(this.a); System.out.println(b); System.out.println(this.b); } public static void main(String[] args) { // TODO Auto-generated method stub int a = 444; System.out.println("执行main()"); System.out.println(a); //System.out.println(this.a); {// 代码块1 int c = 666; System.out.println(c); } System.out.println(c); {// 代码块2 int d = 666; System.out.println(d); } int d = 888; System.out.println(d); VariableDemo vd = new VariableDemo(); vd.display(); vd.b = 999; System.out.println(vd.b); } }
该代码存在以下问题:
1. 在`main`方法中,不能直接访问非静态变量`a`和`b`,因为它们是属于对象的,必须先创建一个类的实例才能访问。
2. `final`修饰的变量`b`不能被修改,所以在`vd.b = 999;`中会编译错误。
3. 在代码块1中定义的变量`c`只能在代码块内部访问,超出代码块范围后就不能再访问了。在`System.out.println(c);`处会编译错误。
4. 变量名`d`在代码块2和`main`方法中都出现了,这是不合法的,因为在同一级作用域中变量名不能重复。
修正后的代码如下:
```
public class VariableDemo {
int a = 111;
final int b = 222;
public void display() {
System.out.println("执行display()");
int a = 333;
System.out.println(a);
System.out.println(this.a);
System.out.println(b);
System.out.println(this.b);
}
public static void main(String[] args) {
int a = 444;
System.out.println("执行main()");
System.out.println(a);
{
int c = 666;
System.out.println(c);
}
// System.out.println(c); // 编译错误
{
int d = 666;
System.out.println(d);
}
int d = 888;
System.out.println(d);
VariableDemo vd = new VariableDemo();
vd.display();
// vd.b = 999; // 编译错误
System.out.println(vd.b);
}
}
```
修正后的代码中,将`vd.b = 999;`注释掉,因为`b`是用`final`修饰的,不能被修改。同时,变量名重复的问题也得到了修正。
public class Test { public static void main(String[] args) { int i = 0: Integer j = new Integer(0) ; System.out.println(i==j); System.out.println(j.equals(i));
这段代码存在语法错误,不能编译通过。其中第二行中冒号应该改为分号,变量类型应该为 `Integer` 而非 `int`。修改后的代码如下:
```
public class Test {
public static void main(String[] args) {
int i = 0;
Integer j = new Integer(0);
System.out.println(i == j);
System.out.println(j.equals(i));
}
}
```
这段代码首先定义了一个 `int` 类型的变量 `i`,并将其赋值为 `0`;然后定义了一个 `Integer` 类型的变量 `j`,并将其赋值为 `0` 的包装类型 `new Integer(0)`。接下来分别输出了 `i == j` 和 `j.equals(i)` 的结果。
由于 `i` 和 `j` 数据类型不同,因此在比较相等性时会进行类型转换。在 `i == j` 中,`j` 会被自动拆箱,得到 `0`,然后和 `i` 进行比较,因此结果为 `true`。在 `j.equals(i)` 中,由于 `i` 是一个 `int` 类型的基本数据类型,因此会自动装箱为 `Integer` 类型,然后进行比较,因此结果也为 `true`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)