Sorry, I forgot one bytecode for the first test, which uses instanceof:
12 aload_1
13 instanceof #10 <Class java.lang.Number>
Of course x has to be pushed onto the stack before we can test if it's a Number.
The response to my challenge brilliantly demonstrated my point about reflection. It's way trickier than it looks! DON'T USE REFLECTION IF AT ALL POSSIBLE!!!
Thank God no idiot modded you up. You couldn't be more wrong. Try running this code:
public class TryIt {
public static void main(String[] args) {
Object x = new Integer(3);
System.out.println(x instanceof Number);
System.out.println(x.getClass() == Number.class);
}
}
Here's the output of running the code:
true
false
Here's the bytecode for the first test:
13 instanceof #10 <Class java.lang.Number>
Now go compile and run that code yourself and use javap -c and look at the bytecode. Then read what I originally wrote again and come back with a more sensible response!
And instanceof is also more powerful than ".getClass().getName().toLowerCase()". It can report whether the class is a subclass of the type you are expecting, not just the exact class. The cast will work in the subclass case, and this is exactly what instanceof tests. The corresponding reflection test would be even more complicated than the code shown.
Anyway, if you want the exact class test, it's better to write "if(x.getClass()==Integer.class)" and compare Class objects instead of dealing with Strings. Either this code or the instanceof code will be more robust and faster than the original code.
Exception handling is not the way to go to handle casting issues, unless it truly is an exceptional or unexpected condition that the cast will fail.
I notice you've put templated code in a.cpp file instead of putting it in the.h file. I always had to put all templated code in the.h file so the compiler can make the proper code generation. It looks like you'll need the compiler to do the code generation because the linker barfs when you ask it to do it.
It's a real shame if this book doesn't explain implementation details like this!
They seemed to me a recipe for bloat/cache thrashing/ugliness.
You have an excellent point. There's a subset of C++ called Embedded C++ that removes templates, multiple inheritance, new-style casts, exceptions, and some other features. Those are features that tend to make C++ programs bigger and slower than C programs. By limiting the features used by a program and its libraries, and Embedded C++ compiler is able to make leaner, faster code. This can be a huge benefit in embedded systems, hence the name.
The nice thing about Java to counter, though, is reflection. You can always check the class type and methods before casting.
It's almost always better to avoid reflection than to use it. In this case, an instanceof test will be easier and faster. Example: Quick, write this code using reflection instead of instanceof:
if (x instanceof Number) {
Number n = (Number) x; //...
}
Now time how much slower it is...
GJ is the old implementation of Java Generics. It has since evolved into JSR-014. Sun has a prototype implementation of a compiler that supports generics, and there's even an entire forum for discussing Java generics.
Ever hear of the punishment fitting the crime? Certainly we can deter crime by giving everyone who breaks any laws outrageous sentences. But the overall effect of sending millions of people who violate minor laws to prison for years is to erode the fabric of society.
Why don't we just secretly pick one area of each city to thoroughly police each day, and execute everyone who commits any crimes in that area? That'll really make people think twice before littering and speeding, won't it? Certainly this would lead to the perfect Utopia! Sheesh!
Where did you get the 95% figure? It's hard for me to find any sites that don't work in Mozilla, and I go to plenty of sites that use JavaScript and DHTML. When I do find a site that doesn't work in Mozilla, it's nearly always very poorly designed and it's just an accident that it happens to work in any browser.
You obviously have no idea how much money it takes to conduct studies that prove the drug safe and effective. This needs to be done before the FDA and other drug agencies approve the drug for use. It can take millions of dollars to conduct just one study, and usually multiple studies are needed to test the safety in kidney patients, the elderly, and the young. If drug companies couldn't make billions of dollars a year for about a decade from "blockbuster" drugs like this, there wouldn't be any drug companies at all, and thus no new drugs.
Just tell him that both PHP and Java are both interpreted languages...
There's no such thing as an "interpreted language". Any language can be compiled or interpreted. Java is first compiled into bytecode, and modern JVMs compile the bytecode into machine code.
A more useful distinction to make is between scripting languages, which usually have an eval feature to evaluate an expression, and programming languages, which usually do not have eval.
Seems like you're way behind the curve. Six years ago the company I worked for teamed up with a company that had been doing this for years. That company was acquired by Nokia in 1999.
It's incidents like this that make me think it really is time to listen to the usability experts. Sure, the extremists we hear about seem to have some crazy ideas, but there must be some out there that have a clue about how we can make interfaces more usable, right?
Funny, I thought the Java Community Process and the Bug Parade were step 2! As a result of feedback, Java has added nested classes, strictfp, generics, enums, foreach, reference objects, tons of library classes, as well as fixed thousands of bugs.
Oh, that brings up a pet peeve of mine -- when people call a matrix a "matricee"! When I hear someone say that word, I roll my eyes and think "this guy has no idea what he's talking about!"
Getting back on topic, maybe the plural for Unix should be Unixen, like the plural for Vax is Vaxen?
Over Christmas and New Years, I helped my wife run a simulation of 1000 different patients for an acedemic pharmacokinetics paper. The run took ten days and had an input file of about 1.5 GB. If her computer was faster, or she had access to more computers, she would have wanted to simulate more patients and would easily have needed support for files larger than 4 GB. As CPUs get faster and hard disks get larger, there will be much more demand for these large files as well as more than 4 GB per process.
12 aload_1
13 instanceof #10 <Class java.lang.Number>
Of course x has to be pushed onto the stack before we can test if it's a Number.
The response to my challenge brilliantly demonstrated my point about reflection. It's way trickier than it looks! DON'T USE REFLECTION IF AT ALL POSSIBLE!!!
public class TryIt {
public static void main(String[] args) {
Object x = new Integer(3);
System.out.println(x instanceof Number);
System.out.println(x.getClass() == Number.class);
}
}
Here's the output of running the code:
true
false
Here's the bytecode for the first test:
13 instanceof #10 <Class java.lang.Number>
Here's the bytecode for the second test:
22 aload_1
23 invokevirtual #12 <Method java.lang.Class getClass()>
26 getstatic #13 <Field java.lang.Class class$java$lang$Number>
29 ifnonnull 44
32 ldc #14 <String "java.lang.Number">
34 invokestatic #15 <Method java.lang.Class class$(java.lang.String)>
37 dup
38 putstatic #13 <Field java.lang.Class class$java$lang$Number>
41 goto 47
44 getstatic #13 <Field java.lang.Class class$java$lang$Number>
47 if_acmpne 54
50 iconst_1
51 goto 55
54 iconst_0
Now go compile and run that code yourself and use javap -c and look at the bytecode. Then read what I originally wrote again and come back with a more sensible response!
Anyway, if you want the exact class test, it's better to write "if(x.getClass()==Integer.class)" and compare Class objects instead of dealing with Strings. Either this code or the instanceof code will be more robust and faster than the original code.
Exception handling is not the way to go to handle casting issues, unless it truly is an exceptional or unexpected condition that the cast will fail.
It's a real shame if this book doesn't explain implementation details like this!
if (x instanceof Number) {
Number n = (Number) x;
}
Now time how much slower it is...
GJ is the old implementation of Java Generics. It has since evolved into JSR-014. Sun has a prototype implementation of a compiler that supports generics, and there's even an entire forum for discussing Java generics.
Ever hear of the punishment fitting the crime? Certainly we can deter crime by giving everyone who breaks any laws outrageous sentences. But the overall effect of sending millions of people who violate minor laws to prison for years is to erode the fabric of society.
Why don't we just secretly pick one area of each city to thoroughly police each day, and execute everyone who commits any crimes in that area? That'll really make people think twice before littering and speeding, won't it? Certainly this would lead to the perfect Utopia! Sheesh!
Where did you get the 95% figure? It's hard for me to find any sites that don't work in Mozilla, and I go to plenty of sites that use JavaScript and DHTML. When I do find a site that doesn't work in Mozilla, it's nearly always very poorly designed and it's just an accident that it happens to work in any browser.
You mean bug 151929? Developers are not "refusing to address" the issue. They have marked that they want help in addressing it, in fact.
Yes. Actually, the new feature is that you can now edit your settings in the about:config window!
F3 or Ctrl-G
It's bug 176715 and should be fixed by Mozilla 1.4: http://bugzilla.mozilla.org/show_bug.cgi?id=176715
No, that's not what "begging the question" means, either. Begging the question is assuming true what you are attempting to prove.
This is completely different from begging the question, which means that in trying to prove something, you assume it's already true.
Both of these are completely different from what the summary author meant, which is "leads to the question."
You obviously have no idea how much money it takes to conduct studies that prove the drug safe and effective. This needs to be done before the FDA and other drug agencies approve the drug for use. It can take millions of dollars to conduct just one study, and usually multiple studies are needed to test the safety in kidney patients, the elderly, and the young. If drug companies couldn't make billions of dollars a year for about a decade from "blockbuster" drugs like this, there wouldn't be any drug companies at all, and thus no new drugs.
A more useful distinction to make is between scripting languages, which usually have an eval feature to evaluate an expression, and programming languages, which usually do not have eval.
Seems like you're way behind the curve. Six years ago the company I worked for teamed up with a company that had been doing this for years. That company was acquired by Nokia in 1999.
It's bug 85464 in Bugzilla. Here's a link to it: http://bugzilla.mozilla.org/show_bug.cgi?id=85464
It's incidents like this that make me think it really is time to listen to the usability experts. Sure, the extremists we hear about seem to have some crazy ideas, but there must be some out there that have a clue about how we can make interfaces more usable, right?
Opera always develops its software for Windows first, then ports it to other platforms. That's why Opera 7 is available only on Windows right now.
Funny, I thought the Java Community Process and the Bug Parade were step 2! As a result of feedback, Java has added nested classes, strictfp, generics, enums, foreach, reference objects, tons of library classes, as well as fixed thousands of bugs.
Getting back on topic, maybe the plural for Unix should be Unixen, like the plural for Vax is Vaxen?
Over Christmas and New Years, I helped my wife run a simulation of 1000 different patients for an acedemic pharmacokinetics paper. The run took ten days and had an input file of about 1.5 GB. If her computer was faster, or she had access to more computers, she would have wanted to simulate more patients and would easily have needed support for files larger than 4 GB. As CPUs get faster and hard disks get larger, there will be much more demand for these large files as well as more than 4 GB per process.