Things to Know Part 1

Main Method:

        -Must be inside a class

        -Looks like:

                public static void main(String[] args){}

                        or

                public static void main(String… args){}

        -The parameter does not have to be named "args". It can be any name that fits Java's variable naming rules.        

Variable Naming Rules:

        -Has to start with a letter, $, or _

        -Can contain numbers

Packages and Imports:

        -Package declaration must come first if not in the default package.

                package my_package;

        -Imports must come after package declaration (if there is one) but before the class declaration.

        -Classes can be imported.

                import my_package.MyClass;

        -Packages can be imported

                import my_package.*;

        -Cannot import subdirectories with *.

Constructors:

        -Method without a return type.

        -Always has same name as class.

        -Can be overridden to have multiple constructors as long as parameters are different.

Numeric Literals:

        -Starts with 0 - 9.

        -0x or 0X means hexadecimal

                0x19 = 25

        -0b or 0B means binary

                0b101 = 5

        -Can have _ in number as long as it is not at the beginning or end, or attached to  a decimal point. Also must be separated by a number

                OKAY:

                        int x = 1_000;

                        double y = 1_1.0_1;

                BAD:

                        int x = 1__000;

                        int y = _1_000;

                        float z = 1_.001;

Garbage Collection:

        -Any object that does not have a reference pointing to it is eligible to be garbage collected.

                void someMethod(){

                        Random r = new Random();

                } //<--r goes out of scope and the object it points to will be eligible for garbage

                          //    collection

                Random s;

        

                void someOtherMethod(){

                        Random r = new Random();

                        s = r;

                }//<--r still goes out of scope, but the object it points to has s pointing to it and so

                         //    collection the object is not eligible for garbage

Variable Scope:

        -Any variable created in brackets {} can only be used in those brackets. Brackets {} define a new scope.

                //package declaration

                package my_package;

//optional but must come first                

                //imports

                import my_package.TestOne;

                import my_package.sub_pkg.*;

                //optional but must go after package declaration (if there is one)

                        //and must come before the class declaration

                

                //class declaration

                public class MyClass {

                        // class variable

                        public static int x = 5;

                        //deleted when program ends

                        //member variables get initialized automatically as follows:

                        //boolean initializes to false

                        //byte, short, int, long, char initialize to 0

                        //float, double initialize to 0.0

                        //objects, Strings initialize to null

                        //instance variable

                        private int y = 2;

//deleted when object is garbage collected

        

                        //constructor

                        public MyClass(){

                                //local variable

                                int z = 4;

                                //deleted at closing bracket }

                        }

source:

Boyarsky, Jeanne, and Scott Selikoff. OCA Oracle Certified Associate. Study Guide. John Wiley & Sons, 2015.