Introduction to Java


Java is an object oriented Programming (OOP) developed by Sun MicroSystems and it was released in the year 1995. It was acquired by Oracle Corporation in 2009. Java runs on a variety of platforms that ranges from Mac OS, UNIX and Windows.

Features of Java
It is object oriented.
It is secured.
It runs on various platforms.
It is portable and dynamic.

Java programs is a collection of objects that communicate via invoking each other’s methods.
Objects is an instance of a class. It has states (fields) and behaviours (methods).
Class is a template that describes the behavior and state that the objects of its type supports.
Methods are where the logics are written, data is manipulated and all actions are executed.
Java Identifiers are names used for classes, methods and variables. Java Identifiers must begin with letter A to Z or a to z, currency character ($) or an underscore (_).
Constructor: Every class has at least one constructor. A constructor of a class must have the same name as the class. A class can have more than one constructor.

Basic Syntax of Java
Java is case sensitive. It means Song and song are not the same because the Song starts with an uppercase and the other with a lower case.

Methods in Java must start with a lower case. If several words are joined together to make a word, the first letter will be in lowercase and the first letter of subsequent words will be in upper case. Examples include: public void goToMarket(), private annualSalaryPayment(), protected void onCreate() etc.

Classes in Java must start with an upper case unlike methods. If several words are joined together to make a word, the first letter will be in uppercase and the first letter of subsequent words will be in uppercase. Examples include: public class BasicSalary, private static AggregateScore etc.

Variables are reserved memory locations

Java keywords are reserved words in Java. They are words that cannot be used as constant or variables.
Comments in Java
// example of single comment
/*another example of single comment */
/*
*Example of multi comment
*in Java
/




1. Package com.android.rexben.lagos is the package name of my Application Lagos. It is where all your java classes are stored. You can have as many classes as possible in a package. It is the way of categorizing classes and interfaces.
2. Import statement – In Android Studio and Java, you don’t need to write all the classes or methods. So, all you need to do is to import them. Any class you import will be listed in that spot.
3. Public class MainActivity extends AppcompatActivity – I will pick the term one after the other.
Public is an access modifier that determines whether a class or method can be accessed by other classes or methods class or method or only within its c. When a method is declared public, it means it can be accessed by any class or method. Other Access Modifiers include protected, private, default, abstract etc.
Class is where all your java codes are written. Where your methods are written and implemented. Class is a compartment that house methods and states.
MainActivity is the name of your java class.
Extends is used when you want to inherit some methods and fields from another class. You extend the features of another class (super class) to a new class (sub class). 
AppCompatActivity is a class that the MainActivity is inheriting from. AppCompatActivity  ….
4. This is the entry of your App as public static void main (Striing args[]) is the entry when use Netbeans or Eclipse.

Data Types in Java

There are two data types in Java
Primitive Data types
Reference or Object Data types
Primitive Data Types
There are eight primitive data types. They are usually in lower cases. They include byte, short, int, long, float, double, boolean and char. They are used to save memory.
Byte: Byte data type is an 8-bit signed two’s complement integer. The minimum value of byte is -128 (-27) and the maximum value is 127 (2-1), the default value is 0.
Examples include: byte score = 99;
                     byte count = -3 etc.
Short: Short data types is a 16-bit signed two’s complement integer. The minimum value is -32, 768 (-215) and the maximum value is 32, 767 (215 -1), the default value is 0. 
Examples include: short score = 32, 000;
                    short count = -20, 989 etc.
Int: Int data type is a 32-bit signed two’s complement integer. The minimum value of an int is -2,147, 483, 648 (-231) vand the maximum value is 2, 147, 483, 647 (231 -1), the default value is 0. 
Examples include: int score = 1, 150, 000;
                    Int count = -2, 760, 444
Long: Long data type is a 64-bit signed two’s complement integer. The minimum value of (-263) and the maximum value is (263 -1), the default value is 0L
Examples include: long score = 200 000L;
                    Long count = -100 000L
Float: Float is a single-precision 32-bit IEEE 754 floating point used to save memory in large arrays of floating points number. Default value is 0.0f
Examples include: float score = 234.5f;
                    float count = -11.4f.
Double: Double is a double-precision 32-bit IEEE 754 floating point, used as the default data type for decimal values. Default value is 0.0d.
Examples include: double score = 123.4;
                    double count = -120.4.
Boolean: Boolean represents one bit of information. There are only two possible vaues: true and false. Default value is false.
Examples include: boolean isScore = true;
                     boolean isCount = false.
Char: Char is a single16-bit Unicode character. It is used to store any character.
Examples include: char letterA = “A”;
                    char letter  = “V”

Reference Data Types 
They are created using defined constructors of the classes. They are declared to be a specific type that cannot be changed. Class objects and various types of array are examples of reference data types. Default value is null.

Variable types
There are basic three types of variable in Java
Local Variables are variables that are declared and initialized inside methods or constructors.
Instance Variables are variables that are declared outside any methods or constructors but within a class. It can be accessed from inside any methods or constructors of that particular class. They are also referred to as fields.
Class Variables are variables declared outside any methods or constructors but within a class with the static keyword. The difference between a class variable and an instance variable is the static keyword.


Creating an object in the java
In Java, an object is created from a class and it created with the new keyword.
There are three steps when creating an object from a class.
Declaration: A variable name with an object type e.g. String score;
Instantiation: The new keyword is used to create a class.
Initialization: The new keyword is followed by a call to a constructor e.g. String score = new String(};