Monday 7 September 2015

Java – Basic Syntax

Every Java program is made up of a collection of objects that can communicate to each other. Below is a brief description of what the fundamental parts of a Java program do.
Class – Classes contain Variables and Methods. Think of a Variable as something that can hold a value. A class is a blueprint for creating Objects.
Methods – A Method, also known as a function, is like a behavior. A class can contain lots of Methods. Within each method logic, which is just code that will be executed when the Method is called.
Object – An Object is an instance of a class. When Objects are created they are using the information created from a particular Class.
Instance Variables – Each object that is created has a unique set of Instance Variables. These variables come from the class that the object was instantiated from.

Java Basic Syntax:

When programming it is good to keep naming conventions universal across all projects. These conventions are good practice and should be followed at all times, however are not syntactically incorrect if done differently.
Class Names – The first letter in all class names should be uppercase. If several words are used for a class name, each inner words first letter should be uppercase. This is also known as camel case. (Example: MyAwesomeClass)
Method Names – The first letter in all method names should be lowercase. If several words are used for the methods name, each inner word’s first letter should be uppercase. (Example: myAwesomeMethod).

The following naming conventions will cause compile time errors if incorrect and must be strictly followed.
Case Sensitivity – Java is case sensitive. This means that example and Example are completely different as far as Java is concerned.
Program File Names – The name of the program file should exactly match the Class name. When saving a file, you should always save it as the class name, followed by the extension (.java). (Example: If you have a class called ExampleClass, you would save this file as ExampleClass.java).

Let’s briefly look at how a program knows where to start running. What happens if our program has 10 different classes? How does it know where to start?
public static void main(String[] args) – This is a method that every Java application must implement. There can only ever be one of these methods inside a Java project, and it is the first method called when you program is executed. In later tutorials you will have a better understanding of what exactly each part of that method statement means.     
          
Modifiers:
In Java it is possible to place modifiers on classes, methods, variables, etc. There are two main types of modifiers.
                Access Modifiers: default, public, protected, private.
                Non-access Modifiers: final, abstract, strictfp.
In the next tutorial we will look into more detail of what these modifiers mean.

Variables:
Here are some of the main variable types inside of Java. These variable types are different from data types, these are to do with the scope (who can access) of the variables.
                Local Variables
                Class Variables (static variables)
                Instance Variables (non-static variables)
  
Comments
Java allows for single line and multi-line comments, which are very similar to c++. Any characters that are placed inside a comment are ignored by the compiler and will not be executed at runtime. Comments are a good way to explain how parts of your code work and make it more readable. Below are examples of single and multi-line comments.


No comments:

Post a Comment