简体   繁体   中英

Java : the method to instantiate an object

While I'm reading the spring tutorial, I found something like this:

LocalChangeInterceptor localChangeInterceptor;
localChangeInterceptor = new LocalChangeInterceptor();

So what is the difference with this manner?

LocalChangeInterceptor localChangeInterceptor = new LocalChangeInterceptor();

Also, I want to know why people prefer

if (String.class.equals(sourceType.getType()))    

to

if (sourceType.getType().equals(String.class))

In this:

LocalChangeInterceptor localChangeInterceptor;
localChangeInterceptor = new LocalChangeInterceptor();

what you do is you first create a reference to a LocalChangeInterceptor and then create a new object in the second line and pass its reference to your variable.

In the second one you do the same but in one step. You create the reference and immediately pass a value to it.

LocalChangeInterceptor localChangeInterceptor = new LocalChangeInterceptor();

This: if (String.class.equals(sourceType.getType())) is the same with: if (sourceType.getType().equals(String.class))

Also I want to give you one tip. Stackoverflow is not the right place to teach yourself programming. This question for example. You could have answer it on your own if you searched a little more. I strongly recommend that you put some more effort into this. Teaching yourself programming is not an easy task.

Good luck though.

  1. LocalChangeInterceptor localChangeInterceptor = new LocalChangeInterceptor();

    is effectively the same as

     LocalChangeInterceptor localChangeInterceptor; localChangeInterceptor = new LocalChangeInterceptor(); 

    But, you sometimes have to split the assignment to extend the scope of a reference . For example, while opening a database Connection you would split the declaration so that the connection reference is available in the finally block.

     Connection connection; try { connection = DriverManager.getConnection(...); // Do statement stuff that may throw exceptions } catch (SQLException e) { // Handle exception } finally { // Close connection if (connection != null) { // Available inside finally, connection.close(); // because declared outside the try block } } 
  2. With

     if (String.class.equals(sourceType.getType())) 

    you avoid a NullPointerExcpetion if getType() returns null .

    That's because when the you flip the code to

     if (sourceType.getType().equals(String.class)) { 

    at runtime it tries to execute

     if (**null**.equals(String.class)) { // NPE! 

The previous anwswer from @TheCrafter answers part one of your question.

However, why people prefer

if (String.class.equals(sourceType.getType()))    

to

if (sourceType.getType().equals(String.class))

if because the second variant can throw NullPointerException.

It is better to call the equals method on a known constant value that can never be null . String.class fulfills that.

For your first question, the only difference is that the variable is initialized after it's declared . This is useful in some cases, where a variable won't be used for a while, thus not needing to contain any data until used. Look into lazy initialization.

For the second one, the idea is to prevent NullPointerException . If sourceType.getType() returned null , your second example would throw a NPE, since we are trying to call equals from a variable which references null . But in the first example, we are calling equals off something we KNOW won't be null. If sourceType.getType() was null in the first example, equals would simply return false instead of a NPE being thrown. Passing null into the equals method will return false

Because sourceType.getType() might return null and String.class always return class java.lang.String .
So ? Whats wrong with null ?
Well, if you translate it after the result returned the null value, you can read the following code
if (sourceType.getType().equals(String.class))
become like this
if (null.equals(String.class))
and your code will ended have NullPointerException error.

There is no functional difference between these two statements.

LocalChangeInterceptor localChangeInterceptor = new LocalChangeInterceptor();

LocalChangeInterceptor localChangeInterceptor;
localChangeInterceptor = new LocalChangeInterceptor();

Choosing between them is primarily stylistic, and usually only makes a difference if you are trying to prevent overly long lines.


There is a subtle, but important, difference between the different equality checks.

String.class.equals(sourceType.getType())`

`sourceType.getType().equals(String.class)`

Behave very differently in one important case, but (as TheCrafter mentioned) you should be able to figure out on your own with a little bit of effort.

If you get stuck, consider the possible values of sourceType , and how that could affect the behavior of the two versions of that statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM