2024-03-30 00:46:37 | 我爱编程网
public static void main(String args[]){
String s1="Java";
int n=s1.length(); //得到s1这个字符串变量的长度
System.out.print("n="+n);
char m=s1.charAt(2); //得到s1变量中第三个字符
System.out.println(" s1="+s1);
}//n=4 s1=Java
2.以下程序的输出结果为 123 。
class Test{
static int num; //定义一个静态变量
int c;
public Test( ){
num++;}
public static void main(String[ ] args){
Test x1=new Test();
System.out.print(x1.num);
Test x2=new Test();
System.out.print(x2.num);
Test x3=new Test();
System.out.println(x3.num);//获得x3对象中num属性的值 3
}}
3、以下程序的输出结果为 Inside main() Mug(1) c1 initialized Mugs() 。
class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")"); }
void f(int marker) {
System.out.println("f(" + marker + ")"); }}
public class Mugs {
Mug c1;
{ c1 = new Mug(1); //实例化对象c1
System.out.println("c1 initialized"); }
Mugs() { System.out.println("Mugs()"); }
public static void main(String[] args) {
System.out.println("Inside main()");我爱编程网
Mugs x = new Mugs();//实例化x
}}
4、以下程序的输出结果为 AB 。
class A{
public A(){
System.out.print(“ A ”);
}}
public class B extends A{//类B继承类A
public B(){
System.out.print(“ B ”);
}
public static void main(String[ ] args){
new B( );//初始化一个类B的临时变量
}}
.public class AboutThreeNum
{public static void main(String[ ] args)
{ System.out.println( fun(-8, 50, 23) ); }
static int fun(int x, int y, int z) //静态函数fun,属于这个类,而不是单个对象
{ return fun( x, fun(y,z) ); }
static int fun(int x,int y)
{ if(x>y) return x; //当x>y时,返回x的值
else return y; }}
结果 50
public class Mystery {
/**
* @param String[] args 接收输入参数 args[0] args[1]
* @return void
*/
public static void main(String[] args) {
Changer c = new Changer();
c.method(args);
System.out.println(args[0] + " " + args[1]);
}
static class Changer {
/**
* 交换字符数组位置0和1的值
* 在java中,参数传递都是值传递
* 所以这里变量s和args的本身地址是不一样的
* 但是他们所指向的输入参数字符数组地址是一样的
*
* @param String[] s 要交换的字符数组
*/
void method(String[] s) {
String temp = s[0];
s[0] = s[1];
s[1] = temp;
}
}
}
输入:Mighty Mouse
存放的结构为:
参数对象:String[] {"Mighty", "Mouse"} 暂且命名为 args_obj(这个在java内部就是一地址)
变量args指向这个地址:args->args_obj
再调用method时,变量s指向args指向的地址:s->args_obj
故结果为:
[答案] 打印:Mouse Mighty
public class Pass{
/**
* 定义静态变量
* 意思就是说定义后,所有对j的修改都会保留下来
*/
static int j=20;
public static void main(String argv[]){
/**
* i 为局部变量,在方法内部调用后,外部值不发生变化
*/
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
/**
* 由于java传递参数是复制传递(值传递),故内部的x和外部的i是两个不同的变量
* 内部x的修改不会影响到外部的i变量
*/
public void amethod(int x){
x=x*2;
j=j*2;
}
}
[答案] 打印:10 40
--、++运算不会立即改变变量值,
所以x--==2是true,y++==2也为true;
所以x--==2&&y++==2 为true;
此后x变为1,y变为3;
if条件语句的或 “||”,a||b,若a=true,则无需判断b,所以z++==2其实是没有执行的,所以z = 2;
最后一行
System.out.println("x="+x+",y="+y+",z="+z);
括号里的“”只是代表里面的是字符串“x=”输出后就是一个字符串 x=而已
+号是衔接字,"x="+x+",y="+y+",z="+z,可以看到括号里包含3个字符串:
"x=" 、",y="、",z=" ,每个字符串后面接(+)了一个变量的值,最终拼凑成答案3的结果。
2024-03-02 02:58:47
2024-01-01 09:58:48
2024-03-21 00:22:13
2024-01-26 10:37:53
2024-03-04 07:43:42
2024-01-17 23:29:33