Skip to content

Instantly share code, notes, and snippets.

@ismaeltoe
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismaeltoe/935531876a56f11162fa to your computer and use it in GitHub Desktop.
Save ismaeltoe/935531876a56f11162fa to your computer and use it in GitHub Desktop.

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.

If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.

Kinds of variables:

  • Instance Variables (Non-Static Fields) Their values are unique to each instance of a class.
  • Class Variables (Static Fields) The same value for all instances.
  • Local Variables They are only visible to the methods in which they are declared.
  • Parameters

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign, or the underscore character. The convention, however, is to always begin your variable names with a letter, not $ or _.

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

float and double should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.

Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type.

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int.

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. For instance, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

The first letter of a class name should be capitalized, and the first (or only) word in a method name should be a verb.

Methods within a class can have the same name if they have different parameter lists. Overloaded methods are differentiated by the number and the type of the arguments passed into the method. The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked.

You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name.

The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

When a method uses a class name as its return type, the class of the type of the returned object must be either a subclass of, or the exact class of, the return type.

You also can use interface names as return types. In this case, the object returned must implement the specified interface.

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment