1
1
123qwe2016-02-02 15:04:19
Java
123qwe, 2016-02-02 15:04:19

What is the difference between creating an object in Java like this, or like this?

What is up, programmer.

StackOfStrings s;
s = new StackOfStrings(100);

or
StackOfStrings s = new StackOfStrings(100);
I missed this moment, I remember how I met it in a book. And now I often see it, but I don’t catch the difference.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil Antoshkin, 2016-02-02
@Yonghwa

First case:
You declare a StackOfStrings variable named s, but don't initialize it
And only then do it on the second line
Second case:
You declare a StackOfStrings variable named s and initialize it right away
Essentially there is no difference, mostly it's the code design

E
Eugene, 2016-02-03
@Onni

StackOfStrings s;
System.out.print(s);
s = new StackOfStrings(100);

The only difference is that the variable s cannot be used on the right side of the expression until at least something is assigned to it. The code above won't even compile. There are no differences that would affect the work of the program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question