📜  Java程序的输出|设置 8

📅  最后修改于: 2022-05-13 01:55:30.770000             🧑  作者: Mango

Java程序的输出|设置 8

难度级别:中级
预测以下Java程序的输出。
方案一:

Java
class GfG
{
    public static void main(String args[])
    {
        String s1 = new String("geeksforgeeks");
        String s2 = new String("geeksforgeeks");
        if (s1 == s2)
            System.out.println("Equal");
        else
            System.out.println("Not equal");
    }
}


Java
class Person
{
    private void who()
    {
        System.out.println("Inside private method Person(who)");
    }
  
    public static void whoAmI()
    {
        System.out.println("Inside static method, Person(whoAmI)");
    }
  
    public void whoAreYou()
    {
        who();
        System.out.println("Inside virtual method, Person(whoAreYou)");
    }
}
 
class Kid extends Person
{
    private void who()
    {
        System.out.println("Kid(who)");
    }
  
    public static void whoAmI()
    {
        System.out.println("Kid(whoAmI)");
    }
  
    public void whoAreYou()
    {
        who();
        System.out.println("Kid(whoAreYou)");
    }
}
public class Gfg
{
    public static void main(String args[])
    {
        Person p = new Kid(); 
        p.whoAmI();
        p.whoAreYou();
    }
}


Java
class GfG
{
    public static void main(String args[])
    {
        try
        {
            System.out.println("First statement of try block");
            int num=45/3;
            System.out.println(num);
        }
        catch(Exception e)
        {
            System.out.println("Gfg caught Exception");
        }
        finally
        {
            System.out.println("finally block");
        }
        System.out.println("Main method");
    }
}


Java
class One implements Runnable
{
    public void run()
    {
        System.out.print(Thread.currentThread().getName());
    }
}
class Two implements Runnable
{
    public void run()
    {
        new One().run();
        new Thread(new One(),"gfg2").run();
        new Thread(new One(),"gfg3").start();
    }
}
class Three
{
    public static void main (String[] args)
    {
        new Thread(new Two(),"gfg1").start();
    }
}


输出:

Not equal

说明:由于 s1 和 s2 是两个不同的对象,所以引用不一样,==运算符比较对象引用。所以它打印“不等于”,为了比较字符串中的实际字符,必须使用 .equals() 方法。方案二:

Java

class Person
{
    private void who()
    {
        System.out.println("Inside private method Person(who)");
    }
  
    public static void whoAmI()
    {
        System.out.println("Inside static method, Person(whoAmI)");
    }
  
    public void whoAreYou()
    {
        who();
        System.out.println("Inside virtual method, Person(whoAreYou)");
    }
}
 
class Kid extends Person
{
    private void who()
    {
        System.out.println("Kid(who)");
    }
  
    public static void whoAmI()
    {
        System.out.println("Kid(whoAmI)");
    }
  
    public void whoAreYou()
    {
        who();
        System.out.println("Kid(whoAreYou)");
    }
}
public class Gfg
{
    public static void main(String args[])
    {
        Person p = new Kid(); 
        p.whoAmI();
        p.whoAreYou();
    }
}

输出:

Inside static method, Person(whoAmI)
Kid(who)
Kid(whoAreYou)

说明:静态方法发生静态绑定(或编译时)。这里p.whoAmI()调用静态方法,因此它在编译时被调用,因此导致静态绑定并在 Person 类中打印该方法。
p.whoAreYou()调用Kid类中的方法,因为默认情况下Java将其作为虚拟方法,即动态绑定。方案 3:

Java

class GfG
{
    public static void main(String args[])
    {
        try
        {
            System.out.println("First statement of try block");
            int num=45/3;
            System.out.println(num);
        }
        catch(Exception e)
        {
            System.out.println("Gfg caught Exception");
        }
        finally
        {
            System.out.println("finally block");
        }
        System.out.println("Main method");
    }
}

输出:

First statement of try block
15
finally block
Main method

解释:
由于没有异常,因此不会调用 catch 块,但无论是否处理异常,都会在 try 块之后执行finally块。程序 4:

Java

class One implements Runnable
{
    public void run()
    {
        System.out.print(Thread.currentThread().getName());
    }
}
class Two implements Runnable
{
    public void run()
    {
        new One().run();
        new Thread(new One(),"gfg2").run();
        new Thread(new One(),"gfg3").start();
    }
}
class Three
{
    public static void main (String[] args)
    {
        new Thread(new Two(),"gfg1").start();
    }
}

输出:

gfg1gfg1gfg3