简体   繁体   English

有人能解释一下为什么只有字符串 s4 的打印结果是 10bab 吗?

[英]Can someone explain me why the print of only string s4 turns out to be 10bab?

I am currently learning java for the first time and got stuck at certain peice of code.我目前是第一次学习 java 并且卡在了某些代码处。 The exercise was on comments.该练习是关于评论的。 The task was to comment out unnecessary part of code.任务是注释掉不必要的代码部分。

public class Solution {
    public static void main(String[] args) {
        int a = 10;
        int b = 15;
        double c = b + 38;
        //int d = a + 12;
       // double e = 12.3;
        String s = "s" + a;
        String s1 = a + "b";
        //String s2 = "a";
        String s3 = s1 + "a";
        String s4 = s3 + "b";
        System.out.println(c + s4 + s);
    }
}            

If i print only s4 the output turns out to be如果我只打印 s4 output 结果是

10bab

Shouldn't the output for only printing s4 be仅打印 s4 的 output 不应该是
10151015
as while printing string s1 the variable a and b both are called and concatenated.就像打印字符串 s1 一样,变量 a 和 b 都被调用并连接起来。 Or is it somewhere fault at my understanding.还是我的理解有问题。 Would appreciate your valuable time and answer.感谢您宝贵的时间和答复。 Thanks谢谢

There's is a rule in java : java中有一条规则:

int a = 10;
String str = a + "xxx"; // means str = String.valueOf(a) + "xxx"

int a = 10;                         // a == 10
int b = 15;                         // b == 15
double c = b + 38;                  // c == b + 38 = 15 + 38 = 53.0
//int d = a + 12;
// double e = 12.3;
String s = "s" + a;                 // s == "s" + a = "s" + "10" = "s10"
String s1 = a + "b";                // s1 == a + "b" = "10" + "b" = "10b"
//String s2 = "a";
String s3 = s1 + "a";               // s3 == s1 + "a" = "10b" + "a" = "10ba"
String s4 = s3 + "b";               // s4 == s3 + "b" = "10ba" + "b" = "10bab"
System.out.println(c + s4 + s);     // c + s4 + s = "53.0" + "10bab" + "s10" = "53.010babs10

First of all you are mixing the types which leeds you to missunderstanding.首先,您混合了导致您误解的类型。

int - responsible for storing whole numbers like 1, 2, 5 int - 负责存储整数,如 1、2、5

double - responsible for storing comma separated vales like 1.5, 6.3, 5.321 double - 负责存储逗号分隔的值,如 1.5、6.3、5.321

String - responsible for alphanumeric signs so it can store anything "Table", "2" (NOTE if im using "" it means that this is meant to be a String)字符串 - 负责字母数字符号,因此它可以存储任何“表”、“2”(请注意,如果我使用“”,则意味着这意味着它是一个字符串)

So going through your code the output of the code line by line is:因此,逐行查看代码的 output 是:

int a = 10;
int b = 15;
int c = b + 38; // OUTPUT 53
//int d = a + 12;
//double e = 12.3;
String s = "s" + a; // OUTPUT s10 COMMENT You are adding String "s" to int a so a become String with value of "10"
String s1 = a + "b"; // OUTPUT 10b COMMENT same story here a become String with value "10" and we are adding string "b"   
//String s2 = "a";                                                  
String s3 = s1 + "a"; // OUTPUT 10ba COMMENT s1 is now "10b" and you are adding "a"
String s4 = s3 + "b"; // OUTPUT 10bab COMMENT s3 is now "10ba" and you are adding "b"                                                
System.out.println(c + s4 + s); // OUTPUT 5310babs10 COMMENT c becomes String with value "53" adding s4 = "10ba" adding String s = "s10" 

To achieve your expectations 10151015 the code should look like this为了达到您的期望 10151015,代码应如下所示

int a = 10;
int b = 15;
int c = b + 38; // OUTPUT 53
String s1 = String.valueOf(a) + String.valueOf(b); // OUTPUT 1015
String s3 = s1 + String.valueOf(a); // OUTPUT 101510
String s4 = s3 + String.valueOf(b); // OUTPUT 10151015

I wasn't running that code but I hope you understand your wrong understanding:D我没有运行该代码,但我希望你理解你的错误理解:D

int a is a variable where as "a" is a String . int a是一个变量,其中"a"是一个String The variable a is only used in String s1 (and String s ), the other String s use "a" which won't be replaced by the value of a , because it is a string literal.变量a仅在String s1 (和String s )中使用,其他String s 使用"a" ,不会被a的值替换,因为它是字符串文字。

int a = 10;
String s1 = a + "b";
String s3 = s1 + "a";
String s4 = s3 + "b";

String s4 will be: String s4将是:

10 + "b" + "a" + "b"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM