Campbell Ritchie wrote:
Afraid that is incorrect. The increment to n occurs first. But the value of n++ is not incremented, i.e. n++ has the old value of n.Mark Ii wrote:. . . postfix increment operator is used. b is assigned the current value of n first which is 1 then n is incremented. . . .
Tomhmas Smith wrote:I am trying to create a code that would populate an array with prime numbers. However, when executing, the answer I get is 12345 instead of 2357 and I cant understand where did I miss the connection between methods.
Annet Rose wrote:My 12 years old child is interested in programming and would like to learn it. How can I teach him coding?
Jonathan Barry wrote: Hi, I'm a complete newbie attempting to learn Java. This is a task from codegym that I've solved
but I don't understand why the answers are what they are. I'm sure I'm missing something obvious,
but I would like some help understanding how this has an output of 12 and 2? X is equal to 10 if I
am deserving of graduated first grade. What am I missing?
Thanks for your help.
John
Cosmid Constantine wrote:Hi,
Sample program:
n = 0;
a = ++n;
b = n++;
If the post-increment or post-decrement operator has higher precedence than the pre-increment or pre-decrement operator, then why b didn't get a value of 2? I mean, it would make more logical sense if a = 0 and b = 2 with the precedence of the two.
I guess my question is, if n++ has higher precedence, why didn't it apply the ++ effect sooner than the ++ on the ++n? Is this just a special case in Java?
Thanks
Jesse Silverman wrote:I feel like I know a lot of true statements that have remained true about the "String Literal Pool" or the "String Constant Pool", as I see it variously called in different posts and articles, even within this forum. If I understand correctly, the behavior has zero dependence on what you compile with, and solely on which JVM is running the code, what's more, I suspect that this all changed ten years ago and is only confusing people because there is so much stale information about SCP out there in old tutorials and archived discussions and old mock exam questions.
My current belief is that the following statements from the certainly great-at-the-time but possibly no longer quite true tutorial on this site may have become inoperative:
When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table. Once a reference is made to that String object, any references to that String literal throughout your program are simply replaced with the reference to the object referenced from the String Literal Pool.
A downstream conclusion from this fundamental explication that I believe is no longer applicable now is:
Strings created at run-time will always be distinct from those created from String Literals.
It is my current belief that the String Constant Pool seems to now be built up during code execution, so that if a String Literal that the JVM is considering adding or looking up in the pool has already been added to the heap and then the SCP by executing user code, it will choose that very one as the reference to add to the String Literal Pool. In the old days, as this was done before a single line of user code executed, at class loading time, that would never ever happen.
The reason this matters so much (or at all) is that the simple downstream conclusion mentioned both in that article and many other places that exam-preparers are reading is no longer operative. Instead, it instead now depends on whether the code creating the string that is only created at runtime executes before the line that references the string constant!
I never loved these questions about "how many String objects get created? Where?" which I see from some sources are indicated to be much more common in preparatory materials and mock exams than on real exams.
Referenced "Was-Great-At-The-Time-But-Confusing-To-New-Exam-Takers-Now" article from this site:
https://javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
Hanna Roberts wrote:
Mark Ii wrote:. . .
Thank you, This code almost worked as well. The last two in my book fails though I'm not sure why. Thanks for your help though I appreciate it.
Carey Brown wrote:NEVER EVER close a Scanner object that has been created from System.in. This will cause System.in to get closed which is a bad thing. Some IDEs complain if you don't close all Scanner objects but in this case you don't want to do it.