<h2>
ANSWER</h2>
=> Code
public class Name{
public static void main(String [ ] args){
String firstname = "George";
String lastname = "Gershwin";
System.out.println(lastname + "," + firstname);
}
}
=> Output
Gershwin,George
<h2>
EXPLANATION</h2>
The code has been written in Java. The following is the line by line explanation of the code.
// Declare the class header
public class Name{
// Write the main method for the code.
public static void main(String [ ] args){
// Declare a string variable called firstname and initialize it to hold the
// first name which is <em>George</em>.
String firstname = "George";
// Declare a string variable called lastname and initialize it to hold the
// last name which is <em>Gershwin</em>.
String lastname = "Gershwin";
// Print out the lastname followed by a comma and then the firstname
System.out.println(lastname + "," + firstname);
} // End of main method
} // End of class declaration
<h2>
</h2><h2>
NOTE</h2>
The explanation of the code has been written above as comments. Please go through the comments of the code for more understanding.
Nevertheless, a few things are worth noting.
(i) Since it is a complete program, a main class (<em>Name</em>, in this case) has to be defined. The main class has a main method where execution actually begins in Java.
(ii) The first name (George) and last name (Gershwin) are both string values therefore they are to be stored in a String variable. In this case, we have used variable <em>firstname </em>to hold the first name and <em>lastname </em> to hold the last name. Remember that variable names must not contain spaces. So variable names <em>lastname </em>and <em>firstname</em>, though compound words, have been written without a space.
(iii)The System.out.println() method is used for printing to the console in Java and has been used to print out the concatenated combination of last name and first name separated by a comma.
(iv) In Java, strings are concatenated using the + operator. To concatenate two strings lastname and firstname, write the following:
lastname + firstname.
But since they are separated by a comma, it is written as:
lastname + "," + firstname