Java for Android - 002

by 08:21 0 comments
If you finished lesson1 Here[link], You have the tools you need to start.
This lesson we will start coding.

Create Project

Open Eclipse, Then go to File>New>JavaProject

 You will find a window asking you for some information to create the project
Enter the project name, Edit the path if you need and press finish

Close the welcome screen if it is still there, here we are done.
You have two folders in the project folder
JRE System Library that contains all libraries needed (check the past lesson), anyway we are not going to touch this folder.
src is the folder contains all the code files we are going to create.
Let's go next.

Hello World

Right-click on the src folder, then choose New>Class
In the window appears you have to set the name of the package and the name of the class.
The package is a unique name identifies a group of classes, Interfaces(we will know that thing soon) and more.
Classes can easily treat with other classed in the same package, Packages can be treated as libraries later so think in as good friends now.
The class name must start with an upper case letter.
Because the program will start running from this class we will check the box of public static void main.
Let us discuss what was that and go on.

Basic Rules

In Java, each application has an entry point, or a starting point, which is a method called main.
If you are c/c++ programmer so you know the syntax, it's the same.
  • In Java, every line of code that can actually run needs to be inside a class.
  • Every line ends with(;) (semicolon) - Never forget it.
  • Anything outside the {} will not run unless we told the program to run it.
  • Any line starts with // will not be compiled or executed.
Don't be confused, we will run some code it's okay.

Hello World Runs

Inside the public static void main(string[] args){} write this line:
System.out.println("Hello World");
Press run (green small icon with white arrow) and see the console in the bottom prints what we wrote inside the "".

Main method

To run our program, the main method must be identical to this signature:
public static void main(string[] args)
public: anyone can access it
static: method can be run without creating an instance of the class containing the main method
void: method doesn't return any value
main: the name of the method.

Input and Output

System.out is used to produce an output (usually to the console), is used to get input for the program.

Code of this tutorial

The code in this lesson is in tutorial001 folder.
All codes of tutorial should be available here[link]
If you want to learn more about git and github you can go there[link]
No need for that you can just download the code

See you next time.

Ammar Atef

Blog Creator

Always work and you will be happy, Writing code, designing system and learning some thing new, I call that real life, No matter what is the problem, humanity can solve it any way, Creative one who believes he is creative, Who really watch, think, try and success, World is watching us because we are engineers.


Post a Comment