Java problems

Asked 2 years ago, Updated 2 years ago, 21 views

Find all the errors and correct them.

1) Calendar d = new Calendar();

2) int i = new Integer(0);

3) Integer i = new Integer("100");

4) int i = Integer.parseInt("100");

5) String s = new String("abc");

That's the question. There was no answer, so I posted it (Crying) 1)Except for the number, I ran the program, so the price came out. But I think there's a problem with number three, too.(Crying)

Can anyone kindly explain which part is the error and why?

java

2022-09-22 17:58

2 Answers

Calendar d = new Calendar(); // Cannot instantiate the type Calendar

The java.util.Calendar class has two constructors (based on JDK10). Both declared protected access controllers. https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html#%3Cinit%3E()

protected is accessible only to classes in the same package or extended (=inherited) the Calendar class. Because of this, a compilation error occurs, so it's called an error.

Usually, you use java.util.GregorianCalendar to create an instance of the type Calendar:

import java.util.GregorianCalendar;

// Omitted

Calendar a = new GregorianCalendar();

By the way, what do you think is the problem with number three?


2022-09-22 17:58

1) Calendar d = new Calendar();

2) int i = new Integer(0);

3) Integer i = new Integer("100");

4) int i = Integer.parseInt("100");

5) String s = new String("abc");

All of the above items seem to need explanation.

Of course, the "error" is number one.

Calendar class number 1 is as follows.

public abstract class Calendar
extends Object
implements Serializable, Cloneable, Comparable<Calendar>

It's an abstract class. Naturally, you cannot create an object with the new operator.

It's a glaring error.

In case number 2, new Integer (0) is created on the heap, then unboxed and put into the stack. From jdk 1.5, it becomes auto-boxing and unboxing, so it is not an error but an inefficient form.

Just inti = 0.

In case number three, there's one thing to think about. Java is not an object of type. Representative ones, not objects, are the primitive types int, char, long, byte. It is not an object, so it is stored in the stack, not in heap.

Then can't I write int and char like an object? You can use the Integer class at that time. When new Integer(), it is stored in heap and is the target of garbage collection. int is stored in the stack and is not the target of garbage collection by default. Auto-processing occurs when the method call is completed.

In the case of number 4, we change the object stored in the heap to the primitive type (unboxing) and put it in the stack. It's just an inefficient form, but it's often necessary. For example, let's say you put ints in an ArrayList.

ArrayList can contain only objects. You can't put int. Then, new Integer (1) is object-type and contained in this way. Naturally, if you get a value from ArrayList, it is an object and you need to convert it to int through unboxing.

In the case of number 5, you need to know the concept of jvm constants pool and the concept of inter. Simply put,

String a = "abcd";
String b = "abcd";

String A = new String("abcd");
String B = new String("abcd");

These two a and b are the same, but A and B are different.

This is possible because the string is a constant and there is a separate space in which only the string is stored. However, new String is stored in heap, which is unnecessary waste.


2022-09-22 17:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.