Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common specification skills in Java development

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what are the common specification techniques used in Java development". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the common specification techniques used in Java development?"

1. Object's equals method is easy to sell short pointer exception.

From the source code to analyze the equals method belongs to the Object class, if the caller is null, then naturally a null pointer exception will be thrown at run time.

The source code in the object class:

From the source code to analyze the equals method belongs to the Object class, if the caller is null, then naturally a null pointer exception will be thrown at run time.

The source code in the object class:

Public boolean equals (Object obj) {

Return (this = = obj)

}

In order to avoid this situation, try to put constants or objects with definite values in front of them when comparing.

For example, say:

Correct: "test" .equals (object)

Error: object.equals ("test")

2. The naming of the class uses the specification of hump naming.

For example: UserService, with the exception of the following scenario: DO / BO / PO / DTO / VO.

For example, say: UserPO,StudentPO (PO,VO,DTO, such nouns need to be fully capitalized)

@ Data

@ Builder

Public class CustomBodyDTO {

Private String name

Private String idCode

Private String status

}

If design patterns are used in modules or interfaces, classes, or methods, please reflect them when naming them. For example: TokenFactory,LoginProxy and so on.

Public class TokenFactory {

Public TokenDTO buildToken (LoginInfo loginInfo) {

String token = UUID.randomUUID () .toString ()

TokenDTO tokenDTO = TokenDTO.builder ()

.token (token)

.createTime (LocalDateTime.now ())

.build ()

String redisKey = RedisKeyBuilder.buildTokenKey (token)

RedisService.setObject (redisKey, loginInfo, Timeout.ONE_DAY * 30 * 2)

Log.info ("token created successfully | loginInfo= {}", loginInfo.toString ())

Return tokenDTO

}

}

4. Equal is used to operate when comparing all wrapper classes of the same type.

For the Integer class, when the corresponding variable values range from-128to 127, the object is stored in IntegerCache.cache, so object reuse occurs.

Therefore, when comparing wrapper classes, it is best to use the equal method uniformly.

Private static class IntegerCache {

Static final int low =-128,

Static final int high

Static final Integer cache []

Static {

/ / high value may be configured by property

Int h = 127h

String integerCacheHighPropValue =

Sun.misc.VM.getSavedProperty ("java.lang.Integer.IntegerCache.high")

If (integerCacheHighPropValue! = null) {

Try {

Int I = parseInt (integerCacheHighPropValue)

I = Math.max (I, 127)

/ / Maximum array size is Integer.MAX_VALUE

H = Math.min (I, Integer.MAX_VALUE-(- low)-1)

} catch (NumberFormatException nfe) {

/ / If the property cannot be parsed into an int, ignore it.

}

}

High = h

Cache = new Integer [(high-low) + 1]

Int j = low

For (int k = 0; k

< cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >

= 127

}

Private IntegerCache () {}

}

Public static Integer valueOf (int I) {

If (I > = IntegerCache.low & & I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report