Best Practices to Be Followed in JAVA

Tips and Tricks & General Questions

Harsh Gupta - Tech Writer
If you want to be a good programmer in Java, then you need to follow certain conventions and you should be aware of some of the common things if you are a beginner. Following such conventions are a good sign of a Java programmer.

Declaring Class Variables as private and Methods as public

You should declare all the class variables as private because data should always remain hidden from objects of other classes. Methods should be declared as public because the methods provide an access to the objects of the other classes.

Declaring Arrays?

Arrays can range from a higher minimum number to a higher maximum number, such as 45 to 90. It is more efficient to begin arrays with an element 0 because it takes up less memory for the compiler to store arrays that begin with 0.

If you write a Java program having the for loop that starts from array element 45 to element 90, compiler will still allocate the memory to the array starting from elements 0 to array element 45

Declaring Class Variables

You can follow Hungarian notation to declare variables in Java. The conventions followed in the Hungarian notation are as follows:

The first letter of the variable should indicate the data type used. You can use the letters, b to indicate an integer, float, or a boolean variable. For example, iAge, fPrlce, and bRest

The variable name should be meaningful. For example, iAge is an integer variable to store age of a student.

In case the variable name consists of multiple words, the first letter of each word should capital, for example iTotalMarks and fPriceOfCommodity.

Some Tips and Tricks

Displaying Text in a Java Program

You can also use the drawstring() method to display text in addition to the System.out.println method in Java. The drawstring () method require 3 arguments. The first argument represents string to be displayed on an applet. The second and third arguments represent the x and y coordinates where the string is to be displayed.

Setting CLASSPATH

Classpath is an environment variable that enables the Java compilerjavac.exe to locate class imported. You can use the set classpath= to set the classpath variable. The following syntax shows how to set classpath in Java:

set classpath= c:jdk1.5in

General Questions asked by users

Why Java is considered ideal for network communication?

Java is considered ideal for network communication because it is a platform wide pendent I Java enables an application to be executed on any network. The built-in classes of Java and TCP/IP and UDP protocols are used for network communication. Java supports the Client-Server architecture.

What will happen if the data type of a variable and the value assigned to the variable are different?

If the data type of a variable and the value assigned to the variable are different then compilation error occurs. If an Integer variable is assigned a character value, the ASCII value of the assigned character is displayed as an output. The following code shows assigning a character value to an Integer variable.

Class datatype

{

public static void main(String a[])

{

int x='b';

System.out.println (x);

}

In the preceding code, the ASCII value of the character b is displayed as 98. Similarly, if you assign a float or a double value to a character variable, an error message is displayed. The following code shows assigning a double value to a character variable:

Class datatype
{
public static void main(String a[])
{
char X= 5.5;
System.out.print(x);
}

Java is a strongly typed language and it allows the values of the specific data types, which are compatible with the type of variable.

Is it correct that all arguments sent to Java application have to be strings?

Yes, all the arguments sent to a Java application have to be string. If you use some other data type, such as int, you need to convert the value to string.

Can two variables have the same letters but different capitalization, as In varlableName and VarlableName ?

No, Java is a case-sensitive language. It differentiates the two variables, variableName and VariableName.

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • Java is case-sensitive language. It differentiates the two variables, variableName and VariableName
  • You can also use the drawstring() method to display text in addition to the System.out.print method
You should declare all the class variables as private because data should always remain hidden from objects of other classes. Methods should be declared as public because the methods provide an access to the objects of the other classes.

To comment, please sign in to your Yahoo! account, or sign up for a new account.