background preloader

j2se

Facebook Twitter

Java is Pass-by-Value, Dammit! - Scott Stanchfield. Introduction I finally decided to write up a little something about Java's parameter passing. I'm really tired of hearing folks (incorrectly) state "primitives are passed by value, objects are passed by reference". I'm a compiler guy at heart. The terms "pass-by-value" semantics and "pass-by-reference" semantics have very precise definitions, and they're often horribly abused when folks talk about Java. 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. The Litmus Test. Java 7: Project Coin in code examples. We Recommend These Resources This blog introduces - by code examples - some new Java 7 features summarized under the term Project Coin. The goal of Project Coin is to add a set of small language changes to JDK 7. These changes do simplify the Java language syntax. Less typing, cleaner code, happy developer ;-) Let's look into that. Prerequisites Install Java 7 SDK on your machine Install Eclipse Indigo 3.7.1 You need to look out for the correct bundles for your operating system.

In your Eclipse workspace you need to define the installed Java 7 JDK in your runtime. Next you need to set the compiler level to 1.7 in Java > Compiler. Project CoinImproved literals A literal is the source code representation of a fixed value. "In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. 01.public class LiteralsExample { 03. public static void main(String[] args) { 04. 06. long creditCardNumber = 1234_5678_9012_3456L; 09. 10. 12. 17. 18.

Thought Spearmints: @SuppressWarnings and its applicability to various program elements. I cooked up a toy Java 5 class that generates warnings of the "unchecked" family when compiled: package pholser.util; import java.util.Arrays; public class Stack<T> { private final Object[] storage; private int top; public Stack() { this.storage = new Object[ 5 ]; } public Stack( final Stack<T> other ) { this.storage = (Object[]) other.storage.clone(); this.top = other.top; final T currentTop = (T) storage[ top ]; } public void push( final T item ) { checkPrecondition( isFull(), "can't push a full stack" ); storage[ top++ ] = item; } public T pop() { checkPrecondition( isEmpty(), "can't pop an empty stack" ); return (T) storage[ --top ]; } public T peek() { checkPrecondition( isEmpty(), "can't peek an empty stack" ); return (T) storage[ --top ]; } @Override public boolean equals( final Object that ) { if ( this == that ) return true; if ( that == null || !

GetClass().equals( that.getClass() ) ) return false; final Stack<? > other = (Stack<? Sure enough, no warnings. Fair enough. Golden. Or. Eamonn McManus's Blog: Getting rid of unchecked warnings for casts. Posted by emcmanus on March 30, 2007 at 3:27 AM PDT If you've ever made a serious effort to get rid of "unchecked" warnings from the Java compiler (the ones it gives you with -Xlint:unchecked) then you'll probably have found some cases where you know a cast is correct but you can't convince the compiler of it. Is there anything better than adding @SuppressWarnings("unchecked") around the whole method? The problem with the @SuppressWarnings solution is that it's much too broad. It suppresses warnings for the whole method, even though it's just one little cast that you want to allow. Fortunately, there's a good solution. List<String> list = (List<String>) x; You can change it to this: List<String> list = cast(x); where the cast method is defined like this: @SuppressWarnings("unchecked")private static <T> T cast(Object x) { return (T) x;} If there several different classes where you need this method, you could make it public and put it in a utility class, say Util.java.

10 examples of Enum in Java - What is Enum in Java Enum in Java is a keyword, a feature which is used to represent fixed number of well known values in Java, For example Number of days in Week, Number of planets in Solar system etc. Enumeration (Enum) in Java was introduced in JDK 1.5 and it is one of my favorite features of J2SE 5 among Autoboxing and unboxing , Generics, varargs and static import. One of the common use of Enum which emerged in recent years is Using Enum to write Singleton in Java, which is by far easiest way to implement Singleton and handles several issues related to thread-safety and Serialization automatically. By the way, Java Enum as type is more suitable to represent well known fixed set of things and state, for example representing state of Order as NEW, PARTIAL FILL, FILL or CLOSED. Enumeration(Enum) was not originally available in Java though it was available in other language like C and C++ but eventually Java realized and introduced Enum on JDK 5 (Tiger) by keyword Enum.

Case PENNY: Java SE APIs & Documentation. Get Ready for Java 7 with the Free JDK 7 Reference Card. Oracle submitted the release content for both Java SE 7 and 8 to the JCP at the end of last year. The JDK 7 and 8 JSRs represented Oracle's "Plan B" approach for separating JDK 7 into two separate releases.

In the months since, JDK 7 has progressed to the Developer Preview milestone and the final version isn't too far off. To provide developers with an at-a-glance reference guide of all the essential JDK 7 elements, Developer.com has published the JDK 7 Reference Card. Following a free registration, you can download this helpful reference guide to get JDK 7 information such as: Newly included JSRs (including NIO.2, The Da Vinci Project and Project Coin)Changed JSRs in maintenance reviews (including JAXP, JAXB and JAX-WS)Enhanced JSRs (including JLS and the JVM spec)Smaller enhancements (including GC and concurrency)JSRs deferred to JDK 8 or later (including closures) All this info and more are condensed into one comprehensive, double-side PDF.

Java Basics: Table of Contents [First Draft - June 2005] Trail: Essential Classes: Table of Contents (The Java™ Tutorials) Trail: JDBC(TM) Database Access: Table of Contents (The Java™ Tutorials) Java Programming Notes.

Libraries

Top Ten New Things You Can Do with NIO. New I/O? Why do we need a new I/O? What's wrong with the old I/O? There's nothing wrong with the classes in the java.io package; they work just dandy -- for what they do. But it turns out there are quite a lot of things the traditional Java I/O model can't handle. Things like non-blocking modes, file locks, readiness selection, scatter/gather, and so on. NIO brings a host of powerful new capabilities to the Java platform. In this article I'm not going to explain buffers, channels, selectors, and the other denizens of the NIO depths. So, without further ado, in the time-honored tradition of O'Reilly authors flogging their own books by cooking up lame top ten lists: Top Ten New Things You Can Do with NIO That You Couldn't Do Before (TTNTYCDW..., oh never mind). 10: File Locking File locking is one of those things most programmers don't need very often.

With NIO, file locks are built right into the FileChannel class. Figure 1: Reader processes holding shared locks. Annotations. Many APIs require a fair amount of boilerplate code. For example, in order to write a JAX-RPC web service, you must provide a paired interface and implementation. This boilerplate could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible. Other APIs require “side files” to be maintained in parallel with programs. For example JavaBeans requires a BeanInfo class to be maintained in parallel with a bean, and Enterprise JavaBeans (EJB) requires a deployment descriptor. It would be more convenient and less error-prone if the information in these side files were maintained as annotations in the program itself. The Java platform has always had various ad hoc annotation mechanisms. Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program.

Annotations complement javadoc tags.

Generics