Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 08-04-2003, 06:08 PM   #1 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 06:13 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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!
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 06:20 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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!
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 06:29 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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".
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 06:31 PM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 07:31 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-04-2003, 07:39 PM   #7 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 08:03 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-04-2003, 08:19 PM   #9 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Great to hear! What exactly is the nature of your programming?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 08:22 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-04-2003, 08:49 PM   #11 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2003, 09:05 PM   #12 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-05-2003, 03:28 PM   #13 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"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.
npa is offline   Reply With Quote
Old 08-05-2003, 04:16 PM   #14 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"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.
npa is offline   Reply With Quote
Old 08-05-2003, 04:28 PM   #15 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"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.
npa is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:37 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle