Hi, I am pretty new to Java. I have some basic questions.

public class Array {
   
   private Object[] values;
   private int count;
   private boolean sorted;

public Array (int max) {

   this.values = new Object[max];
   this.count = 0;
   this.sorted = true;

I am kinda confused at line 9. It looks like declaring an instance/instantiate because of the keyword new, but from what I have seen, we usually create an object in the driver/main class; we don't create it in the constructor. So is it doable? and why?

2. What is Stream IO? I know it is like input and output, but what exactly is it? What's the difference between IO.stdout.print and System.out.print?

public final class IO {
 public static PrintStream stderr = System.err;
....}

What is stderr? I looked it up but the definition is too complicated to understand.

Thanks!

I am kinda confused at line 9. It looks like declaring an instance/instantiate because of the keyword new, but from what I have seen, we usually create an object in the driver/main class; we don't create it in the constructor. So is it doable? and why?

At line 9, you are creating a new values object which is contained in the Array object you instantiated at the static main method. So yes, you are correct; normally you do create an object in the main method. However, the beauty of OOP is that objects can contain other objects, as seen at the constructor. Also, you can create objects outside the constructor.

For instance, you can change the code like this:

public class Array {
   
   private Object[] values;
   private int count = 0;
   private boolean sorted = true;

public Array (int max) {
   this.sorted = true;
}

PrintStream is used to write data to a file or other source. System.out is for the compiler console (e.g. testing environment).

So while you may use: System.out.println("test") to print to the console, you can use file.println("test") to write to a file (assuming you initialized it correctly).

"stderr" is the name of the PrintStream object that was initialized. Its name is arbitrary.

At line 9, you are creating a new values object which is contained in the Array object you instantiated at the static main method. So yes, you are correct; normally you do create an object in the main method. However, the beauty of OOP is that objects can contain other objects, as seen at the constructor. Also, you can create objects outside the constructor.

For instance, you can change the code like this:

public class Array {
   
   private Object[] values;
   private int count = 0;
   private boolean sorted = true;

public Array (int max) {
   this.sorted = true;
}

Thanks! It makes more sense now. So you are saying that Array class should have the object Array[] a = new Array[5], for instance (Actually, can you instantiate the Array object within the constructor?); but this object has another object called values? and it is also an array?? But I don't quite get your code. How does it prove the point that you can create object outside the constructor?

oops, I left the values part out.

public class Array {
   
   private Object[] values;
   private int count = 0;
   private boolean sorted = true;

   public Array (int max) {
      values = new Object[5];
   }
}

You can call values how I have it above, or by 'this.values'. Also, you generally do not want to create Object arrays. This should be something less generalized like int[] or String[] or even some class you create like Employee[].

If not, I could add all of the following to the same values array:

values[0] = 3.14;
values[1] = "3.14";
values[2] = "cherry";

How does it prove the point that you can create object outside the constructor?

consider this:

private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

String is an object; This is instantiated outside the constructor, but bound to the constructor object.

Consider a Sentence class with the above ALPHABET String.

Sentence s1 = new Sentence();
Sentence s2 = new Sentence();

In memory, s1's ALPHABET object is not the same location as s2's. They each have a spot allocated in memory for it. They also do not exist without their parent object. However, I can invoke method calls from the String class on ALPHABET, such as: char[] myArray = ALPHABET.toCharArry();

Hope that helps!

You can call values how I have it above, or by 'this.values'. Also, you generally do not want to create Object arrays. This should be something less generalized like int[] or String[] or even some class you create like Employee[].

Thanks again! Not sure if I understand you correctly. Was this your answer for my question about:" Array class should have the object Array[] a = new Array[5], for instance (Actually, can you instantiate the Array object within the constructor?); but this object has another object called values? and it is also an array??" What do you mean by creating Object arrays? do you mean create an object for the Array class or....? (since you capitalize the 'O'). Because the class here named Array, and that's why I thought the object for this class should be Array[] a = new Array..But I don't understand the whole point an object containing another object!

consider this:

private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

String is an object; This is instantiated outside the constructor, but bound to the constructor object.

Consider a Sentence class with the above ALPHABET String.

Sentence s1 = new Sentence();
Sentence s2 = new Sentence();

In memory, s1's ALPHABET object is not the same location as s2's. They each have a spot allocated in memory for it. They also do not exist without their parent object. However, I can invoke method calls from the String class on ALPHABET, such as: char[] myArray = ALPHABET.toCharArry();

Hope that helps!

So you are saying that this is another case of an object containing another object? Object s1 contains th string? And the string will not exist without s1 and s2?

Thank you so much!


PrintStream is used to write data to a file or other source. System.out is for the compiler console (e.g. testing environment).

So while you may use: System.out.println("test") to print to the console, you can use file.println("test") to write to a file (assuming you initialized it correctly).

"stderr" is the name of the PrintStream object that was initialized. Its name is arbitrary.

Regarding your comment, my professor wrote the following code:

public static PrintStream stdout = System.out;
public static PrintStream stderr =System.err;

As you were saying, PrintStream is to print the output to a file, and System.out is to the console. So what does line 1 mean here? and What is System.err? Thanks!

This article has been dead for over six months. Start a new discussion instead.