Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
CPColin
Sep 9, 2003

Big ol' smile.
Child classes are allowed to "hide" members from their parent classes. It gets annoying, like in this case. Eclipse, at least, can be configured to warn when a member variable or method parameter is hiding another variable.

Adbot
ADBOT LOVES YOU

CPColin
Sep 9, 2003

Big ol' smile.

qntm posted:

getClassName bound to A.className, because that's the variable which was in scope when A was initialised... so why doesn't getClassName bind to A.inner when THAT'S in scope?

Because it's bound to this.inner. That is, it's polymorphic and uses whatever inner is closest. Methods are polymorphic and variables are not. Declare the variables private and it becomes more obvious what's going on.

CPColin
Sep 9, 2003

Big ol' smile.
I would jokingly suggest using Live by Experts Exchange, but there's not enough people using it yet to be a good joke.

CPColin
Sep 9, 2003

Big ol' smile.

Gravity Pike posted:

We aggressively use Optionals, and we're very happy with it. It drastically cuts down on NPE's, and clarifies intent in interfaces.

Also Java 8 is beautiful and nothing hurts. Except type erasure. gently caress that guy.

Speaking of type erasure, you wouldn't be able to do this with Optional:

code:
public void setWhatever(Optional<ClassA> value) { ... }

public void setWhatever(Optional<ClassB> value) { ... }
but you would be able to do it with annotations:

code:
public void setWhatever(@Nullable ClassA value) { ... }

public void setWhatever(@Nullable ClassB value) { ... }
I really wish java.lang would pick up the null annotations and rt.jar would get annotated, so Eclipse could stop complaining about everything so much.

CPColin
Sep 9, 2003

Big ol' smile.
And what if setWhatever() needs to do something different when you pass it a null value?

CPColin
Sep 9, 2003

Big ol' smile.
Assume it's not a setter on a POJO, despite the name I arbitrarily gave it. Like, maybe these processValue() methods have to do something special, like display an error, when you pass in an empty Optional:

code:
public abstract void processValue(Optional<ClassA> classA);

public abstract void processValue(Optional<ClassB> classB);
You can't do this, because of type erasure:

code:
public void someCodeSomewhere()
{
   Optional<ClassA> classA = loadSomeValueFromSomewhereMaybe();

   processValue(classA);

   Optional<ClassB> classB = loadSomeValueFromSomewhereElseMaybe();

   processValue(classB);
}
You would have to change processValue() to make the signatures different:

code:
public abstract void processValue(ClassA classA);

public abstract void processValue(ClassB classB);
and then you'd have to unwrap the Optional values before you passed them, which kind of defeats the purpose. Now you need @Nullable on the method parameters anyway and ugh Oracle just extend the JVM spec already!

CPColin
Sep 9, 2003

Big ol' smile.
Also, do you get to use Java 8 features?

CPColin
Sep 9, 2003

Big ol' smile.
Now I'm laughing at you, instead!

CPColin
Sep 9, 2003

Big ol' smile.
Casual poster had already edited in the answer by the time you replied.

CPColin
Sep 9, 2003

Big ol' smile.

baka kaba posted:

#JustJavaThings, but this isn't true right? As far as I remember (phoneposting when I shouldn't be) there's a pool of Integer objects for a certain range of values, and when you do Integer d = 3 it assigns d to the pre-existing object instead of creating a new one. You have to explicitly create a new Integer object if that's what you want. So

I just checked the bytecode that line generates and you're right. The autoboxing makes it Integer d = Integer.valueOf(3), which does check a cache for values between -128 and a system property that defaults to 127. I didn't know that, but that's okay; I should never care exactly which Integer instances I'm dealing with.

CPColin
Sep 9, 2003

Big ol' smile.
I'd probably debug that method and see what gets passed in when you click the button normally, then create an instance of ActionEvent in your test that matches that. I don't know if that counts as valid testing, or what.

CPColin
Sep 9, 2003

Big ol' smile.
Most of that stuff is internal stuff you can't set. You can call the ActionEvent constructor:

code:
new ActionEvent(theButtonThatWasClicked, ActionEvent.ACTION_PERFORMED, "Improve Barricades");
If that impBarrActionPerformed() method doesn't call evt.getSource() or evt.getCommand(), you can probably pass nulls for those parameters. If the method doesn't check anything in the event at all, you can probably skip creating an event altogether.

But, at that point, you're not really testing much.

CPColin
Sep 9, 2003

Big ol' smile.
The way the interfaces are currently designed, you could pass anything that implements Contract.View to Contract.Presenter.onCreate(), but you can only pass something that implements MainContract.View to MainContract.Presenter.onCreate(). Types could exist that implement Contract.View, but not MainContract.View, so MainContract.Presenter.onCreate() doesn't cover everything on its own.

CPColin
Sep 9, 2003

Big ol' smile.
Different how?

CPColin
Sep 9, 2003

Big ol' smile.
I mean, I guess you're right, that pre-computing the values is technically a pre-optimization, but I'd be reluctant to approve a pull request that replaced those values with method calls (even though the JIT probably would make them fast).

CPColin
Sep 9, 2003

Big ol' smile.
Except don't iterate over a Map's key set, calling Map.get() with each key. Instead, iterate over the Map's entry set and save yourself a pile of lookups.

CPColin
Sep 9, 2003

Big ol' smile.

Paul MaudDib posted:

use StringBuffer

Or StringBuilder, so you don't waste processing time on unnecessary thread safely.

CPColin
Sep 9, 2003

Big ol' smile.
There are tools that let you profile memory usage and inspect the heap and stuff, but I have no idea which ones are "good" these days.

CPColin
Sep 9, 2003

Big ol' smile.
I think basically everybody at my work is pretty tired of our home-built controller/layout/template/rendering engine and I wouldn't mind seeing if something like Thymeleaf could rescue us. Has anybody used it? (Is this a better question for the webdev thread?)

CPColin
Sep 9, 2003

Big ol' smile.
It looks like they recently released version 3.0.0 and rewrote their parser so you don't need strict XML any more.

We were using JSP when I first started here. It sucked, but like most technologies, we probably weren't using it right!

CPColin
Sep 9, 2003

Big ol' smile.
Yeah, I'm eager to see if it'll work for our email templating, too, because we have, of course, yet another ancient subsystem that nobody likes handling our email templates. I guess the latest version also added support for CSS and JS templates, which sounds like we could finally get all of that stuff to share the same constants with our Java code, for things like CSS class names.

CPColin
Sep 9, 2003

Big ol' smile.
The try-catch will probably be a bunch slower than a range check would be, if you expect to be out of range a lot.

For the other issue, if nulls are the billion-dollar mistake, List.indexOf() and Map.get() taking Object instead of their parameterized type has got to be worth a couple hundred thousand.

CPColin
Sep 9, 2003

Big ol' smile.
I spent a couple hours a few days ago trying to figure out why, no matter what I did, turning on Thymeleaf's caching didn't seem to do anything. It finally worked when I used reflection to get the field I needed, set it to be accessible, and set it myself.

Then I discovered that turning off JRebel made it work fine. Thanks, JRebel.

CPColin
Sep 9, 2003

Big ol' smile.
Use try-with-resources blocks for both the Reader and the Writer and you don't have to worry about explicitly closing them!

CPColin
Sep 9, 2003

Big ol' smile.
IntStream.forEach() takes an IntConsumer, though, so you can't just pass this::action if it doesn't accept an integer parameter. Now you're jumping through hoops to get the signatures to fall in line and you'd be better off just using a normal loop.

CPColin
Sep 9, 2003

Big ol' smile.
But you only have to write that function once, compared to writing IntStream.range(0,5).forEach(ignored -> this.action()) every time. (You can't use the method reference in that line if it doesn't accept an integer parameter.)

CPColin
Sep 9, 2003

Big ol' smile.

Volguus posted:

And, oh, by the way, the double-checked locking idiom that you have in Locker is wrong.

As that article says, only before Java 5 and only if you leave out the volatile keyword.

CPColin
Sep 9, 2003

Big ol' smile.
The part I really liked about Thymeleaf was that the template can be valid HTML, so our designers could return to them and make changes, even after all the processing hooks were added. You can't do that with JSP.

CPColin
Sep 9, 2003

Big ol' smile.
For that, you probably either can't use an array or you can't use a stream.

CPColin
Sep 9, 2003

Big ol' smile.
Are you sure the older JAR went away when you were excluding it? And that no other dependencies were sneaking in old versions? (Whee, dependency hell!)

CPColin
Sep 9, 2003

Big ol' smile.

Boz0r posted:

I have some code that doesn't compile in IntelliJ, but our ant script works fine. I don't get it.

code:
public double someFunction(List<Pair<Long, Double>> parameter) { ... }

public BigDecimal someFunction(List<Pair<Integer, BigDecimal>> parameter) { ... }
As far as I can tell, it shouldn't compile. Why does it compile in ant?

Yeah, that's weird. While the JVM spec allows methods to differ only by return type, Java definitely does not, so with the type erasure, those two should clash.

CPColin
Sep 9, 2003

Big ol' smile.
Yeah, you shouldn't ever compare Strings using ==. Always use String.equals() or you'll get behavior like you're seeing.

CPColin
Sep 9, 2003

Big ol' smile.

PierreTheMime posted:

So I have a report application that pulls data from an API and adding results to a List. At this point everything works fine but I'm looking to increase performance by having the pulls subdivided into multiple threads to save on time. Is using a Collections.SynchronizedList() and a synchronized method for adding items okay or is there a better thread-safe option? This would be running on a server with 1.7.

If you don't want to roll your own thread pool, you could use an ExecutorService and submit your tasks to that, instead of adding them to a queue.

CPColin
Sep 9, 2003

Big ol' smile.
One way or another, you need to keep track of the state of the three bases. If you care which player is on a given base (and if you had a Player class or enum), you could have:

code:
   Player firstBase;
   Player secondBase;
   Player thirdBase;
If, on the other hand, you only care whether or not a base is occupied, you could have:

code:
   boolean firstBase;
   boolean secondBase;
   boolean thirdBase;
Then, your batting() code could update the scores and bases.

At this point, you're starting to pass a lot of state around, so you'll want to start thinking about how all the state fits together. Like, what parts of the game are the same every time you run the simulator? (Does the state of the game need to track who's on each team or just who's up to bat next? Does each team care if it's the home team or does only the game care about that? Stuff like that.)

CPColin
Sep 9, 2003

Big ol' smile.
Class.getResourceAsStream() should be able to handle things while skipping the URL step.

CPColin
Sep 9, 2003

Big ol' smile.

Ariong posted:

Okay here's something. When I run this code:

Java code:
System.out.println(new File(".").getAbsolutePath());
I get this output.

Java code:
C:\Users\Bill Murray\Documents\NetBeansProjects\slotMachineFXML
That's not right, is it? Shouldn't src be in there somewhere?

Only if you're in the src directory when you're running your code. Looks like NetBeans is running your code from the project root.

CPColin
Sep 9, 2003

Big ol' smile.

MercurialOne posted:

I'm learning Java now.

This compiles, runs, and prints horse:

code:
boolean x = true;
String s = "Horse";
System.out.println(x ? s: 1L);
I understand why/how it works, but what I don't get is how it translates into bytecode. A long and an object reference are different sizes, does it not matter which one you're talking about when setting up the call stack?

Or, is ternary in Java just syntatic sugar for an if statement, and it actually translates into two code paths, with their own copy of System.out.println?

You can use the javap command to see what the bytecode looks like. In this case, it looks like this:

code:
$ javap -c Test.class 
Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: iconst_1
       5: istore_1
       6: ldc           #2                  // String Horse
       8: astore_2
       9: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
      12: iload_1
      13: ifeq          20
      16: aload_2
      17: goto          24
      20: lconst_1
      21: invokestatic  #4                  // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      24: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      27: return
}
You can see here what M31's talking about. The Long.valueOf() call boxes up the primitive into a Long instance and then passes it (or the String) to the overload of System.out.println() that takes an Object.

CPColin
Sep 9, 2003

Big ol' smile.
When you say "httpinterface," do you mean the "IHttpService" interface? If so, the problem would be that CustomScanIssue doesn't implement IHttpService, so the @Override annotations are incorrect. That doesn't quite gel with what you're asking about, though. Can you provide more information about the errors you're seeing?

CPColin
Sep 9, 2003

Big ol' smile.
Ah, yes, that fun Gotcha. If you're lucky, it throws an exception. If you're unlucky, you get silent, undefined behavior. Yeah, no good reason why that class needs to keep track of any state, rather than pass state around its private methods.

Adbot
ADBOT LOVES YOU

CPColin
Sep 9, 2003

Big ol' smile.
Maybe the class is so old, they don't have good test coverage of it and are afraid to break something? There's a pretty reasonable solution to that problem, of course.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply