Using string concatenation in Java to solve problems

Source: Internet
Author: User

The most commonly used data types during application development are strings. The same is true in the Java language platform. Mastering the technique of string handling is undoubtedly a skill that a database administrator must master. The author here to introduce how to use string concatenation to solve some practical problems.

An overview of string concatenation.

When writing applications, we often need to connect multiple strings to accomplish specific functions. If there are two string variables, the first name (the variable name) and the age (the variable is the name of the time). Now you need to output the string "My name is So-and-so, how old" on the screen. How do you write this statement? can be written as "My name is" +name+ ", Age is" +age. That is, in the Java language, multiple strings, including variables, can be concatenated into a string by the + number. Print on the screen or assign it to a new string variable. You need to be aware of this string connection. Perhaps some programmers have studied other development languages, such as SQL language, and so on. The string concatenation characters used in different languages are different. As used in SQL is the Symbol. Sometimes developers can be confused when developing applications. If you are developing an application in the Java language, you need to include SQL statements in your code to query the data from the database. It is possible to use string concatenation in both Java Code and SQL code (in SQL code to concatenate the contents of multiple fields into one field). Because two code uses a different connection symbol, you may get it wrong.

Second, the use of string connection to solve the problem of branch.

In the Java development platform, you can combine multiple strings into a single string by using the + number. In practice, however, this consolidation can often help system developers solve some of the toughest problems. As a rule of grammar, a contiguous string of strings in Java code cannot be written separately in two lines. What does that mean? If there is a long string, for "My name is radish, this year 29 years old, working in Ningbo." If you write this string on the same line, then this line will be very long, thus affecting the reading of the code. However, the author wants it to be on the same line when it is printed on the screen or saved in a file. In this case, how to deal with it?

Indeed, this is an issue that application developers often encounter during program development. Because when writing code, a good program developer should take into account the beauty of its code, which helps program developers read the code. After all, sometimes it takes multiple developers to do a task together. Instead of being able to do just one particular function. At this point, for the beauty of the Code and better reading, experienced program developers will often use this + number connection characters to deal with this branch of the problem.

Because of long strings, Java code cannot be written in two lines. Otherwise, the system prompts for syntax errors. In this case, in order to improve the reading, we often ask the program developers to divide them into two lines, and use the plus sign to connect them. As shown below:

"My name is radish, 29 years old this year," +

"Work in Ningbo"

That is, an application developer can concatenate two strings by using the + number, and then wrap the lines at the + number, thus connecting two strings. At this point, the Java compiler considers this to be legal, allowing the program developer to use this technique to make a reasonable branch of the string. Therefore, the use of string concatenation can effectively solve the branch problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.