简体   繁体   中英

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

There's is a rule in 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

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

int a is a variable where as "a" is 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.

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

String s4 will be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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