Safe string concatenation

本文详细解释了在JavaScript中使用concat()方法与+运算符进行字符串拼接的区别与注意事项,强调了concat()方法在不同类型变量拼接时的正确用法,并对比了其性能表现。

Suppose you have a couple of variables with unknown types and you want to concatenate them. To be sure that the arithmetical operation would not be applied during the concatenation:

var one = 1;
var two = 2;
var three = '3';

var result = ''.concat(one, two, three); //"123"

This way of concatenation does exactly what you expect. On the contrary, concatenation with pluses might lead to unexpected results:

var one = 1;
var two = 2;
var three = '3';

var result = one + two + three; //"33" instead of "123"

Speaking about performance, in comparison with join type of concatenation, the speed of the concat type is pretty much the same.

You can read more about concat method on MDN page.

from:github/loverajoel

Lab 2 Chapter 1 Java Language Programming Chapter 1: Variables and Primitive Data Types (Part 2) Instructor: Ahsan Shehzad Date: September 3, 2025 Practical Session Objective Lab Goal: Store and Display a Contact Our objective is to create a simple Java program that stores the information for one contact using variables and then prints that information neatly to the console. Final Result Preview: (This is what your console output will look like) (Screenshot of the final console output will go here) Prerequisites: Java Development Kit (JDK) 11 or higher installed. An IDE like IntelliJ IDEA Community Edition ready to go. Step-by-Step Guided Exercise Step 1: Create the Project Structure Goal: Set up your project in IntelliJ and create the main class file. Instructions: 1. Open IntelliJ IDEA. 2. Go to File > New > Project... . 3. Name your project MyContactManager and choose a location to save it. Contact Profile: Name: John Doe Age: 32 Phone: 447700900123 Is a Friend: true 1 2 3 4 54. Ensure Java is selected and you have a JDK configured. 5. Once the project is created, right-click the src folder in the Project Explorer. 6. Select New > Java Class . 7. Name the class Main and press Enter. Code Block: Your IDE will generate this boilerplate code for you. Add the main method inside the class. Expected Result: You have a clean Main.java file, and the program can be run (though it won't do anything yet). Step 2: Declare and Initialize Contact Variables Goal: Create variables inside the main method to hold a contact's data. Instructions: 1. Inside the main method, declare and initialize variables for each piece of contact information. 2. Choose the most appropriate data type for each value. 3. Pay special attention to the literals for long ( L ) and String (double quotes). Code Block: Add the following lines inside your main method. Expected Result: The program should compile without any errors. When you run it, still nothing will happen, but the data is now stored in memory. public class Main { public static void main(String[] args) { // Our code will go here! } } 1 2 3 4 5 public static void main(String[] args) { // String is a special object type we use for text. String firstName = "John"; String lastName = "Doe"; // Use 'int' for age. int age = 32; // A phone number can be large, so 'long' is a safe choice. long phoneNumber = 447700900123L; // 'boolean' is perfect for a simple true/false status. boolean isFriend = true; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14Step 3: Display the Information Goal: Use System.out.println() and string concatenation ( + ) to print the stored data to the console. Instructions: 1. After the variable declarations, add a println statement to act as a header. 2. For each variable, write a println that combines a descriptive label (e.g., "Name: ") with the variable itself using the + operator. Code Block: Add these lines after your variables in the main method. Expected Result: When you run the main method, you should see the formatted contact details printed to your console, matching the goal on slide 13. Putting It All Together Goal: Review the complete, final code for this lab session. Instructions: Your final Main.java file should look exactly like this. Make sure your code matches and run it one last time to confirm the output. Code Block: // ... variables from previous slide ... // --- Displaying the Information --- System.out.println("Contact Profile:"); System.out.println("Name: " + firstName + " " + lastName); System.out.println("Age: " + age); System.out.println("Phone: " + phoneNumber); System.out.println("Is a Friend: " + isFriend); 1 2 3 4 5 6 7 8 public class Main { public static void main(String[] args) { // --- Storing Contact Information --- // String is a special object type we use for text. String firstName = "John"; String lastName = "Doe"; // Use 'int' for age. int age = 32; // A phone number can be large, so 'long' is a safe choice. long phoneNumber = 447700900123L; // 'boolean' is perfect for a simple true/false status. boolean isFriend = true; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Expected Result: A clean, working program that successfully stores and displays data. You've just completed Phase 1 of the project! (This slide is intentionally left blank to fit the 21-slide structure. It can be used for instructor-specific notes or an extra exercise if needed.) Challenge Task For Those Who Finish Early... Challenge 1: Add a Second Contact Declare and initialize a new set of variables for a second person (e.g., firstName2 , age2 , etc.). Print their details to the console, separated from the first contact by a line of dashes ( "----------" ). Challenge 2: Perform a Calculation After creating both contacts, declare a new double variable named averageAge . Calculate the average age of the two contacts and store it in the variable. Hint: (age1 + age2) / 2.0 . Why / 2.0 and not / 2 ? Print the average age to the console with a descriptive label. // --- Displaying the Information --- System.out.println("Contact Profile:"); System.out.println("Name: " + firstName + " " + lastName); System.out.println("Age: " + age); System.out.println("Phone: " + phoneNumber); System.out.println("Is a Friend: " + isFriend); } } 17 18 19 20 21 22 23 24 答案是什么
09-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值