Topic: 交流一下关于jdk5(1.5) tiger 的新特点 |
Print this page |
1.交流一下关于jdk5(1.5) tiger 的新特点 | Copy to clipboard |
Posted by: nothing Posted on: 2004-10-02 05:16 |
2.Re:交流一下关于jdk5(1.5) tiger 的新特点哈。。。。 [Re: nothing] | Copy to clipboard |
Posted by: 异类喳哇 Posted on: 2004-10-02 05:23 最早叫 JDK,曾经改叫 J2SE, 现在又叫 JDK。 |
3.全新J2SE 1.5新特性简介 [Re: nothing] | Copy to clipboard |
Posted by: 异类喳哇 Posted on: 2004-10-02 14:32 {敬请活用编辑功能使您的文章更易于阅读,谢谢} 里程碑式的革新—全新J2SE 1.5新特性简介 http://www.sina.com.cn 2004年05月13日 16:28 天极网 文/飞鹰编译 Java2标准版(Java 2 Platform, Standard Edition, J2SE)1.5即将正式推出,这一次的版本更新不同于以往,它带来了很多里程碑式的革新,SUN将其绰号取名为“虎”。这一次的变革将是Java诞生以来从未有过的,它给我们带来了耳目一新的感觉。下面我们就来欣赏一下其中的部分典型变化: 1.自动包装和解包(Autoboxing and unboxing) 代码示例 往一个ArrayList中加入一个整数,1.5版本以前的版本写法是: List list = new ArrayList(); list.add( new Integer( 10 ) ); 而在1.5版本中可以写为: list.add( 10 ); 因为,在1.5版本中,对一个整数进行包装,使之成为一个Integer对象(即包装,boxing),然后加入到一个ArrayList中的做法被认为是没有必要的,反之,解包(unboxing)的做法也是没有必要的,这样的代码只是增加了程序的文本长度而已,所以1.5版本支持了自动包装和解包操作,对于bool/Boolean,byte/Byte, double/Double,short/Short,int/Integer,long/Long,float/Float的相应包装/解包操作都进行了支持,从而使代码变得简单。 2.更优化的循环语句(The inhanced for loop) 代码示例 一个典型的遍历数组的循环语句,1.5版本以前的写法是: for ( Iterator iterator = list.iterator(); iterator.hasNext(); ) { Integer n = (Integer)iterator.next(); ... }//for 而在1.5版本中可以写为: for ( Integer n : list ) { ... }//for 显然1.5版本的写法比以前是大大简化了,但是在需要修改集合,比如删除其中元素时不能采用这种写法。之所以Java1.5版本没有象C#那样干脆定义一个foreach关键词,主要是因为SUN认为增加一个专门的关键词成本太高了 (too costly)。但1.4版本中就曾经增加了assert关键词,1.5版本中也新增加了enum关键词,因此这一解释恐怕并不那么令人信服。 3.参数可变的方法和printf 代码示例 当不能确定一个方法的入口参数的个数时,以往版本的Java中,通常的做法是将多个参数放在一个数组或者对象集合中作为参数来传递,1.5版本以前的写法是: int sum(Integer[] numbers) { int nSum = 0; for(int i: numbers) nSum += i; return nSum; } ... //在别处调用该方法 sum(new Integer[] {12,13,20}); 而在1.5版本中可以写为: int sum(Integer... numbers) { int nSum = 0; for(int i: numbers) nSum += i; return nSum; } ... //在别处调用该方法 sum(12,13,20); 显然,1.5版本的写法更为简易,也更为直观,尤其是方法的调用语句,不仅简化很多,而且更符合通常的思维方式,更易于理解。 1.5版本自身就有一个应用该特征的典型例子,即C风格的格式化输出方法——printf。 代码示例 输出一个加法算式,1.5版本以前的写法是: int x = 5; int y = 7; int nSum = x + y; System.out.println(x + " + " + y + " = " + nSum); 而在1.5版本中可以写为: System.out.printf("%d + %d = %d\n", x, y, nSum); 以上两种写法的输出结构是一样的,即“5 + 7 = 12”。 这种改变不仅仅是形式上的,printf还可以提供更为灵活、强大的输出功能,比如限定按照两位整数的形式输出,可以写为“System.out.printf("%02d + %02d = %02d\n", x, y, nSum);”,输出结果将是“05 + 07 = 12”。 4.枚举 代码示例 构建一个表示色彩的枚举,并赋值,在1.5版本中可以写为: public enum MyColor{ Red, Yellow, Blue } MyColor color = MyColor.Red; for ( MyColor mycolor : MyColor.values() ) System.out.println( mycolor ); 以往的Java版本中没有enum关键词,1.5版本中终于加入了进来,这确实是一个令人高兴的改进。此外,enum还提供了一个名为values()的静态方法,用以返回枚举的所有值的集合。所以,以上程序的输出结果是把“Red”、 “Yellow”、“Blue”分行输出。 而enum提供的静态方法valueOf()则将以字符串的形式返回某一个具体枚举元素的值,比如“MyColor.valueOf(“Red”)”会返回“Color.Red”。静态方法name()则返回某一个具体枚举元素的名字,比如“MyColor.Red.name()”会返回“Red”。类似的方法还有不少。此外,enum自身还可以有构造方法。 5.静态引用 代码示例 当我们要获取一个随即数时,1.5版本以前的写法是: import java.lang.Math; //程序开头处 ... double x = Math.random(); 而在1.5版本中可以写为: import static java.lang.Math.random; //程序开头处 … double x = random(); 静态引用使我们可以象调用本地方法一样调用一个引入的方法,当我们需要引入同一个类的多个方法时,只需写为“import static java.lang.Math.*”即可。这样的引用方式对于枚举也同样有效。 6.总结 以上对J2SE1.5的部分新特征做了一些简单的介绍。总而言之,1.5版本的Java确实给我们带来了很多令人激动的变革,如同以上介绍的那样,很多功能以前的版本也能实现,但是不能实现得这样简单、漂亮。相信这次变革会给Java带来更多的追随者。 |
4.Re:交流一下关于jdk5(1.5) tiger 的新特点哈。。。。 [Re: nothing] | Copy to clipboard |
Posted by: aleel_008 Posted on: 2004-10-03 12:44 还有泛型啊!的确很不错 |
5.Re:交流一下关于jdk5(1.5) tiger 的新特点哈。。。。 [Re: nothing] | Copy to clipboard |
Posted by: COWCOW Posted on: 2004-10-06 16:34 {敬请活用编辑功能使您的文章更易于阅读,谢谢}USING STATIC IMPORTS FOR CONSTANTS AND METHODS Many new language features have been introduced in the release of J2SE 5.0. Static imports is a feature that helps in the creation and use of global constants. It's one of the easiest new features to incorporate into your code. In this tip, you'll have the chance to use static imports for constants and methods. You'll start with the traditional technique of using "constant interfaces". These interfaces only exist to hold constants. These constants should be moved into utility classes, but these classes have traditionally been hard to use. The problems disappear with static imports. In addition to their use for constants, static imports can ease the calling of methods and classes. However there is a downside to using static imports in this way: it can make your code less readable. This is especially true with wild cards. It can become hard to determine where methods and attributes originate. In the past, it was common for developers to use interfaces to store values that would be treated as global constants. Here's an example: public interface BadIrrationalConstants { public static final double SQRT_TWO = 1.414; public static final double SQRT_THREE = 1.732; } This was particularly appealing to developers who needed more than one group of constants. For instance, a developer might need the values defined in the BadIrrationalConstants interface as well as those in the following interface, BadTranscendentalConstants: public interface BadTranscendentalConstants { public static final double PI = 3.14159; public static final double E = 2.71828; } It is tempting to use this technique of constant interfaces because it is easy to make the global constants available for use. You simply implement each interface containing the needed constants like this: public class BadUseOfConstants implements BadTranscendentalConstants, BadIrrationalConstants { public static double sinPiOverFour() { return SQRT_TWO / 2; } public static void main(String[] args) { System.out.println("Pi is approximately " + PI); System.out.println("The sin of Pi/4 is about " + sinPiOverFour()); } } Before the J2SE 5.0 release, your only alternative was to use fully qualified names such as BadIrrationalConstants.SQRT_TWO everywhere. This was excessively verbose and led to hacks such as BadUseOfConstants. The problem with the approach above is that the constants are now part of the type BadUseOfConstants. Any class that uses these constants adds them to its public interface. This is a restriction on further modifications of the class. That's because clients of the class might depend on the constants being present. Instead, the constants should be part of a class like this: package staticEx; public class IrrationalConstants { public static final double SQRT_TWO = 1.414; public static final double SQRT_THREE = 1.732; } Before you are tempted to extend IrrationalConstants to use SQRT_TWO and SQRT_THREE, assume that you also need the constants defined in TranscendentalConstants: package staticEx; public class TranscendentalConstants { public static final double PI = 3.14159; public static final double E = 2.71828; } Before the release of J2SE 5.0, it took quite a bit of effort to use these constants, sometimes obscuring the equations in which they were used. However, static import allows you to easily import static members into your code. For example, if you want to use the static class attribute SQRT_TWO, you declare the following: import static staticEx.IrrationalConstants.SQRT_TWO at the beginning of the class in which you want to use the constant. You can then use SQRT_TWO as you did in BadTranscendentalConstants. Note that you need to use the keyword static after the keyword import. If you try the following without the static keyword, it will not compile. Compile and run the following class, ConstantsWithStaticImport, using a J2SE 5.0 compiler: package staticEx; import static staticEx.IrrationalConstants.SQRT_TWO; import static staticEx.IrrationalConstants.SQRT_THREE; import static staticEx.TranscendentalConstants.PI; public class ConstantsWithStaticImport { public static double sinPiOverFour() { return SQRT_TWO / 2; } public static void main(String[] args) { System.out.println("Pi is approximately " + PI); System.out.println("The sin of Pi/4 is about " + sinPiOverFour()); } } You should see the following displayed: Pi is approximately 3.14159 The sin of Pi/4 is about 0.707 You can use wildcards in the import as you might expect. In this case, you can replace the first two import statements with: import staticEx.IrrationalConstants.*; As always, being specific and avoiding the use of the wildcard helps control exactly what is being imported. It also makes things clearer for someone reading your source code. You can use static imports for static methods as well as for static attributes. In the class below, IrrationalConstants2, you have both: package staticEx; public class IrrationalConstants2 { public static final double SQRT_TWO = 1.414; public static final double SQRT_THREE = 1.732; public static double sinPiOverFour() { return SQRT_TWO / 2; } } Make them all available by using the wildcard. Now you can call the static method sinPiOverFour(), which is defined in IrrationalConstants2, like this: package staticEx; import static staticEx.IrrationalConstants2.*; import static staticEx.TranscendentalConstants.*; public class ConstantsWithStaticImport2 { public static void main(String[] args) { System.out.println("Pi is approximately " + PI); System.out.println("The sin of Pi/4 is about " + sinPiOverFour()); } } You can see however that there are already readability problems. It is not easy to determine whether sinPiOverFour() is in the IrrationalConstants2 class or in TranscendentalConstants. You can clarify this by replacing the wildcards in the import with explicit references to the method being called. There is less confusion when you use a class in the standard J2SE distribution such as java.lang.Math. By using the static import, you reduce client code such as Math.PI and Math.sin() to PI and sin(). package staticEx; import static java.lang.Math.*; public class ConstantsWithStaticImport3 { public static void main(String[] args) { System.out.println("Pi is approximately " + PI); System.out.println("The sin of Pi/4 is about " + sin(PI / 4)); } } In addition to static methods and attributes, you can use static import for static classes. For example, the class java.awt.geom.Point2D.Double is a static subclass of java.awt.geom.Point2D. If you import java.awt.geom.Point2D.Double, it can refer to all of its static members by using Double.xxx. The following example, StaticClassEx, uses the static field out in the final class, java.lang.System, to simplify calls to System.out.println(). StaticClassEx prints to standard out using out.println(), and calls the static method distance() using Double.distance(). package staticEx; import static java.awt.geom.Point2D.Double; import static java.lang.System.out; public class StaticClassEx { public static void main(String[] args) { out.println("The diagonal of a unit square is " + Double.distance(0.0, 0.0, 1.0, 1.0) + "."); } } The result is: The diagonal of a unit square is 1.4142135623730951. When used judiciously, static imports can make your code simpler to read and make it easier to work with static variables, methods, and classes. Be careful not to overuse this technique. Also be sure to explicitly import a variable or method when there is a possibility of ambiguity. For additional information about static imports, see the section Static Import in the description of new features and enhancements for J2SE 5.0. |
6.Re:交流一下关于jdk5(1.5) tiger 的新特点哈。。。。 [Re: nothing] | Copy to clipboard |
Posted by: COWCOW Posted on: 2004-10-06 16:37 FORMATTING OUTPUT WITH THE NEW FORMATTER J2SE 5.0 introduces a new way to format output that is similar to that of the C language's printf. In this approach, each argument to be formatted is described using a string that begins with % and ends with the formatted object's type. This tip introduces you to the new Formatter class and to the syntax of the formatting that you perform with the class. It's likely that you will most often use the new formatting approach in a call similar to either of the following: System.out.format("Pi is approximately %f", Math.Pi); System.out.printf("Pi is approximately %f", Math.Pi); The printf() and the format() methods perform the same function. System.out is an instance of java.io.PrintStream. PrintStream, java.io.PrintWriter, and java.lang.String each have four new public methods: format( String format, Object... args); printf( String format, Object... args); format( Locale locale, String format, Object... args); printf( Locale locale, String format, Object... args); These correspond to the format() method in the underlying worker class java.util.Formatter class: format(String format, Object... args) format(Locale l, String format, Object... args) Although you'll likely use these methods from String, PrintStream, and PrintWriter, you'll find the documentation for the various available formatting options in the documentation for the Formatter class. Let's begin with the following example of the format() method: formatter.format("Pi is approximately %1$f," + "and e is about %2$f", Math.PI, Math.E); The format() method requires some of the new language features introduced in J2SE 5.0. One is varargs, which simplifies the way that an arbitray number of arguments can be passed to a method. Notice that a variable number of Object instances can be entered for formatting. Also notice that the objects in the example are autoboxed and unboxed. Autoboxing/Unboxing is also a new feature in J2SE 5.0. Autoboxing eliminates the need for manually converting a primitive type (such as int) to a wrapper class (such as Integer), unboxing automates the reverse process. In the example, Math.PI is a double which is autoboxed to a Double (to be treated as an Object). In addition, in the example the formatted output is written to java.lang.StringBuilder, yet another new feature introduced in J2SE 5.0. The format itself is a String that includes zero or more formatting elements, each beginning with a %. Each formatting element is applied to one of the Objects passed in. Each formatting element has the general form: %[argument_index$][flags][width][.precision]conversion The argument index is a positive integer that indicates the position of the argument in the argument list. The numbering begins with 1 for the first position, not with 0. So the first position in the previous code snippet is occupied by Math.PI, and is indicated by using 1$. The second position is occupied by Math.E, and is indicated by using 2$. The width specifies the minimum number of characters to be written as output. The precision is used to restrict the number of non-zero characters. The conversion describes the type of the object being formatted. Much of this should be familiar to C programmers because this is a Java implementation of printf(). Common types include f for float, t for time, d for decimal, o for octal, x for hexadecimal, s for general, and c for a Unicode character. The following sample application, UsingFormatter, allows you to enter different formats from the command line and view the output. Notice that the application instantiates a destination -- in this example, a StringBuilder. It then instantiates a Formatter and associates it with the destination. The Formatter then formats some String and sends the output to the destination. The results of the conversion are then displayed to standard out. package format; import java.util.Formatter; public class UsingFormatter { public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: " + "java format/UsingFormatter "); System.exit(0); } String format = args[0]; StringBuilder stringBuilder = new StringBuilder(); Formatter formatter = new Formatter(stringBuilder); formatter.format("Pi is approximately " + format + ", and e is about " + format, Math.PI, Math.E); System.out.println(stringBuilder); } } Compile and run this with the command line argument %f: java format/UsingFormatter %f You should get the result: Pi is approximately 3.141593, and e is about 2.718282 Rerun this and set the precision to be two decimal places: java format/UsingFormatter %.2f You should see the following: Pi is approximately 3.14, and e is about 2.72 Notice that the numbers are not just truncated. The value of e is rounded off to two decimal places. You can additionally specify the width by supplying the command line argument %6.2f. This time leading spaces are inserted because you specified that the number should use six characters, even though the precision restricts it to using only three characters and a decimal place. If you enter the command: java format/UsingFormatter %6.2f You should see this: Pi is approximately 3.14, and e is about 2.72 The position can be used to specify which argument to format. Rerun UsingFormatter with the command line argument %1$.2f. This specifies that you want to use Math.PI twice. You should see the following output: Pi is approximately 3.14, and e is about 3.14 You can change the Locale used to format the numbers by adding an import statement and calling a different constructor. Here's an example: package format; import java.util.Formatter; import java.util.Locale; public class UsingFormatter { public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: " + "java format/UsingFormatter <format string>"); System.exit(0); } String format = args[0]; StringBuilder stringBuilder = new StringBuilder(); Formatter formatter = new Formatter(stringBuilder, Locale.FRANCE); formatter.format("Pi is approximately " + format + ", and e is about " + format, Math.PI, Math.E); System.out.println(stringBuilder); } } Compile and run this with the argument %.2f and you should see the decimal points changed to commas. Pi is approximately 3,14, and e is about 2,72 As previously mentioned, you will typically not use the Formatter class explicitly. Instead, for example, you can directly use the printf() and format() methods in the PrintStream class. The following program, UsingSystemOut, is a rewritten version of the UsingFormatter program to use standard out: package format; public class UsingSystemOut { public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: " + "java format/UsingSystemOut <format string>"); System.exit(0); } String format = args[0]; System.out.format("Pi is approximately " + format + ", and e is approximately " + format, Math.PI, Math.E); } } The behavior of UsingSystemOut is slightly different than that of UsingFormatter. The UsingFormatter program uses println(), the UsingSystemOut program does not. Because of that, if you run UsingSystemOut from the command line, you will notice that your next command prompt is on the same line as your output. You need to insert a new line. You can do this using formatted output by adding %n. Run UsingSystemOut with the command line argument %.2f%n: java format/UsingSystemOut %.2f%n You will see the following result: Pi is approximately 3.14 , and e is about 2.72 You can replace the last method call with printf() if you prefer. There is no difference between System.out.format() and System.out.println(). As a final example, let's take a look at how date and time objects can be formatted. These objects have the conversion type t or T. That letter is followed by a second letter that indicates which part of the time should be displayed and how it should be displayed. For example, you can display the hour in a variety of forms using tH, tI, tk, or tl, and the minute within the hour using tM. These can also be combined with tr, which displays tH:tM. Similarly, you can display the day of the week, the name of the month, and so on, using the format conversion keys detailed in the Formatter API. Here is an example that displays the date by formatting the current time using tr for the hour and minute, tA for the day of the week, tB for the name of the month, te for the number of the day of the month, and tY for the year. These could all be preceded by 1$ to point to the first position. Instead, the %tr points to the first position. The < in the other format strings refers back to the position formatted previously. package format; import java.util.Calendar; public class FormattingDates { public static void main(String[] args) { System.out.printf("Right now it is %tr on " + "%<tA, %<tB %<te, %<tY.%n", Calendar.getInstance()); } } Compile and run the FormattingDates program. You will see output that looks something like this: Right now it is 01:55:19 PM on Wednesday, September 22, 2004. This tip is intended to get you started using the new Formatter facility for formatting output. In some ways, the options should feel familiar to you from the old printf() days. Here as before, there are many possibilities available. You will only learn them by trying out different options and deciding which ones meet your needs. For more information about formatting with the new formatter, see the documentation for the Formatter class. |
7.Re:交流一下关于jdk5(1.5) tiger 的新特点哈。。。。 [Re: nothing] | Copy to clipboard |
Posted by: Hanf Posted on: 2004-10-11 17:48 http://java.sun.com/developer/technicalArticles/releases/j2se15/ 还有很多可访问SUN网站获取。 建议大家别急升级,BUG真的多多(虽然J2SE 5.0 (1.5.0) Tiger 老虎 已发行),先关注一下新特性,呵呵。 |
8.Re:交流一下关于jdk5(1.5) tiger 的新特点哈。。。。 [Re: nothing] | Copy to clipboard |
Posted by: savage1984 Posted on: 2004-10-23 17:21 其实我觉得最大的变化是泛型(generics) ,大大简化了我们对类型的控制,还有元数据(metadata),使我们在编写程序时可以动态调用这些注释,来简化我们的操作.... 各位可以上sun的官方网站下jdk5.0的文档来看一下.... |
9.Re:交流一下关于jdk5(1.5) tiger 的新特点 [Re: nothing] | Copy to clipboard |
Posted by: yongl04 Posted on: 2004-10-25 19:55 E文?! 累呀,看这些资料! |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |