Similar Threads:
1.Which of these is better casting or toString()
List lst = new ArrayList();
lst.add("a");
// more add statements here..
((String) lst.get(j)).toUpperCase()
lst.get(j).toString().toUpperCase();
casting or toString() ?? which one is better to use.
kiri
2.difference between toString() and type casting a n Object to String
what is the difference between toString() and type casting a n Object
to String
3.using ClassDep to reduce rt.jar
i have been trying to use the JINI tool ClassDep to create a list of the
dependencies of a program i have written, but have been unsuccessful. when
i run it i output a list of classes that ClassDeps says my program needs,
but when i create an rt.jar file containing only those classes (yes, in the
appropriate package directories) i get the following:
NoClassDefFoundError: java.lang.ArrayStoreException.
i figured maybe it was a fluke, and just manually included the class.
after doing so i got the following, more confusing error:
error: java.lang.Error: java.io.UnsupportedEncodingException: Cp1252
Cp1252 is not something i specifically make reference to in my program, and
have only a nebulous idea of what it even is/does.
anyway, though, that's beside the point: i would really like to make a
slimmed down version of rt.jar to fit my specific needs according to the
classes that are needed by my app.
is there any way to do this that works, or am i just using ClassDep
incorrectly?
Please Help!
Thank You!
-Voltaic
4.Another question about inheritance (up-casting and down-casting)
Hi all:
Sorry to bother you again:)
Still the question about inheritance,please see this codes below
first:
// codes start
class base{//a base class
// constructor
public base(){
System.out.println("base class construct");
}
// perform
public void perform(){
System.out.println("base class perform");
}
// destructor
public void finalize(){
System.out.println("base class destruct");
}
}
class subbase extends base{// derive from base
// constructor
public subbase(){
System.out.println("sub class construct");
}
// perform
public void perform(){
System.out.println("sub class perform");
}
// destructor
public void finalize(){
System.out.println("sub class destruct");
}
}
public class casting{// test casting class
// constructor
public casting(){
System.out.println("begin casting test");
}
public static void main(String args[]){
base father = new base();
subbase son = new subbase();
father.perform();
son.perform();
father = (base)son; // <1>
father.perform();
son = (subbase)father; // <2>
son.perform();
father = (base)((subbase)father); // <3>
father.perform();
}
}
// codes end
***************************************************
and the execution result is:
// begin
base class construct
base class construct
sub class construct
base class perform
sub class perform
sub class perform // <a>
sub class perform // <b>
sub class perform // <c>
// end
****************************************************
My question is: there are 3 castings in the codes(e.g. <1><2><3>),but
why they perform not as I like, I cast the subclass son to base class
at <1>, but at <a>,father.perform() don't print "base class perform",
the situation is similar in <2><a> and <3><c>,so, why?
****************************************************
And I am confused about how "casting" behave?
for example:
base class: sub class derive from base
+---------------+ +------------------------------+
| int nbase | | int nbase int nsub |
----------------- --------------------------------
| void fbase() | | void fbase() void fsub() |
+---------------+ +------------------------------+
so, if I declare:
base a = new base(); // a should contains nbase and fbase()
sub b = new sub(); // b should contains nbase,nsub and
fbase(),fsub()
base c;
sub d;
c = (base)b; // ? what c contains ? like a or like b
// that is to say if son class upcasting to father
class
// if the member belongs to son class should be
truncated?
d = (sub)c; // ? what d contains ? like a or like b
// that is to say if d get the members belongs to son
class
// automatically
// d == b ? i mean if equal in memory profile
*******************************************************
I am a little confused about how memory allocation changes when
upcasting
or downcasting ?
that's all
thank you!
kevin
5.Restricting package access when using reflection/casting
Here's a scenario.. I have the following interface:
===============================
package com.mypublicpackage;
public interface MyInterface {
public void foo();
}
===============================
and the following class:
===============================
package com.myprivatepackage;
public class MyClass implements com.mypublicpackage.MyInterface {
public void foo() {}
public void bar() {}
}
===============================
I have a method in another class that returns a reference to
MyInterface, eg:
package com.mypublicpackage;
public class MyImplementation {
public MyInterface getMyInterface() { return new
com.myprivatepackage.MyClass(); }
}
I want to protect applications (loaded with my class loader) from
accessing anything directly in com.myprivatepackage. So if I call
MyImplementation.getMyInterface() then I should NOT be able to cast
MyInterface to MyClass or use reflection to call MyClass.bar() without
getting an AccessControlException etc.
I have my own SecurityManager and Policy implementation so is it just a
question of adding "com.myprivatepackage." to the package.access line
in java.security or is there more to it than that?
Thanks,
Stu
6. Getting class using (Class.forname()) and casting the instance
7. How to reduce startup latency while playing .wmv file - from Javas
8. Tuning a machine to reduce run time.