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-05-2003, 04:34 PM   #16 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"i accidently get NullPointerExceptions when checking
what strings equal, how can i avoid it easily?":

Code:
String a = null; if(a.equals("Hello")){ // oops exception! } // this may be solved with: if(a != null && a.equals("Hello")) // but you can just do: if("Hello".equals(a)){ // and avoid checking null at all.
It is good practice to put the constant value on the
left-hand-side so accidents dont occur in other languages
(javascript, c, c++) where they evaluate assignment to
be either true or false and hence a valid expression in
an "if" statement.
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 08-05-2003, 09:00 PM   #17 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Wow, some of that is real hacker stuff. But who wouldn't want to use main()?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-05-2003, 09:07 PM   #18 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
well that was just my of showing that main() isn't
needed as the jvm loads and runs any "static" blocks
before anything else (including main) so if you
feel like it you do not need to use him (main).

generally you use static blocks for loading
things (i use it to connect to the database),
other people use it to load tables of constants:

Code:
private static final int POWERS_OF_2[]; static { // // // // for(int k = 0; k < 32; k++){ POWERS_OF_2[k] = Math.pow(k, 2); } } // // now we can referrence powers of two faster // than trying to calculate them // }
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 08-06-2003, 12:18 AM   #19 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"i want to count the number of set-bits in an interger!":

(can't take credit for this one):
Code:
public final static int getBits(int i) { i = ((i & 0xaaaaaaaa) >> 1) + (i & 0x55555555); i = ((i & 0xcccccccc) >> 2) + (i & 0x33333333); i = ((i >> 4) + i) & 0x0f0f0f0f; i += i >> 8; i += i >> 16; return i & 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 06:45 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