“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Difficulty level: medium

Predict the output of the following Java program:

# The problem

## Problem a

``````class Gfg
{
// constructor
Gfg()
{
System.out.println("juejin");
}

static Gfg a = new Gfg(); //line 8

public static void main(String args[])
{
Gfg b; //line 12
b = newGfg(); }}Copy the code``````

## Question 2

``````class Gfg
{
static int num;
static String mystr;

// constructor
Gfg()
{
num = 100;
mystr = "Constructor";
}

// First Static block
static
{
System.out.println("Static Block 1");
num = 68;
mystr = "Block1";
}

// Second static block
static
{
System.out.println("Static Block 2");
num = 98;
mystr = "Block2";
}

public static void main(String args[])
{
Gfg a = new Gfg();
System.out.println("Value of num = " + a.num);
System.out.println("Value of mystr = "+ a.mystr); }}Copy the code``````

## Question 3

``````class superClass
{
final public int calc(int a, int b)
{
return 0; }}class subClass extends superClass
{
public int calc(int a, int b)
{
return 1; }}public class Gfg
{
public static void main(String args[])
{
subClass get = new subClass();
System.out.println("x = " + get.calc(0.1)); }}Copy the code``````

## Problem four

``````public class Gfg
{
public static void main(String[] args)
{
Integer a = 128, b = 128;
System.out.println(a == b);

Integer c = 100, d = 100; System.out.println(c == d); }}Copy the code``````

Put a picture of a cute girl to relieve eye fatigue, the second half of the article is the output and analysis of the program

# Output and parsing

Output:

``````juejin
juejin
Copy the code``````

Explanation:

We know that static variables are called when the class loads, and static variables are only called once. Line 13 now causes the creation of the object, which in turn calls the constructor, printing “juejin” a second time. If static variables are not used in line 8, the object is called recursively indefinitely, resulting in a StackOverFlow error. See this example run.

Output:

``````Static Block 1
Static Block 2
Value of num = 100
Value of mystr = Constructor
Copy the code``````

Description:

Static blocks are executed when the class is loaded into memory. A class can have multiple static blocks that are executed in the same order in which they were written to the program.

Note: Static methods can access class variables without using the object of the class. Since the constructor is called when a new instance is created, the static block is called first, followed by the constructor. If we run the same program without using an object, the constructor is not called.

Output:

``````Compilation fails
Copy the code``````

Explanation:

The calc() method in the superClass class is final and therefore cannot be overridden.

Output:

``````false
true
Copy the code``````

Description:

Note: In the source code for the Integer object, we find a method ‘valueOf’. In this method, we can see that the Integer object ranges from integerCache.low (-128) to integerCache.high (127). Therefore, numbers above 127 do not give the expected output. The scope of IntegerCache can be seen in the IntegerCache class source code.

## Related articles:

Java program Java exercises 】 【 | at the output of the first set of (analysis) the output of the Java program Java exercises 】 【 | second (analysis) the output of a Java program | Java exercises 】 【 a third (parsing) the output of a Java program | Java exercises 】 【 4 sets (including parsing) The output of a Java program | Java exercises 】 【 5 sets (including parsing) the output of the Java program Java exercises 】 【 | 6 sets (including parsing) the output of a Java program | Java exercises 】 【 7 sets (including parsing) the output of a Java program | Java exercises 】 【 8 sets (including parsing)

I have been writing a tech blog for a long time and this is one of my tech articles/tutorials. Hope you like it! Here is a summary of all my original and work source code: GitHub, Gitee

If you do learn something new from this post, like it, bookmark it and share it with your friends. 🤗 Finally, don’t forget ❤ or 📑 for support