should avoid creating object instance you don't need to. Ex- use String result = ""; for (String s : hugeArray) { result = result + s; } instead of StringBuilder sb = new StringBuilder(); for (String s : hugeArray) { sb.append(s); } String result = sb.toString();
than an array of Integer objects. • two parallel arrays of ints are lot more efficient than int[][] • use class Container{ Object a, Object b } Container [] container = new ...... instead of Container{ Object[] a; Object[] b; }
static String strVal = "Hello, world!"; The compiler generates a class initializer method, called <clinit>, that is executed when the class is first used. The method stores the value 42 into intVal, and extracts a reference from the classfile string constant table for strVal. When these values are referenced later on, they are accessed with field lookups. We can improve matters with the "final" keyword: static final int intVal = 42; static final String strVal = "Hello, world!";
} Foo[] mArray = ... public void zero() { int sum = 0; for (int i = 0; i < mArray.length; ++i) { sum += mArray[i].mSplat; } } public void one() { int sum = 0; Foo[] localArray = mArray; int len = localArray.length; for (int i = 0; i < len; ++i) { sum += localArray[i].mSplat; } }
= Integer.parseInt(value); } catch (NumberFormatException e) { } } void setServerPort(String value) { try { serverPort = Integer.parseInt(value); } catch (NumberFormatException e) { throw new RuntimeException("port " + value " is invalid, ", e); } } Anytime somebody has an empty catch clause they should have a creepy feeling. There are definitely times when it is actually the correct thing to do, but at least you have to think about it. In Java you can't escape the creepy feeling. - James Gosling
IOException someComplicatedParsingFunction(); // may throw ParsingException someComplicatedSecurityFunction(); // may throw SecurityException // phew, made it all the way } catch (Exception e) { // I'll just catch all exceptions handleError(); // with one generic handler! }
be meaningful, methods name should be like what they do, for say calculate() which perform some calculation. And naming should be in English spelling. Naming should be avoid abbreviations. Should use meaningful name while naming class name, variable name, constant name. Like - class name → AddressDetails method name → addAddressDetails variable name → username constant name → DEAFULT_VALUE
clients etc Case : Java is case sensitive. So username and Username is different. But it should no use same name that differs only in case. Package name: package name must be lower case. Like → iit.bit.rokon.com Class name: class name must be start with upper case. Class name should be noun. Class name can contain multiple words. This case they are linked like → AddressDetails. Every word should start with upper case letter. Interface name: interface name should be like class name. And should use none or adjective while naming interface.
first letter of each subsequent word that appears in a method name. And method name should be verb. In the case of accessor method, we should use JavaBean convention. Like getter and setters. Example - class User{ private String username; public void setUsername(String username){ this.username = username; } public String getUsername(){ return username; } } Variable name: Variable name should be noun and should be start with lowercase and capitalize first letter of each subsequent word that appears. Constant Name: Constant name should use uppercase letters for each word and separate each pair of words with and underscore. Example - public static final int DEAFULT_VALUE = 1;
in naming variables, methods, and classes. The names are much more readable: Good Bad XmlHttpRequest XMLHTTPRequest getCustomerId getCustomerID class Html class HTML String url String URL long id long ID
HTTP requests, handle responses in anonymous callbacks • HTTP requests happen outside the UI thread • Requests use a threadpool to cap concurrent resource usage • GET/POST params builder (RequestParams) • Multipart file uploads with no additional third party libraries • Tiny size overhead to your application, only 25kb for everything • Automatic smart request retries optimized for spotty mobile connections • Automatic gzip response decoding support for super-fast requests • Binary file (images etc) downloading with BinaryHttpResponseHandler • Built-in response parsing into JSON with JsonHttpResponseHandler • Persistent cookie store, saves cookies into your app’s SharedPreferences