Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

java - 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. 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

10bab

Shouldn't the output for only printing s4 be
10151015
as while printing string s1 the variable a and b both are called and concatenated. Or is it somewhere fault at my understanding. Would appreciate your valuable time and answer. Thanks

question from:https://stackoverflow.com/questions/65939989/can-someone-explain-me-why-the-print-of-only-string-s4-turns-out-to-be-10bab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all you are mixing the types which leeds you to missunderstanding.

int - responsible for storing whole numbers like 1, 2, 5

double - responsible for storing comma separated vales like 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)

So going through your code the output of the code line by line is:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...