• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about a specific line of code  RSS feed

 
Pat Gareau
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have been practicing code through a website and was wondering if someone could explain what each part in it does and if there's a more efficient way to write it out?



the part I'm wondering about is System.out.println(a + " x " + (d+1) + " = " + (a * (d+1)));    why the + " x " and why the + " = "

Thank you all, I am very new to programming and just trying to find out why things are the way they are

 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A warm welcome to the Ranch, Pat

First, remember, that class names supposed to start with an upper case, so, the mult better would read as Mult, in fact, it isn't a good name too, because it doesn't express class purpose. Maybe for a start would be better Multiplication or Multiplicator?

Indentation is also very important. Look for the suggestions how to indent the code, so the aligments and formatting would help you read the code.

Pat Gareau wrote:why the + " x " and why the + " = "
These are simply being printed out to a console, so you see how the result being built. i.e.
3 x 4 = 12
If you'd have line like that, what you'd see in the console as text would be 3 4 12, not that clear isn't it?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on what Liutauras Vilda said, if you just write

then that would be the same thing as

Since println() has a form that accepts a single argument of type double, that method is used and converts that double to a String and prints it.
However, if you include the " x " and " = ", then the content inside the call to println is seen as a String, since java looks at

and says, "how do I use the "+" operator on a double and a String", and converts the first argument (a, a double) to a String and then concatenates it with the second argument, a String, producing a new String. This continues with the rest of the expression, eventually producing the string that Liutauras mentioned.  Now, since there is also a version of println() that takes a single argument of type String, it then uses that form and merely prints out that argument.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!