There are many concepts to learn in Java, but there a handful of core concepts that are central to learning Java. These concepts are:
  • Variables
  • Operations
  • Classes + Objects
    • Fields
    • Constructors
    • Methods
Each of these concepts are explained in a little more detail throughout the rest of this text, and in richer detail in their own texts later in this tutorial.

Variables

Computer programs, regardless of programming language, typically read data from somewhere (file, keyboard, mouse, network etc.), process the data, and write the data somewhere again (to screen, file, network etc.).
In Java, data is kept in variables. Your Java program first declares variables, then read data into them, execute operations on the variables, and then write the variables somewhere again. Variables are explained in more detail in the text on Java variables.
Each variable has a certain data type. The data type determines what kind of data the variable can contain, and what operations you can execute on it. For instance, a variable could be a number. Numbers can be added, subtracted, multiplied, divided etc. Or, a variable could be a string (text). String's can be divided into substrings, searched for characters, concatenated with other strings etc. Java has a set of built-in data types. These data types are described in more detail in the text on Java data types.

Operations

Operations in Java are the instructions you can use to process the data in the variables. Some operations work directly on the variables, while other control the program flow. The most important operations are:
  • Variable operations
    • Variable assignment of values.
    • Variable reading of values.
    • Variable arithmetic.
    • Object instantiation.
  • Program flow
    • for loops.
    • while loops.
    • if statements (branches).
    • switch statements.
    • Method calls.
All of these operations are explained in their own texts.

Classes + Objects

Classes group variables and operations together in coherent modules. A class can have fields, constructors and methods (plus more, but that is not important now). I will shortly describe fields, constructors and methods here, but they are explained in more details in their own texts too. Here is a diagram illustrating classes, fields, constructors and methods:
Objects are instances of classes. When you create an object, that object is of a certain class. The class is like a template (or blueprint) telling how objects of that class should look. When you create an object, you say "give me an object of this class".
If you think of a factory producing lots and lots of the same items, then the class would be the blueprint / manual of how the finished product should look, and the objects would be each of the finished products. If the factory produced cars, then the blueprint / design manual of the cars to produce corresponds to a Java class, and the physical cars produced corresponds to Java objects.
Here is a simple diagram illustrating the principle of objects being of a certain class. The class determines what fields and methods the objects of that class have.
A Java class can contain fields, constructors and methods.
A Java class can contain fields, constructors and methods.

Fields

A field is a variable that belongs to a class or object. It is a piece of data, in other words. For instance, a Car class could define the field brand which all Car objects would have. Then each Car object can have a different value for the brand field.
Fields are covered in more detail in the text on Java fields.

Constructors

Constructors are a special kind of method that is executed when an object is created of the given class. Constructors typically initialize the objects internal fields - if necessary.
Constructors are covered in more detail in the text on Java constructors.

Methods

Methods are groups of operations that carry out a certain function together. For instance, a method may add to numbers, and divide it by a third number. Or, a method could read and write data in a database etc.
Methods are typically used when you need to group operations together, that you need to be able execute from several different places. Or, if you just want your code to be easier to read.

Powered by Blogger.

- Copyright © 2013 Taqi Shah Blogspot -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -