使用Eclipse打包Java工程成为Jar

Categories: Java; Tagged with: ; @ February 12th, 2009 15:36

1. Eclipse中右键 Export,

2. 选择Java> Runnable JAR File

3. 选择Launch configuration及输出目录,

finish.

继续理解Java基本数据类型, 直接数, Boxing/UnBoxing

Categories: Java; Tagged with: ; @ February 5th, 2009 17:23

Java Types:

Primitive Types: boolean, char, byte, short, int, long, float, double.

Reference Types

直接数:

整数, 浮点数, 单引号括起来的char, 双引号括起来的String, true, false, null;

所有的Primitive Types 在java.lang中都有对应的包装类. 如下:

int i = 1;

使用直接数赋值, 可理解为直接在栈区内使用int, 没有任何Object的创建. 而如果使用new int()进行创建, 则会创建额外无用的Object.

int i = new Integer(1);

在经过JVM编译后, 等同于下:

int i = (new Integer(1)).intValue();

此举将创建一个int的Object, 并将这个Object的intValue()传给i. 此后该对象将废弃不用,等待垃圾回收.

因此, 在多数情况下, 应使用直接数赋值.

	private static Integer getInt() {
		return 1;
	}

	private static int getInitP() {
		return new Integer(1);
	}

以上代码在编译之后的Class文件反编译后的代码, 可立即理解Boxing 与Unboxing :

    private static Integer getInt()
    {
        return Integer.valueOf(1);
    }

    private static int getInitP()
    {
        return (new Integer(1)).intValue();
    }

Java – Final修饰符到底修饰了什么

Categories: Java; Tagged with: ; @ February 5th, 2009 15:09

Final 修饰符在修饰基本数据类型时, 能保证其值不被改变, 但在修饰对象时, Final保证引用本身不变, 而不能保证引用对象的不变.

如下:

		
		final StringBuilder sb = new StringBuilder("Final SB");
		System.out.println(sb.toString());
		StringBuilder sb2 = new StringBuilder(" USB");
		//sb = sb2 //试图修改sb的引用. 编译出错, final变量不可被更改
		sb.append(sb2); //修改sb引用对象, final不能保证引用对象不被改变. 编译通过.
		System.out.println(sb.toString());

输出:

Final SB

Final SB USB

Java String小结

Categories: Java; Tagged with: ; @ February 5th, 2009 0:25

String

String对象是不可改变的.

String s1 = “s”;

改语句声明了一个指向对象的引用, 名为s1, 目前指向一个String对象, 其内容为”s”;

String s2 = “s”;

上一句则声明一个指向”s” 的引用 – 目前Sun的JVM会对此过程进行优化, 会将s2指向s1所指向的对象. 而非创建新的对象.

String s3 = s2;

声明了一个引用, 改引用指向s2所指向的String对象.

String s4 = new String(“s”);

声明一个引用s4, 同事通过String类创建String对象, s4指向该对象.

通过new String()函数创建String, 每次都会创建一个String对象,相比使用直接数, 效率低.

== 与 equals

首先需要明确:

在Java中, “==” 用于判断两个对象是否相同 – 可以理解为引用了相同的内存地址

而equals()则是Object类中的一个方法, 在 具体的类中有着具体的实现.

在String中, equals被冲写为:

    public boolean equals(Object anObject) {
	if (this == anObject) {
	    return true;
	}
	if (anObject instanceof String) {
	    String anotherString = (String)anObject;
	    int n = count;
	    if (n == anotherString.count) {
		char v1[] = value;
		char v2[] = anotherString.value;
		int i = offset;
		int j = anotherString.offset;
		while (n-- != 0) {
		    if (v1[i++] != v2[j++])
			return false;
		}
		return true;
	    }
	}
	return false;
    }

可见, 如果两个String”==” – 即相同, 则equals直接返回true. 如果不同 则先比较长度 , 再逐字比较. 一般来说, 在重写equals时也应重写hashCode.

在String中:

使用直接数赋值时,  具有优化作用的JVM会先搜索是否已经存在内容相同的Stinig对象, 如果存在, 则返回一个引用, 如果不存在则先创建,再返回引用.因此如果使用new String()进行操作, 则将丧失JVM优化的作用.

因此,

String s1 = “s”;

String s2 = “s”;

是相同的.

而使用String s1 = new(“s1”); 时, 将会创建一个新的String对象, 因此

String s1 = “s”

String s2 = new String(“s”);

是不同的, 但是相等的.

因此有:

		String s1 = "s";
		String s2 = new String("s");
		String s3 = "s";
		String s4 = new String(s1);
		System.out.println("s1 == s2 - " + (s1 == s2));
		System.out.println("s1.equals(s2)) - " + s1.equals(s2));

		System.out.println("s1 == s3 - " + (s1 == s3));
		System.out.println("s1.equals(s3) - " + s1.equals(s3));

		System.out.println("s4 == s1 - " + (s4 == s1));
		System.out.println("s4 == s2 - " + (s4 == s2));

输出信息如下:
s1 == s2 – false
s1.equals(s2)) – true
s1 == s3 – true
s1.equals(s3) – true
s4 == s1 – false
s4 == s2 – false

另外:

String s1  = “s1”;

String s2 = s1;

s1 = “ss”;

执行完之后 , s2 内容为”s1″, s1的内容为”ss”;

表明第二行代码中, s2 引用了 s1指向的String地址, 此后s2与s1再无关系.

Java Decompiler Java Class查看器下载

Categories: Java; Tagged with: ; @ February 4th, 2009 16:39

有Windows GUI, Eclipse, IntelliJ插件, 详见: http://java.decompiler.free.fr/

The “Java Decompiler project” aims to develop tools in order to decompile and analyze Java 5 “byte code” and the later versions.

JD-Core is a library that reconstructs Java source code from one or more “.class” files. JD-Core may be used to recover lost source code and explore the source of Java runtime libraries. New features of Java 5, such as annotations, generics or type “enum”, are supported. JD-GUI and JD-Eclipse include JD-Core library.

JD-GUI is a standalone graphical utility that displays Java source codes of “.class” files. You can browse the reconstructed source code with the JD-GUI for instant access to methods and fields.

JD-Eclipse is a plug-in for the Eclipse platform. It allows you to display all the Java sources during your debugging process, even if you do not have them all.

JD-Core, JD-GUI and JD-Eclipse are free for non-commercial use. This means that JD-Core, JD-GUI and JD-Eclipse shall not be included or embedded into commercial software products. Nevertheless, these projects may be freely used for personal needs in a commercial or non-commercial environments.

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.