|
 |
|
 |
 |
|
08-04-2003, 06:08 PM
|
#1 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Java tips and tricks
Here's a thread to post all the neat little tricks you know about in Java. Some may be well documents, some not so well.
|
|
|
08-04-2003, 06:13 PM
|
#2 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Ever have a collection you wanted to turn into an Object array? This one is well documented in the API, but to my suprise a lot of younger students at my college don't know how to properly do it. So here's an example with ArrayList (and if you're using Vector; Bad programmer, bad programmer!):
/* Assume there is an ArrayList called "list" */
String[] array = (String[])list.toArray(new String[0]);
That's it in one line. The toArray method returns an Object array that can be cast, provided you pass an instance of an array of the type you want to cast to. No more for loops!
|
|
|
08-04-2003, 06:20 PM
|
#3 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Ever wanted to give users a more explicit explaination for exceptions? Sure, you could just throw a new exception with the message, but you'd loose the stack trace associated with the original exception. Here's how you can have you cake, and eat too.
try{
// some code that throws an exception
}catch(Exception e){
Exception ex = new Exception("Something bad happened");
ex.setStackTrace(e.getStackTrace());
throw ex;
}
Just that simple!
|
|
|
08-04-2003, 06:29 PM
|
#4 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
When declaring methods that return an array, consider returning an array of size 0 instead of places where you'd return a "null". This will help prevent runtime NullPointerExceptions when someone tries to reference an element in it, or even more common, "length".
|
|
|
08-04-2003, 06:31 PM
|
#5 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
If you can't come up with a reason to use Vector other than "Someone told me to use it," or "Why not?" you shouldn't be using Vector. Use ArrayList.
|
|
|
08-04-2003, 07:31 PM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
hey thanks! great thread. i'm learning c# and now i can see why they say it's M$' version of java =)
i'm going to now learn java long with c#.
__________________
testing 1 2 3
|
|
|
08-04-2003, 07:39 PM
|
#7 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
I really hope you've discovered the greatest seceret about Java, it's documentation. One thing I've learned about Java is that those fat books about Java you see in book stores? Complete waste of money, the say almost the exact same thing that come with the API. No other language I know of is as well documented online as Java. The libraries that come included, the language spec, and ten million tutorials are all located on Sun's website.
Bookmark the API and don't look back.
|
|
|
08-04-2003, 08:03 PM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
yeah, even your earlier posts and signature got me on the bandwagon. i program for a company in the music industry, and although we have a windows network, we have many mac users within the company. it would be nice to be able to build a single solution for both platforms.
__________________
testing 1 2 3
|
|
|
08-04-2003, 08:19 PM
|
#9 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Great to hear! What exactly is the nature of your programming?
|
|
|
08-04-2003, 08:22 PM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
call center to log tech support calls integrated with online help desk and support, .. online education/testing systems, .. those are the main projects, .. but then many other little apps that interface with various db's.
most of it until now have been web based solutions .. that has worked great for multi-platform users, but i want to step it up to windows & mac applciations now.
__________________
testing 1 2 3
|
|
|
08-04-2003, 08:49 PM
|
#11 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Java is great for web-based solutions, but Java on Mac is weird. Sun doesn't publish the JVM for Mac, Apple does. It's specially tuned for Mac, and as a result tends to lag behind the reference JVM Sun puts out.
|
|
|
08-04-2003, 09:05 PM
|
#12 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
thanks a lot, that is great information. i have a co-worker who is diving into java on mac as well. his main computer is a mac.. i'll forward the information ..
he's actually a member here, maybe we can get him to post some more lol
thanks again,
__________________
testing 1 2 3
|
|
|
08-05-2003, 03:28 PM
|
#13 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
"i want to swap two integers without creating a temp one":
(oldest trick in the book, but i like it)
Code:
int a = 4, b = 6;
a = a ^ b;
b = a ^ b;
a = a ^ b;
"i want to run a class without using 'static void main(String[] args)'":
Code:
class Okay {
static {
System.out.println("Hello static world.");
System.exit(0);
}
}
"i want to call a method with the string vesion of its name":
Code:
import java.lang.reflect.*;
class RefTest {
public static void main(String[] args){
RefTest reftest = new RefTest();
reftest.doit();
}
public void doit(){
String methodToCall = "reflectionTest";
Method mymethod;
try {
mymethod = getClass().getDeclaredMethod(methodToCall, null);
mymethod.invoke(this, null);
} catch(Exception e){
System.err.println("oops:" + e);
}
}
public void reflectionTest(){
System.out.println("Called");
}
}
__________________
direct entry file specification.
|
|
|
08-05-2003, 04:16 PM
|
#14 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
"i want a faster way to divide when my numbers are powers of 2":
Code:
int a = x / (power of 2) // is equiv to
int a = x >> sqroot(power of 2)
//so:
int a = 10 / 4;
// becomes
a = 10 >> 2;
int b = 5412 / 16
// becomes
b = 54112 >> 4
same goes for multiplying except use "<<".
the ">>" is a faster operation than "/" (in c++ at least)
"i want a faster way to get the 'mod' of a number when it is a power of 2":
Code:
int a = x % (power of 2)
//becomes
a = x & (power of 2) - 1
int a = 56 % 8
// becomes
a = 56 & 7
// "&" is a faster operation than "%"
__________________
direct entry file specification.
|
|
|
08-05-2003, 04:28 PM
|
#15 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
"i keep getting negative byte values like -45 etc, why
aren't they positive.. help!":
Code:
// java bytes are signed, to unsign them:
byte b = -34;
int unsigned_b = b & 0xff;
__________________
direct entry file specification.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:37 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|