Jan Hoppmann

Ranch Hand
+ Follow
since Jul 19, 2010
Merit badge: grant badges
For More
Germany
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Jan Hoppmann

Campbell Ritchie wrote:Yes, it does make a difference. Repeated concatenation in a loop runs in worse than O  time complexity. You won't notice anything for such a small String, not until you are concatenating at least 10⁴ instances. The problem remains today, even though an optimisation introduced in Java9 reduced the problem and made the process something from 10× to 100× faster than in Java8.



Ah, I was talking about the string not in a loop - there it doesn't make a difference if a StringBuilder is used explicitly or implicitly later. Sure, for loops you are definitely correct. But still thanks for the in-depth explanation!
1 week ago

Tim Holloway wrote:And if you must concatenate by brute force, use a StringBuilder!



But this doesn't make a difference since Java 8, does it? At compile time, the + will be replaced by calls to a StringBuilder's append method. Or did I get something wrong here?
1 week ago

Liutauras Vilda wrote:



Oh, I want to try this myself to see if I still remember correctly.

The first line creates a reference and one String object. It is a compile-time constant string and should be interned automatically. The reference points to the string in the string pool.
The second line creates a new String with the same content - but new creates a new object, stored in the heap.

So, there are two objects that are created. They are equal when compared by the equals method, but comparing by == will produce false.

But to add to the discussion, here is a question my former employer asks every applicant: What is the difference between StringBuilder and StringBuffer?
1 month ago

Swapnil Mishra wrote:That means constructors are only for Sub classes?



Pretty much, yes. You can call them implicitly, like here, or explicitly with a super() call. They can be used to set the state of its fields, or perform some other work common to all child classes.
1 month ago
Try extending your Alien class, don't give the child class its own constructor, and then instantiate it. What happens?
1 month ago
Nice!

I've been on and off here for the last 11 years (my first post was in 2013), but more silently reading than actually replying. Back then, I was, at most, an intermediate dev and still working on my bachelor's degree. Nowadays, I'm a senior Java dev. And some of my day-to-day knowledge actually came from the Java Ranch.
I'm still a bit salty because I won a book on here a few years back, but never got it (don't know if it got lost in transit), but that's not the Ranch's fault.

I'm so glad this place is still active.

Edit: Oh, I looked only on the first page of my posts. Looks like I've been here a few years longer than I remember, since my Junior Dev days in 2010
2 months ago
My approach seems to be a bit more naïve (and rather long):

10 years ago
I had a similar problem a while back (computing probabilities for an arbitrary number of different dice (with arbitrary number of sides), and am interested in how you solved this
10 years ago
I have to say that I have difficulty following your code. For starters, compDate1 through 3 are not good names for variables. Why not something like birthdate, zodiacStart, zodiacEnd or something like this? That would make the logic in line 49 much clearer. And why do you parse the year of date in line 36, only to throw it away in line 45?
10 years ago

Monica. Shiralkar wrote:

Do we have to create object of Abc abc=new Abc() as class variable and then set the value as above or this object should be created inside the method and then value is set?



I'd say the way you're doing it now is right, but it really depends on your requirements.

If the value is meant to be part of the state of the object at creation, you can set it in your constructor. But if it is likely to change (or is meant to be changed), the setter / getter you're using now seems right.
10 years ago

Edwin Torres wrote:You can use threads. In this example, a thread does the querying (simulated), sleeps 10 seconds, and repeats. The thread ends when you call interrupt(). Here's the code:



That's the way I solved problems like these before using the Timer / TimerTask. Are there any potential downsides to this? I think code like this is in a no-longer maintained program of mine that might or might not run on a client's server ;)
10 years ago

Duncan MacFarland wrote:I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass? Thanks.



I'm not sure if Pawel got your problem.
Is it something like you have a class A, a subclass of A named B, and a LinkedList, but want to insert an object of B at position, say, 5, and want the JVM to know that this is of type B, not A?
10 years ago

John Brendan wrote:Could you write me the code so I can test in Eclipse please?



There is no code involved. Both of these links are for tools, not code. YOu need to start / install them. And the links should provide you with all the info you need to do that.
10 years ago
Yay! This is the second time in my life I won anything
Thanks!
10 years ago

Mark Do wrote:Ok.......I cant still seem to figure out how or where to put the for loop so that the whole array will loop 1024 times! someone help! thanks!



You edited your code after my reply. It looked good before; you looped 1024 times over the loop that flips the five coins - that is exactly what you wanted, is it not? I don't see why you changed it. Did it produce weird results?
10 years ago