Jump to content

Tips on how to not be bad


Sigma

Recommended Posts

thisIsCamelCase

 

You use the above syntax to denote a field that is not final, examples:

int numOne;
Object objOne;

public protected private abstract static final synchonized

 

The above are modifiers you use before defining a field or method, examples:

public static final String NAME;
protected void method1() { }

That is the order you should be putting them in, omitting any unused modifiers. You'll probably never use transient, volatile, or native modifiers, so they won't be included in the above order.

 

 

byte short int long float double boolean char

 

The above are primitive data types that you are able to compare equality with the == operator, examples:

int numOne = 1;
int numTwo = 2;
public static void main(String[] args) {
    System.out.println(numOne == numTwo);
    //If you run the above code, it will print out as 'false'
}

ClazzName

 

The above is the correct syntax for class names; the first letter of each word should be capitalized, examples:

class TestClazz { }
class TipsOnHowToNotBeBad { }

I_AM_A_FINAL_FIELD

 

The above is the correct syntax for defining a final field, examples:

int MAX_VALUE = Integer.MAX_VALUE;
int MIN_VALUE = Integer.MIN_VALUE;

.equals(Object obj)

 

The above is a method that is inherited from the Object class. It is used to compare two objects for equality (do not use the == operator, as stated in a previous lesson above it is for primitive data types only), examples:

String cheese = "Cheese";
String cheese2 = "Cheese";
 
public static void main(String[] args) {
    System.out.println(cheese.equals(cheese2));
    //If you run the above code, it will print out as 'true'
}

public void method1() {

 

}

 

public void method2()

{

 

}

 

public void method3(){

 

}

 

The above are three different ways to format your braces in java. All 3 are correct, but you should never mix one with another. 

 

 

if (booleanValue)

    method1();

 

The above is an if statement that has the braces omitted. You should only do this if there is only one line of code that is executed inside the if statement (note that you can also do this with for and while loops).

 

 

int numOne;

String stringOne;

 

public Clazz(int numOne) {

    this(numOne, "Default);

}

 

public Clazz(int numOne, String stringOne) {

    this.numOne = numOne;

    this.stringOne = stringOne;

}

 

The above is an example of Constructor overloading. In the first constructor, you are only required to pass in an int, and the String field is automatically assigned whereas in the second constructor you are required to provide both an int and a String.

 

 

If you learned anything from this, congratulations, you're less bad than you were before.

Edited by Sigma
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...