Start a new project and open MainActivity.java (app > java > [package name, something like com.example.testapp]). You should see something like this:
package com.example.kannan.testapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Right now, that's a whole lot of nonsense. There's a bunch of words that seem like English, but the order doesn't make sense. Punctuation is used strangely. We are going to try and break it down a bit.
The first thing to realize is that all of this is not going to immediately make sense. You're not going to understand all of this even at the end of this week. And that is okay!
If you've ever learnt a foreign language, you know that you start with memorizing a lot of phrases. You don't have a grasp of the grammar yet, and you may not even know what many of the individual words mean. But pretty soon you're able to start using some common phrases. If you know how to say, "Where is the library?", and you then learn the word for "canteen", you can now magically say, "Where is the canteen?". You haven't mastered grammar yet. But you're able to spot the pattern. You can identify some words, and you've learned that you can swap them out for other words.
Programming works just the same way. All of this won't make sense right now. What we are going to learn is to see some structure, and figure out which areas we can play around with for now. As we learn more, those areas will expand. In a few weeks, that entire block of code will make sense. But for now, let's try to get an overview.
The first thing I want you to focus on are the brackets. You will notice two kinds of brackets in use in that sample. The normal brackets: ( ). And the curly brackets: { }. These are also called braces, or parentheses. If you haven't programmed before, the second type of bracket may be unfamiliar to you, so find them on your keyboard.
Notice that whenever a bracket opens, it is later closed. Just like in English, or in Mathematics. For example:
setContentView(R.layout.activity_main);
The bracket opens, and is later closed. This is true for curly brackets too:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
There is an open bracket at the end of the first line, and it is closed on the last line. Notice that brackets can contain other brackets. In this example, a set of curly brackets contains two lines which have their own brackets. This works just like in Mathematics. Each open bracket needs to be closed at some point, and they must be closed in the correct order.
Curly brackets contain a block of code. Right now, we don't know what any of the words mean, but we can see that there are lines of code contained within each block. Blocks of code can contain other blocks. For example:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The first line (which starts with public class MainActivity) has an open curly bracket, which is closed on the last line. The middle six lines are a block of code. Line 4 starts a smaller block of code, which contains just two lines. You can imagine them like boxes, that can contain other boxes.
[image]
Notice that there is a shape to the way the lines have been written. Each time a curly bracket is opened (and a new block of code starts), the following lines are shifted to the right. This is done using the "tab" key on your keyboard. You can use the shape of the lines, and how much they are shifted to the right, to see the structure of the code. This is called indentation.
The second thing I want you to notice are the semicolons. Each line in Java ends with a semicolon. You might notice that the only exceptions are lines that are starting a block of code (using a curly bracket). Otherwise, every line of code in Java must end with a semicolon.
Believe it or not, we've taken the first step to understanding Java. We may not yet understand most of the terms on the page, but we've started to see the structure of the text. This will let us start learning the introductory concepts.
In the subsequent sections, I will refer to blocks of code in a particular way. When I say I want to write something inside of MainActivity, I mean the block of code that is started by public class MainActivity. Specifically, I am referring to code in the blue box. When I say I want to write something inside of onCreate, I mean the block of code that is started by protected void onCreate. This is the code that is in the red box.
[image]