background preloader

Java 1

Facebook Twitter

Jvm

Concurrency 1. Scala 1. Concurrency 2. Scala 2. Scala 3. Scala 4. Developing with real-time Java, Part 1: Exploit real-time Java&# Stories/MichaelDykman - J. Michael Dykman I am probably the last person on this list who should pipe in here. I have been dabbling with the language for about 2 years now. Previously, I had spent 30+ years using computer languages, 20 of them professionally, mostly in what I lovingly refer to as "the bottom-feeding world of business app development". I have used C, C++, Perl, Java, PHP, Javascript. All the predictable suspects and then some. I discovered J by accident, the subject of another post for another time. As I spent my first few hours fooling around, getting it do "something", I was gradually overcome by a sense of awe. When I encountered train syntax, my first reaction was that it was the most arbitrary, academically-inspired nonsense I had ever seen.

Others can better describe the formal properties of the train that lend it such power and elegance but I can tell that now, to me, every problem looks like a train. Posted to the J forum on 2009-06-04. "What does it stand for? " It proved that he was serious. How do ClassLoader leaks happen? Other Articles in the Reloading Java Classes Series A video presentation “Do you really get class loaders” by Jevgeni Kabanov From ClassLoaders to Classes If you have programmed in Java for some time you know that memory leaks do happen.

Usually it’s the case of a collection somewhere with references to objects (e.g. listeners) that should have been cleared, but never were. Let’s get started. Every object had a reference to its class, which in turn had a reference to its classloader. This means that If a classloader is leaked it will hold on to all its classes and all their static fields. To examine this from a different perspective let’s return to the code example from our previous article. Introducing the Leak We will use the exact same Main class as before to show what a simple leak could look like: The ExampleFactory class is also exactly the same, but here’s where things get leaky. The important things to note about Example class are: Post Mortem The results include several Leak objects. Args4j:

Java is Pass-by-Value, Dammit! Serbo-Croation Translation Available! Woohoo! Jovana Milutinovich gratiously offered to translate this article to Serbo-Croation! You can read the translation here: Thanks a million, Jovana! Introduction I finally decided to write up a little something about Java's parameter passing. I'm a compiler guy at heart. Pass-by-value The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. Pass-by-reference The formal parameter merely acts as an alias for the actual parameter. Java is strictly pass-by-value, exactly as in C. When the method or constructor is invoked (15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared Type, before execution of the body of the method or constructor.

[In the above, values is my emphasis, not theirs] itself. Objects as Functions in Java. Earlier this year I wrote a build tool in java. The core idea at the time was to express the build in terms of functions and function composition. This is not exactly a good fit with java. So last week I had some spare time and came up with this way of defining a function (application) in java: public static class FancyFunction extends FunctionBase { @In public final Str a; @In public final Str b; @Out public final Str c = null; @Out public final Str d = null; public FancyFunction(Str a, Str b) { this.a = a; this.b = b; super.reflect(); } protected void evaluate() { setResult(c, new StrImpl(a.getValue() + "-" + b.getValue())); setResult(d, new StrImpl("XX")); } } There is obviously a lot of magic going on and I do feel a bit bad abou setting final fields using reflection.

What it actually does is defining two functions. In a friendlier syntax somwhat like this: fancy.c(a, b) = a + "-" + b; fancy.d(a, b) = "XX"; But back to the java example, you can now compose applications like this: What I miss in Java. So I finally got some time to sit down and write, after being knee deep in work the past month or two. And without a doubt, I wanted to write about what has been heckling and annoying me over the past month.

I am an ardent defender of Java as a good language, especially defending it from Misko day in and day out, but even I will agree that it does suck at times. So today, Java, the gloves are off. I love you, but this is the way things are. To give some context, I have been working on GWT a lot recently, and have done some crazy things with GWT generators (which I might cover in a few posts later). Closures (Abiity to pass around methods) So this was the straw that broke the camel’s back. Ok, so my other option is write a function which checks that, right? CallConditionally(myPojo.setValue, actualValue); Except you can’t. There are multiple JSR’s and open source libraries which try to implement this for Java, and one of these days, I’m gonna give it a try. Map if (myValue) instead of. Signalling Integer Overflows in Java. ...and people are still using java? Last week I was at the LASER 2009 summer school on Software Testing.

It was pretty interesting and one brief talk captured my attention. The speaker, Martin Nordio, asked the audience if they knew Eiffel and only a few hands were raised. So he asked if they knew Java instead. Let's do the same here. 1. Foo () { int b = 1; b++; } 2. Foo () { int b = 1; while (true) { b++; break; }} 3. Foo () { int b = 1; try { throw new Exception(); } finally { b++; }} 4. Foo () { int b = 1; while (true) { try { b++; throw new Exception(); } finally { b++; break; } } b++; } 5. Int foo () { try { return 1; } finally { return 2; }} 6.

Int b; int foo () { b = 0; try { b++; return b; } finally { b++; }} PS: Feel free to post your answers as comments to this blog entry. DEADBEEF or CAFEBABE. Two Java Type‐System Holes. Next Generation Java is not very different from today's Jav. Next Generation Java is not very different from today's Java In a recent post, Stephan Schmidt makes several suggestions in order to write "Next Generation Java".

Unfortunately, I disagree with most of them... Let's take them in turn: final everywhere This is guaranteed to make your code littered with final keywords and therefore, impair readability significantly (same problem as Python's self pollution). And it really doesn't buy you that much, I can't even remember last time I was bitten by a bug involving the reassignment of a variable to a different value within a code block. For what it's worth, final on non fields is banned from Google's style guide (except where it's unavoidable, such as when you need to pass variables to inner classes).

No Setters It would be nice, but it's just not realistic. No loops Java is not very well suited to a functional style, so I think the first example is more readable than the second one that uses Predicates. Use one liners Depends. Thanks. 1. 2. 3. Java's Lots of Little Files. I ran into this article about "Next Generation Java Programming Style", at YCombinator. There was some interesting discussion about the overall effectiveness of these suggestions. Part of the discussion involved commenter Xixi asking if anyone had followed point #5, "Use many, many objects with many interfaces". It turns out, I've been following that model. I started to reply there, but I recognized a blog post after a bit. Here's my general workflow, and I've found it to be quite effective.

The linked-to article refers to interfaces as "roles", and that's probably the easiest way to think of them. If I have a few related items which need to be handled, I first create an interface for it: IMessage (I realize it isn't the java convention to prefix interfaces with "I", but I prefer it - especially with as many as I ended up with).

Create the concrete class (the actual implementation): FooMessage. Next up, the test class - but I'll get to it in a moment. Well, it has. Go Ahead: Next Generation Java Programming Style. August 10, 2009 by Stephan Schmidt Many companies and developers move away from Java to new languages: Ruby, Python, Groovy, Erlang, Scala. You might be trapped with Java. Even if you’ve trapped, you can change your programming style and reap some of the benefits of those new languages. In the last 15 years Java programming style has changed significantly: This tips lead to better Java code. Try them in your next Java class. Uodate: Some thoughts to Cedrics thoughts. As the author of the post, your thoughts are appreciated, some of mine: “Besides, it’s convenient to be able to mutate object if you want to use pools.” No setters doesn’t mean you can’t mutate objects, it’s just that plain setters are not object oriented thinking.

“I think the first example is more readable than the second one that uses Predicates.” I think your assumptions are wrong. About the author Stephan Schmidt has been working with internet technologies for the last 20 years. A Java old timer reminisces – Oak, 0XCAFEBABE, Green Threads, Ru. I have been working with Java since the mid nineties, and in that time, Java has gone from being a “fad”, the next “big thing”, etc, to becoming a widely accepted and deployed programming language in the business world.

Being involved with Java for a long time has its share of ups and downs. On the plus side, you know and understand the very basic foundations of the language and its evolution, on the negative side you sometimes talk about stuff no one else has a clue about. So many people have given me a strange look whenever I have mentioned any one of the four items in the title that I have decided to write a blog post about it just so that I can declare “go read it up on my blog” :). Oak Oak is the name that Java was known by internally at Sun before a committee of lawyers gave their approval for it to be called Java. 0xCAFEBABE There was much speculation about the origin of word when it was initially introduced.

Green Threads RuntimeException Like this: Like Loading... Double Brace Initialisation. The high cost of (WS-)Security. WS-Security provides a comprehensive set of security features for Web service applications, building on established industry standards for cryptography and XML encryption and signing. You can specify the features to be used for a particular application with WS-Policy and WS-SecurityPolicy, allowing clients of the service to configure themselves automatically to access the service. With widespread support for these standards across multiple platforms and web services frameworks, interoperability is good (and getting better over time). Despite these benefits, WS-Security also has some drawbacks. You've seen in the last two articles of this series that WS-Security can be complex to configure, and that it sometimes adds a lot of bulk to the messages being exchanged.

So when are the benefits of WS-Security worth the costs? Looking at performance Performance test application The application used for testing is a seismic data retrieval service. Listing 1. WS-Security performance Figure 1. <? #534364. Keynote: The Future of Java Innovation. Ternary Try/Catch. We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it’s often easy to forget the outcome, and lose code snippets. So I thought I’d start blogging some of them so I don’t lose them, and other people might suggest other ways of doing things that we’ve overlooked. This particular problem occurs when you want to assign the result of a method that can throw an exception to a final variable. For example: This will fail to compile with “variable c might already have been assigned”.

Of course making c not final would solve the problem, but that’s no fun. If we were not using Exceptions, Java provides a useful ternary operator “? Which is nice and clean, but means that getCustomer is going to have to return null or throw a RuntimeException in the case that there is no matching customer, which is undesirable. We could also possibly use something along the lines of. Common Java Cookbook. Original Sin. I’ve often said that Java’s original sin was not being a pure object oriented language - a language where everything is an object. As one example, consider type char.

When Java was introduced, the Unicode standard required 16 bits. This later changed, as 16 bits were inadequate to describe the world’s characters. Tangent: Not really surprising if you think about it. In the meantime, Java had committed to a 16 bit character type. I was having this conversation for the umpteenth time last week. So how would we go about getting rid of primitive types without incurring a significant performance penalty? Java has a mandatory static type system; it is compiled into a statically typed assembly language (Java byte codes, aka JVML).

Assume that the we have a final class Int representing 32 bit integers. To make Int a suitable replacement for int, we would like the syntactic convenience of using operators: Int twice(Int i) { return i + i;} This is easy enough. Why? However, what of types like int[]? Is Java as we know it doomed? While Oracle and Sun Microsystems are hailing Oracle's purchase of Sun as a big boost for Java, others are not so sure, questioning what kind of control Oracle might try to exercise over the popular platform that has driven so many enterprise applications since it was first developed in 1995.

Observers also expect Oracle to make a go of trying to make more money off of Java than Sun ever could. Sun has tried to leverage Java as a lead-in to selling services, but without much success. By contrast, Oracle is very disciplined about extracting money from its technologies. "Java is one of the computer industry's best-known brands and most widely deployed technologies, and it is the most important software Oracle has ever acquired," the companies said in a joint statement announcing the acquisition.

"Oracle Fusion middleware, Oracle's fastest growing business, is built on top of Sun's Java language and software. . [ What on earth was Oracle thinking when it bought Sun? Mulling over Java. Transaction strategies: The API Layer strategy. Whether you are using a container environment with EJB 2.1 or 3.0, the Spring Framework environment, or a Web container environment such as Tomcat or Jetty with the Java Open Transaction Manager (JOTM), you still need a transaction strategy to ensure database consistency and integrity. The Java Transaction API (JTA) specifies the syntax and interfaces associated with transaction processing (see Resources), but it doesn't describe how to put these building blocks together.

Just as a construction crew needs a blueprint to build a house out of a pile of lumber, you need a strategy that describes how the transactional building blocks are put together. The strategy I describe in this article is the API Layer transaction strategy. It is the most robust, simplest, and easiest-to-implement transaction strategy.

With that simplicity comes limitations and considerations that I also describe. Basic structure Figure 1 illustrates a typical logical application layer stack for most Java applications: Why Java doesn&#039;t need operator overloading (and very few la. MVEL - Home. Small Language Changes for JDK7. Putting Java’s Null in its Place. Presenting the Permanent Generation. Busting java.lang.String.intern() Myths. Correcting the Billion Dollar Mistake. Ten Amazing Java Applications. How to deal with filesystem softlinks/symbolic links in Java. How much memory is used by my Java object? Jedi - Examples. Java : the perpetually undead language. Data Rattle.

Heterogeneous Lists and the Limits of the Java Type System. The case for Java modularity - Java World. Top 3 Reasons Why Constructors are Worthless. Build an Embedded Array Language in Java. Java Is Too Academic. The Long Strange Trip to Java. Java in 2008.