Thursday, September 8, 2011

Challenge 012’s Solution

Statements I, II and III all print the number “1234”. I believe that this means that the “+” sign has two different meanings then in JAVA. When there aren’t any quotation marks in the problem, JAVA works like a regular calculator, with 12 × 100 + 34 = 1234. However, when there are quotation marks, the “+” sign merely combines the quoted expression with the added number, ignoring whether or not it makes mathematical sense that “12” + 34 = 1234. Therefore, the use of quotation marks is very important when programming mathematics in JAVA.
 
−Phoenix

5 comments:

  1. Phoenix, I like the way you document your observations and dialog about the conclusions you draw from them. This is computer science, and you exemplify the science part of this process in your post.

    My follow-up question, then:

    There are different types in data in Java. When we see the double quotes in Java ("), what kind of data is contained between them, versus the data type represented by just a whole number, like 12? I'm looking for the specific names of each type here. Thank you.

    ReplyDelete
  2. I believe that a statement within quotation marks is called a “literal string”. When a literal string is used with a “+” sign, it merely combines the two expressions. However, without quotation marks, the variable is not a literal string, and instead acts like a variable.

    −Phoenix

    ReplyDelete
  3. Almost there. Let's look at "However, without quotation marks, the variable is not a literal string, and instead acts like a variable."

    Agreed, quotes mean that we're dealing with a String data type.

    If I have just 12, what kind of data type is that?

    If I have 12.0, what kind of data type is that?

    Answer those two questions as follow-up. Thank you?

    ReplyDelete
  4. If you have just 12, then it is an “int” data type. If you have 12.0, then it is a “double” data type. Both of these data types are considered to be “literary constants”.

    −Phoenix

    ReplyDelete
  5. Fair enough, literal constants, yes. Thanks for working through the details on this. That is work that will payoff in the long run.

    15 XPs awarded for this completion.

    ReplyDelete