From 0 to 1, I want to be a developer!

·

14 min read

From 0 to 1, I want to be a developer!

Hello World

My name is Han. My goal is to become a software developer. I believe I can achieve that goal by spending my time and effort on it. This blog aims to record my learning process and share any problems that I ever had.

Which language do I use?

My first programming language is Java. The reason why I choose Java is that Java is one of the most popular programming languages in the world and it has been widely used in all kinds of industries.

Let's get started!

Step 1. Install Java

To use Java, you need to install Java JDK(Java Development Kit), Download. To run an XXX.java file, we need to use javac to compile it to the XXX.class file. And then we can use java to run it. javac and java are two important tools included in JDK.

Step 2. Install IDEA

If you want to do good work, you must first sharpen your tools. Once you downloaded and installed JDK, you gonna need a good tool to use it. Here I recommend IntelliJ IDEA.

Step 3. Create your first program

Here's a great video showing you how to create a project with IDEA %[youtube.com/watch?v=H_XxH66lm3U&ab_chan..

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("HelloWorld");
    }
}

Java operation method

coding: developer write .java code

compile: machine can only read the file as 0011 languages, .java file needs to be transferred to machine language

run: machine runs the compiled .java language

java runs on any platform with JVM (Java Virtual Machine)

Java Basic Concepts

Annoation

//I'm annotation (single line)

/*
 I am also annotation (multi-lines)
 */
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("HelloWorld");
    }
}

The information in the annotation will not be compiled

Java keywords

there are over 50 keywords in Java and they are all highlighted when you use them in the coding. Keywords are all lower case

public, class, private, final.....

Java data type

byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

How does Java read negative values?
We use Ones' complement and Two's complement
For example -4 in binary is 1000 0100, in One's complement 1111 1011, in Two's complement 1111 1100
Positive numbers are all the same in binary, in One's complement, and Two's complement

Flow control statement

  1. Decision making Statements: if / switch

  2. Loop statements: do while / while / for / for-each

  3. Jump statements: break / continue

Array

What is Array?
Arrays are used to store multiple values with the same data type in a single variable, instead of declaring separate variables for each value.
For example:

String[ ] cars;

We can also use:

String cars[ ];

We defined a String Array, the values stored in this array must be String type.

String[ ] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Static initialization of an Array

Initialization - Create space for the Array container in memory and store the data in the container.
For example:

int[ ] arr = new int [ ] {1, 2, 3};

Notice: arr stores the memory address for the Array

System.out.println(arr); //[D@568db2f2

Dynamic initialization of an Array

When initializing Array, we set the length of Array only, the initial values will be assigned by the program.
For example:

String [ ] arr = new String [10];

Here we created an Array with type String and length of 10.
Notice:
The length of the Array can not be changed once it's been defined.

Method in Java

What is Method?
Method is the smallest execution unit in a program.

When and why should we use Method?
Repeated code and code with independent functions can be put into the method. Improve code reusability and maintainability.

Notice:

  1. Method will not run until you call it.
  2. Method and method are on the same level, you cant nest one method within another method.>/br>
  3. The order in which methods are written has nothing to do with the order of execution.
  4. When the return type is void in the method, it means this method has no return value.
  5. You can write any code after the return keyword.
Method overload

For example:

public class MethodDemo {
      public static int sum (int a, int b) {
            return a +b;
      }
      public static int sum (int a, int b, int c) {
            return a + b +c;
      }
      public static int sum (int a, int b, int c, int d) {
            return a + b + c + d;
      }
      public static double sum (double a, double b) {
            return a +b;
      }
}

Methods with the same name and different signatures within the same class are called Method overloading.
In method overloading, the return type can or can not be the same.

Java basic data type / reference data type

Basic data type is the type of data that is stored in stack memory. Variable stores the real data.
Reference data type is the type of data that is stored in heap memory. Variable stores the address of the data in heap memory.

Object-Oriented

We know Java is an object-oriented program, but what is OOP?
Object is something that can help you to do something.
Object-Oriented means you take the object you need to do something you want.
For example, if you want to wash your clothes, so you need a washing machine, this washing machine is the object. The process by that you use the washing machine to wash your clothes is called OOP.
Learning how to use existing objects and how to create our objects and use them are the two most important things in OOP.

Class and Object

In Java, you must design a class before getting your object.
Class is the description of the common features of the object.

The compositions of the class include member variables and member methods.
Member variables stand for the object properties.
Member methods stand for the object actions.
For example:

public class Phone {

     String brand;
     Double price;

     public void call ( ) {
     }
     public void playGame ( ) {
     }

To use this object, we need to call it.

Phone iphone = new Phone ( );
iphone.name = "iphone13";
iphone.price = 999.99;

Note:
In Java, the class that we used to describe something is called the JavaBean class.

Three major pillars for OOP: Encapsulation / Inheritance / Polymorphism

Encapsulation

Encapsulation is forming a protective barrier around the information contained within a class from the rest of the code. It prevents the code and data of the class from being randomly accessed by code defined by an external class. To access the code and data of the class, it must be controlled by a strict interface.

Polymorphism

Polymorphism in Java allows us to perform the same action in many different ways. By using the same fatter class, we can reference different son classes. All Java objects are polymorphic as they passed the IS-A test for their type and the class Object.

Inheritance

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields to your current class.

Encapsulation

Constructor method

What is the constructor method in Java?
When we create an object, JVM will call the constructor automatically and use it to assign data to member variables.

Parameterized constructor / No argument constructor

With parameterized constructor, member variables will be assigned with the default value.
with a no-argument constructor, member variables will be assigned with the data that is offered by you.

For example:

public class Student {
    private int age;
    private String name;

    public Student() {
    }
    public Student(int age, String name, String address) {
        this.age = age;
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

All classes will automatically have no argument constructor when they are created, no matter whether you write it or not.
If you write a parameterized constructor in your class, you must also define a no-argument constructor if you want to use it.

When we call method, we always see member variable and local variable, what are the differences between those two types of variables?
MV is defined within class, outside of method / LV is defined within method and method clarification.
MV has default values / LV has no default values, it needs to be defined before use.
MV is stored in heap / LV is stored in stack.
MV exists when Object exists, it disappears when Object disappears / LV is called when the method is called, it disappears once the method is over.
MV works in the whole class / LV works within the method.

API (Application Programming Interface)

API means the class with all kinds of functions. We can call the API that's been written by other programmers or we can write our own API.
Java provides a bunch of API for us to use.

String API

String is a class defined by Java, and stored in java.lang package. String object is immutable once it's been created.
String API has multiple constructures:

public String( ); 
public String (String original); 
public String (char [ ] chs);
public String (byte [ ] chs);

Notice:
When we use definition method like

String str= "HelloWorld" ;

Java will create a string pool in Heap to store the string value, str refer to the data address in that string pool.

String s1 = "HelloWorld";
String s2 = "HelloWorld";

s1 and s2 both refer to the same address because the same string will only be generated once in the string pool.

Let's check another situation

String s1 = new String ("HelloWorld");
String s2 = "HelloWorld";

Here s1 and s2 refer to different addresses because when we use the "new" keyword, Java will create a new space for that data in Heap, s2 refers to the string that is stored in the String pool, which is different.

StringBuilder API

We know that String is immutable once it has been created, but in some cases, we may need to modify our String variable, how could we do that? We can use StringBuilder.
StringBuilder can be seen as a container, the content is mutable.

ArrayList API

We know that the length of the Array can't be changed once it's settled, in some cases we need to add more values in the Array, we can now use ArrayList.

ArrayList<Integer> arr = new ArrayList<>();

The type of data that will be stored in the ArrayList can be defined when we create the ArrayList. Now we can only put Integer type data in arr.

static

In Java program, we see the keyword "static" a lot, what is "static"?
"static" is one of the Java keywords and it can be used to modify member variables or member methods.

Static variable: shared by all Objects in this class
Notice: static variable will be loaded when the class is loaded in the program, it takes precedence over objects. It belongs to the class, not objects
Static variable will be stored in a space in heap memory called the static area.

Static method: mainly used in test class or tool class
Notice: static method can only access static variables and static methods. Non-static methods can access both static variables/methods and non-static variables/methods. Static method can not have "this" keyword.

Inheritance

Java provided one keyword "extends", with the keyword, we can build connections between classes.

public class Student extends Person { }

In this example, Student class is called child class(derived class, sub class). Person class is called father class(base class, super class).
The advantages of using inheritance:

  1. We can put repeated code in father class to improve code reusability.
  2. Son class can add different functions based on father class.

When can we use inheritance?
When common content exists between class and class, and son class is a type of parent class, we can use inheritance to optimize our code.
In our example, students belong to person and they have the same content like name, age, and address. Students have their special function - study.
Notice:
Java supports only single inheritance, one son class can only have one father class. Father class can have multi son classes.
Java supports multi-level inheritance, for example, class A can inherit class B, and class B can inherit class C. We say class C is an indirect father of class A.
If one class doesn't have any father class, Java will automatically add class Object as its father class. All classes in Java are directly or indirectly inherit class Object.

When a son class inherits a father class, what content will be inherited?

  1. Constructors. Both non-private and private constructors will not be inherited.
  2. Member variables: Both non-private and private member variables will be inherited. But son class can not use the inherited private member variables!

Example:

public class Father {
     String name;
     int age;
}
public class Son {
    String game;
}
public class Test{
     public static void main (String [ ] args) {
          Son son = new Son( );
          System.out.println(son); // print memory address of son object
          son.name ="Mike";
          son.age = 23;
          son.game = "Halo";
          System.out.println(son.name + ", " +son.age + ", " +son.game); // system will print Mike, 23, Halo

But, if we change Father class to:

public class Father {
     private String name;
     private int age;
}

We can not use name and age variables in son object.

Member methods: Non-private member methods can be inherited but private member methods can not be inherited.

Member variable access features

Let's check the following examples:

public class Father {
     String name = "Father";
}
public class Son extends Father {
     String name = "Son"
     public void show( ){
          String name = "A";
          System.out.println(name);
     }
}

The result would be "A".
What if we change the Son class to this:

public class Son extends Father {
     String name = "Son"
     public void show( ){

          System.out.println(name);
     }
}

The result would be "Son".
What if we change the Son class again, such as:

public class Son extends Father {

     public void show( ){

          System.out.println(name);
     }
}

The result would be "Father".
If we change the Son class to:

public class Son extends Father {
     String name = "Son"
     public void show( ){
          String name = "A";
          System.out.println(name);
          System.out.println(this.name);
          System.out.println(super.name);
     }
}

The result would be "A", "Son", "Father".
When the system tried to print the name, it will first check if there is a local variable with the same name, if not, the system will check the member variable, if not, the system will check the extended member variable from the father class, it the system still can't find anything, an exception will throw out.

Member method access features

Same like member variable access features, the system will try to check if there is a member method in the son class, if not, the system will check if there is a member method in the father class.

class Person{
     public void eat ( ){
     System.out.println("I am hungry...");
     }
     public void drink ( ){
     System.out.println("I am thirsty...");
     }
}

class Man extends Person {
     public void lunch ( ){
     eat ( );
     drink ( );
     }
]
public class Test{
    public static void main(String[] args) {
          Man man = new Man ();
          man.eat();
          man.drink();
    }
}

The result would be "I am hungry..." and "I am thirsty...".

Method overriding

When the methods in the father class can't meet the needs of the son class, we need to override the methods.
For example:

public class Father () {
        public void Drink() {
            System.out.println("I want to drink water");
        }
    }

    public class Son extends Father (){
        @Override
        public void Drink () {
            System.out.println("I want to drink ice tea");
        }
    }

The "@Override" annotation is used to check if we override the method correctly.
Notice:

  1. When overriding the method, the name of the methods and local variables must be the same.
  2. When overriding the method, the access modifier in the Son class must be the same or wider than Father class. ( public > protected > empty)
  3. When overriding the method, the return type in the Son class must be the subtype or the same as the Father class.
  4. Final, static, private methods can not be overridden.

Polymorphism

Polymorphism allows us to perform the same action in different ways. By using the same father class, we can reference different son classes. Inheritance is the precondition of polymorphism.
For example, we have a Father class and a Son class:

public class Father {

}

public class Son extends Father{

}

We can call father object in this way

public class Test {
    public static void main(String[] args) {
        Father father = new Son();
    }
}