Tag Archives: java

On the intern () method of string class in Java

String s1 = new StringBuilder(” Go “).append(” od “).toString();
System.out.println(s1.intern() == s1); String s2 = new
StringBuilder(” ja “).append(” va “).toString(); String s2 = new
StringBuilder(” ja “).append(” va “).toString();
System.out.println(s2.intern() == s2); Output is true or false
in the search on the net, have explained string constants in the pool a Java this string constants, and so the example below would be false.

String sb =new StringBuilder(” Go “).append(” OD “).toString();
String sb =new StringBuilder(” Go “).append(” OD “).toString();
String s1 = new String(” Good “);
System.out.println(sb.intern()==sb);
returns false!!
originally used in intern method, will first judgment in the constant pool have the corresponding string, have put the string, not words

The intern() implementation in JDK 1.7 does not copy the instance any more, it just records the instance reference that first appears in the constant pool, so the reference returned by the intern() is the same as the instance of the string created by the StringBuilder

Access characteristics of construction methods in Java inheritance

Access characteristics of constructors in Java inheritance
The

    subclass constructor has a default implicit “super()” call, so it must be the parent constructor called first and the subclass constructor executed later. Subclass constructs can be invoked with the super keyword to call superclass overloaded constructs. Super’s superclass constructor call must be the first statement of the subclass constructor. A subclass construct cannot call the super construct more than once.

Conclusion:
subclasses must call the superclass constructor, don’t write gives super (); You can only have one super, and it has to be the first one.

class.getResource () and ClassLoader.getResource How to fill in the parameters in ()

Class.getResource(String Name) : Find the resource under the same path as the current calling Class
For example, my current Class below the impl package, then the Class. The getResource () is located in path:/D:/learnSpringboot/basicJava/out/production/basicJava impl /
1. If the query resources are the same as the current classpath, they are all under the impl package
Class.getResource(” XXX. XXX “)
Class.getResource(” /impl/ xxx.xxx “)
ClassLoader.getResource(String Name) : Find the resource file in the root directory, i.e. “/” or the classpath directory
2. If the query resource is not at the same level as the current class
For example, my current class below the impl package, so this. GetResource () is located in path:/D:/learnSpringboot/basicJava/out/production/basicJava /,
I have to use the relative path notation
This getResource (” XXX/XXX. XXX “)
 
Reference blog: https://blog.csdn.net/qq_33591903/article/details/91444342
https://blog.csdn.net/zhangshk_/article/details/82704010

Design and implementation of music website based on Java

The graduation design adopts the current popular framework Spring Boot, thus simplifying the operation of environment construction. For students who just finished learning Spring Boot framework, the current system has a certain reference value. Cool page, suitable for young friends.
The main functions include list ranking, online playing, etc., but some of the functions have not been implemented. Students who are interested in this can write relevant codes by themselves, which will help improve their technical level.
The source code

http://www.byamd.xyz/dljd.html

Mutual conversion between date and localdatetime

Sometimes you want to change the time of localDateTime, but Calendar can only change the information of date. You need to convert date to localDateTime or change localDateTime to date.

//date->localDateTime
Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()
//localDateTime->date
Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

Idea Maven project uses package to package and report error (package does not exist)

Scene: I pass File -> Project Structure -> Modules and Libraries have added an external Jar that runs normally in the project, but the following error occurred when packaging with Maven’s package

Solution: Configure the Jar to a pom.xml file and you can package it properly
Steps:
1. Open the Terminal execution (Terminal window at the bottom of the IDEA interface)

mvn install:install-file -DgroupId=com.external -DartifactId=http-sdk -Dversion=1.2.6-SNAPSHOT -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\BOOT-INF\lib\http.jar 

2. Add the following dependencies to the pom.xml file

<dependency>
    <groupId>com.souche</groupId>
    <artifactId>msgcenter-sdk</artifactId>
    <version>1.2.6-SNAPSHOT</version>
</dependency>

The -dgrouPID corresponds to the groupid-dartifactid in the POM and the artifactitid-Dversion corresponds to the version-dpackaging import package which is a TYPE of JAR and that’s the jar-DFile where you put the JAR that you downloaded from the POM

Once you run the command, you can see the corresponding JAR in the local Maven repository

 

And then re-execute
Compile and package MVN Clean Package (install)
Or choose the right side of the IDEA interface, select the Maven status bar and click package packaging

 

 

The required configuration in the POM.xml file

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

 

 

Java compareto() method

The compareTo() method is used to compare the two approaches:
String is compared to an object.
compares two strings in dictionary order.
syntax
int compareTo(Object o)
or
Int compareTo(String anotherString)
parameter
o – object to compare.
AnotherString – The string to compare.
Return value
the return value is the integer, it is first to compare the size of the corresponding character (ASCII), if the first character and the parameters of the first character, end of comparison, return the difference between them, if the first character is equal to the parameters of the first character, with the second character and parameter of the second character, and so on, until the comparison of character or character one end are being compared.
If the parameter string is equal to this string, the value 0 is returned;
returns a value less than 0 if the string is smaller than the string argument;
returns a value greater than 0 if the string is greater than the string argument.
instance
public class Test {

public static void main(String args[]) {
    String str1 = "Strings";
    String str2 = "Strings";
    String str3 = "Strings123";

    int result = str1.compareTo( str2 );
    System.out.println(result);
  
    result = str2.compareTo( str3 );
    System.out.println(result);
 
    result = str3.compareTo( str1 );
    System.out.println(result);
}

}
above program execution result is:
0
-3
3

RPC principle and related technologies used

RPC: Remote process call:
Several more typical RPC implementation and invocation frameworks (note: not a development framework) :
1: RMI: Implemented using the Java.RMI package, based on Java remote Method protocol and native Java serialization implementation.
2: hession: altogether lightweight remoting onhttp tool that provides RMI functionality with a simple approach based onhttp protocol.
3: Thrift: Thrift is a framework for scalable cross-language services.
Implementation principle of RPC framework:
There are three main roles in the framework, provider, Consumer, and Registry.

The techniques used in RPC:
1: Dynamic proxy: Java dynamic proxy technology is required to generate Client stub and Server stub. You can use the JDK’s native dynamic proxy mechanism. You can also use open source bytecode tools such as Cglib, Javassist.
2: Serialization: To be able to transfer and receive Java objects over the network, serialization and deserialization operations are required. Serialization is the process of converting Java objects to Byte [], and thus encoding. Deserialization is the process of converting a Byte [] into a Java object, or decoding. Java’s native serialization mechanism is available but inefficient, and it is recommended to use open source mature serialization technologies such as Protobuf, Thrift, and Hessian.
Nio: Many RPC frameworks use netty directly.
Service Registry: Redis, ZK, Elling, EtCD

About content type ‘multipart / form data…’ not support

Project Scenario:
on using springcloud, the gateway jump specifies that the service displays an error message


Problem description:

Content type 'multipart/form-data;boundary=----WebKitFormBoundaryek7Ljn5odm2QALlC;charset=UTF-8' not supported

Reason analysis:
this is because the requested parameter is content-type:multipart/form-data, which is submitted as a form. Therefore, the problem occurs on the parameter, and the annotation on the parameter can be removed (it only needs to be in the parameter form of springmvc)

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Context


issue the following error when unit testing

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Context

There are two reasons for this problem

1. See if there is a primary startup class

2. As shown in the figure above, if the main boot class already exists, the error may be in the test directory and will be reported if they are not in the same directory.
Put it in the same directory and the problem is solved.

Solution to the problem that listen TCP 0.0.0.0:3306: bind: address already in use port is occupied in Linux centos7

Problem description:
When installing the mysql container running docker in the host environment, the port is occupied

Reason analysis:
The port is occupied and cannot bind the address
Solutions:
netstat -nap look for port 3306 look at the pid that the program is running and find that it is actually occupied, remember the pid
netstat -nap | grep 3306

kill the process, here is kill 1107