Underwater acoustic target recognition (UATR) technology based on deep learning and automatic encoding has become an important research direction in the underwater acoustic field in recent years. However, the existing methods do not have favourable self‐adaptability for different data because of the complex and changeable underwater environment, which easily leads to an unsatisfactory recognition effect. The concept of contrastive learning is introduced into UATR and a model named Contrastive Coding for UATR (CCU) is proposed. Based on the unsupervised contrastive learning framework, the model has been modified for the underwater acoustic field. Thus, the CCU can generate adaptable automatic features according to different data. The experimental test shows that the model is superior to other automatic encoding models and has achieved excellent recognition performance on different underwater acoustic datasets.