Rob Spoor

Marshal
+ Follow
since Oct 27, 2005
Rob likes ...
Eclipse IDE Spring VI Editor Chrome Java Windows
Cows and Likes
Cows
Total received
121
In last 30 days
0
Total given
56
Likes
Total received
1779
Received in last 30 days
27
Total given
2496
Given in last 30 days
13
Forums and Threads

Recent posts by Rob Spoor

Correct. That means the order is important: 15 + 10 + "" becomes "25" (addition first, then concatenation), while "" + 15 + 10 and 15 + "" + 10 both become "1510" (only concatenation). Because of this, I advise to never combine concatenation with any other operator unless you use parentheses. Even better is to create variables to store the results before the concatenation.

There is another way that prevents confusion, because there is no String concatenation involved. That's to use printf or String.format. For instance:
Note that you need to add a newline yourself, either by using \n (Linux newline) or %n (system newline, the same thing that println uses).
13 hours ago
The issue is caused by the evaluation, and operator precedence. In Java (and probably everywhere else, even in Math), + and - have the same precedence. That means that if these are encountered, they are executed in left-to-right order.

So let's check your examples:
1) System.out.println(x-y);. There is only one operator to apply before the method call, -. That is applied to both int operands, and the resulting value is passed to the method call.

2) System.out.println(x + " - " + y + " is: " + (x-y));. There are several operators to apply here, so let's go through them in order:
  • x + "-"; this is String concatenation, where x is converted to a String. The result is "25-"
  • "25-" + y; again, String concatenation, where y is converted to a String. The result is "25-10"
  • "25-10" + " is: "; again String concatenation. The result is "25-10 is: "
  • Parentheses are encountered, which have the highest precedence, so this is executed first. The result of x-y is 15
  • "25-10 is: " + 15; again, String concatenation, where 15 is converted to a String. The result is "25-10 is: 15"

  • This final value is then printed.

    3) System.out.println(x + " - " + y + " is: " + x-y);. There are several operators to apply here, so let's go through them in order:
  • x + "-"; this is String concatenation, where x is converted to a String. The result is "25-"
  • "25-" + y; again, String concatenation, where y is converted to a String. The result is "25-10"
  • "25-10" + " is: "; again String concatenation. The result is "25-10 is: "
  • "25-10 is: " + x; again, String concatenation, where x is converted to a String. The result is "25-10 is: 25"
  • "25-10 is: 25"-y; this is where the compiler fails. It tries to apply - to a String and an int. Unlike +, - is not supported for String, so the compiler shows the error you've seen

  • 17 hours ago
    Here's the official "Get started" guide for Java: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html. It also recommends Maven.
    17 hours ago
    This calls for a bug/change request in the OpenJDK mailing lists, because the documentation still tells people to create new CachedRowSetImpl instances...
    2 days ago
    In your example, Gaston and Alphonse want to bow to each other. The following flows do not lead to deadlock:
    1) Gaston bows (lock on Gaston), Alphonse bows back (lock on Gaston is still there, additional lock on Alphonse), then both locks are dropped. Alphonse bows (lock on Alphonse), Gaston bows back (lock on Alphonse is still there, additional lock on Gaston), then both locks are dropped.
    2) Alphonse bows (lock on Alphonse), Gaston bows back (lock on Alphonse is still there, additional lock on Gaston), then both locks are dropped. Gaston bows (lock on Gaston), Alphonse bows back (lock on Gaston is still there, additional lock on Alphonse), then both locks are dropped.

    Now consider that Alphonse and Gaston both start bowing at the same time:
    * Gaston bows (lock on Gaston) and Alphonse bows (lock on Alphonse). Gaston waits for Alphonse to bow back, and Alphonse waits for Gaston to bow back. Since both are now waiting on the other, there is deadlock.
    4 days ago

    Matthew Bendford wrote:I don't get the question - but you could look inside src.zip or rt.jar - depend on if you looking for source or class files.


    Note that rt.jar doesn't exist anymore. It's replaced by modules (.jmod files) which you can find at $JAVA_HOME/jmods. These .jmod files apparently are stil glorified ZIP files. Not like JAR files though, they have a more complex internal structure.
    5 days ago
    Spring can do JSTL + CSS as well, and it can definitely do MVC; there's a module called "spring-webmvc" after all. However, to get JSP + JSTL working you need to do some configuration. The default is Thymeleaf, and with it you can do just about the same as with JSP + JSTL but in a slightly different way. CSS is really easy to get working; just put it in src/main/resources/static or src/main/resources/public, as everything in those folders is directly accessible as static content relative to the context path (so src/main/resources/static/style.css is available as http://localhost:8080/style.css out-of-the-box, as there is no context path out-of-the-box).
    5 days ago
    I know one other use: to add annotations in JEE/Jakarta EE or Spring (Boot) applications like @Transactional or @PostConstruct.
    6 days ago
    With JsonParser you can also do streaming parsing. It works like an iterator, and only the current token needs to be in memory (well, for the parser):

    The possible tokens reflect on what's possible in JSON: START_OBJECT and END_OBJECT, START_ARRAY and END_ARRAY, FIELD_NAME, and the possible values: VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT, VALUE_TRUE, VALUE_FALSE and VALUE_NULL (I don't know why they didn't create VALUE_BOOLEAN instead of VALUE_TRUE and VALUE_FALSE).

    However, if you can't do a similar streaming write, then this is obviously irrelevant.
    1 week ago
    Copied from your other thread:

    Akash bhamri wrote:


    In the add method, your assignments are this.from = from, this.to = to and this.greetings = greetings. Since this method has no parameters or local variables with those names, these are equivalent to the following:
    Since none of these fields had been assigned yet, they were null, and you're just making them null again. If the fields were final, you'd have gotten a compiler error. You probably want to use E.from, E.to and E.greetings instead.

    And please, rename the parameter to email, or at the very least e. Fields, variables and parameters shouldn't start with a capital letter.
    1 week ago
    What do you mean with "stay in line"?

    You're using tab characters as separators. If your values have different lengths, these tabs are located in different columns. That does lead to text that isn't aligned. However, that shouldn't matter; comma or tab separated files are not really meant to be read by people. You can easily import them in applications like Excel which does provide a nice table layout.
    1 week ago