| |

VerySource

 Forgot password?
 Register
Search
View: 764|Reply: 8

Confusion about new Integer () and new int [3]? ?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-20 03:20:01
| Show all posts |Read mode
1. I wrote a class myself, the code is as follows:
public class Test {
private final int i;
public Test (int i) {
// TODO Auto-generated constructor stub
this.i = i;
}

/ **
* @param args
* /
public static void main (String [] args) {
// TODO Auto-generated method stub
System.out.println (new Test (123));
System.out.println (new Integer (123));

}

}
Why System.out.println (new Test (123)); The result is Test @ 35ce36
The result of System.out.println (new Integer (123)); is 123? . . .

2.int [] j = new int [3]; is int [] a class? Why is new int [3] possible?


Thank you for your guidance. . . . . .
Reply

Use magic Report

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-28 11:27:02
| Show all posts
Personal understanding. The general custom class, if output directly, will output the memory address of this object similar to Test @ 35ce36. And these wrapper classes will output the value it represents, I have not thought deeply
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-29 13:36:02
| Show all posts
You did not override the toString () method, System.out.println (par) will call the toString () method of par.
public class Test {
private final int i;
public Test (int i) {
// TODO Auto-generated constructor stub
this.i = i;
}
public String toString () {
StringBuffer tmp = new StringBuffer ();
tmp.append (i);
return tmp.toString ();
}
/ **
* @param args
* /
public static void main (String [] args) {
// TODO Auto-generated method stub
System.out.println (new Test (123));
System.out.println (new Integer (123));

}

}

This will come out,
Ha ha!
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-29 15:00:02
| Show all posts
int [] j = new int [3]; is an array of integers that can be put into 3 integers.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-29 19:45:02
| Show all posts
catmay, I know that new int [3] is an array of integers. I want to know, is int [] a class? Why else can it be new?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-29 20:27:01
| Show all posts
1 System.out.println (new Test (123)); In the new test (123) you are printing a class, it will call the "default" toString method, if you want to print the desired result, you need to override toString method.

2 int [] j = new int [3]; indicates that an int array j with 3 elements is defined

Ha ha, I have n’t looked at these basics for a long time, I may have forgotten it, so I understand it
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-30 12:45:01
| Show all posts
Brothers may have misunderstood what I mean. I know that int [] j = new int [3]; indicates that an int-type array j with 3 elements is defined. What I want to ask is: only when int [] is a class When you can create an object of it? But is int [] an object? Why can't I see its source code? confused. . . . . . .
Reply

Use magic Report

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-2-13 15:30:01
| Show all posts
I think that's just one way to initialize the array
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-16 19:15:01
| Show all posts
All arrays are a class. You can use the isArray method of the Class class to determine whether an object is an array.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list