Commented code of the first program - what are Classes and Methods



Now that you are a Java programmer - because you created a program already -
we will understand what we did, what is that alphabet soup and what is every part of that bizarre coda

This includes an explanation about car classes and methods.


Commenting on the first program created in detail, in Java, giving a background on what are classes and methods.

Already in this first example I will mention two of the most important concepts in Java, you'll hear the rest of your Java life:
classes and methods.

Class is the basis of everything in Java. Class is something more abstract, is a general definition. An example or instance of a class is an object.
Class is something more generic, object is something more specific.
Hence the fact that Java is object-oriented.

It seems to be complicated and confusing, but I'll explain and you will see that it is simple.
Actually, it was meant to be simple because imitates the life.

The car for example, is a class. It is something more general.
An example of an object, is the beetle. The Honda Civic is an object of class car, but it is a different object from the object beetle.
It is pretty obvious, right?

Classes have attributes that all objects have, but are not necessarily equal. But they can be. How so?

Returning to the cars.
All the class cars have the item 'engine'. Then the beetle object has engine and the Honda Civic also has engine, although these engines are different, because the characteristic of the engines are different.
Other class cars item is doors. In beetle car, this value is 2, it only has two doors (unless you have boosted your beetle or a door has fallen). However, other objects of the class car has four doors.

This helps a lot when creating large projects. Imagine that you have a job in a company with thousands of employees and services.
Easy easy your Java program will have thousands of objects, but if you organize well will just have a few dozen classes.
So, you organize the 'world' (in which case the company is), in blocks, Classes.
So when you make a change, make the classes that all objects automatically inherit this change.

Begin to see the world as Classes and Objects.

For example, there were only male and female sex.
Today, with this wave of bisexuality and differents kind of music, there is another kind of sex, the 'emo'.
So, you will in each object and change, adding one more sex option for all objects? They're thousands!
No man, go there in class 'employees' and adds the 'emo' as an alternative to sex. Okay, all objects (ie, employees, people), and will inherit this trait, because all objects are instances of the class. They are the class. If the class has changed, the object has changed.

In our case, our class is 'First'.

Now let's methods!
Methods are methods ...! That is, are the means or ways of doing something. In other languages, they are called functions (C, C + +) or subroutines (Perl).

What makes our method? It prints the text 'My first Java program!'.
Methods can calculate addition, subtraction, integrate etc.. There are methods of formatting the hard drive, backup methods, methods of invasion, methods to show a text, a menu, a number of methods that cast lots ... methods are tasks.
It is a piece of code that does something well defined.
It is a method to do something.

In our case, the system ('System') write ('out'), in which case the screen is in the form of writing ('print') the message 'My first Java program!.

If you like do question, then the answer is yes. Just as gets out ('out'), you can get in ('in'), which is when the system ('System') receives user data from you or from a file - 'System.in', you will see in the future.

And yes, he not only writes on the screen. It is also common write, or print to files, to create a log of errors and events, for example.
For now, do not stress about the 'public', 'static', 'void', 'String []', 'args', they are scrutinized in detail to a few shares in the core of his bowels intimate and personal.

Now the most important parts of beginning:
1. All programs, to run, need to have the 'main' method.
The first thingJava does to run is look for the 'public static void main (String [] args)'. 
That's where the program starts.

2. The class containing the main method, 'First', must have the same project name.
Remember that the name of our project is' First 'and the class is "public class First {}'
That is, the main method has to be somewhere within that pair of brackets there.

These are the most common mistakes I see in forums, that beginners make. Forgot the main.

Now you'll use the best method of learning the test. Test:
1. Instead of main, write Main and compile / run.
2. Instead of 'public class First ...' write "public class first 'and compile / run
3. Take the ';', a '{' or '}' off and compile / run
4. What is the difference between the codes below?

This:
public class First 
{

    public static void main(String[] args) {
        System.out.println("My first Java program!");
    }
}

To this
public class First
{

    public static void main(String[] args)
    {
        System.out.println("My first Java program!");
    }
}

And the original?
public class First {

    public static void main(String[] args) {
        System.out.println("My first Java program!");
    }
}

If you think you had friends, I present what is a friend indeed: debugger
This guy shows you the mistakes you made. Sometimes the line and shows up exactly what you missed.
Always read the errors and try to understand what he is saying.

Do the tests and obtain the answers, is the first homework.
Read the error message and interpret.
If they do and to discover the errors and the difference, if any, with their own eyes, they will learn by experience, which is the best way to learn something.

And congratulations, you have many 'advanced' who still misses these basic things ..

No comments: