Basic Java Grammer Tips for beginner


I read two books as follows:

This article shows some interested tips in these books.


😎 Hello World

Make hello.java like as follows:

package forloops;

public class Hello {
public static void main(String[] args) {


if
System.out.println("My Project One");
}
}

After making, you can run the Java program.

$ javac hello.java
$ java Hello
#=> My Project One

πŸ˜€ Features

  • Encapsulation
  • Polymorphism
  • Inheritance
  • Abstraction

😼 PRIMARY FEATURES OF JAVA

  • Simple
  • Secure
  • Portable
  • Platform Independent
  • Multi-threading
  • Object Oriented
  • Distributed
  • Robust

😸 Variable Type

  • Integer Number
    • log
    • int
    • short
    • byte
  • Decimal Number
    • double
    • float
  • Boolean
  • Char
  • String

πŸ—½ Literal

  • int : 30
  • log : 3000000L
  • double : 30.5
  • float : 30.5F
  • boolean : true
  • char : β€˜ι›…β€™
  • String : β€œJava”

🚜 Method Overloading

Method overloading is the ability to create multiple methods of the same name
with different implementations.

class DisplayOverloading {
public void disp(char c) {
System.out.println(c);
}
public void disp(char c, int num) {
System.out.println(c + " "+num);
}
}
class Sample {
public static void main(String args[]) {
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}

πŸ€ Method Signature

Method Signeture = (1) Method Name, (2) Argument Number, Type, Order

🎳 Encapsulation

Access Modifiers for Member(Field, Method)

  • Private : The member can be accessed within same class
  • Package private : The member can be accessed within the same package
  • Protected : The member can be accessed within same package or inherited class
  • Public : The class can be accessed anywhere

Access Modifiers for Class

  • Package private : The class can be accessed within the same package
  • Public : The class can be accessed anywhere

πŸŽƒ Inheritance

Constructor

Constructors use super to invoke the superclass’s constructor.
If a constructor uses super, it must use it in the first line;
otherwise, the compiler will complain. An example follows:

public class Parent {
Parent() {}
}
class Child extends Parent {
Child() {
super();
}
}

Is-a relationship

is-a is a subsumption relationship between abstractions (e.g. types, classes),
where one class A is a subclass of another class B (and so B is a superclass of A).

Abstract Class / Method / Interface

  • Abstract Method
  • Abstract Class
  • Interface = public abstract

🍣 Java Standard API

Date

  • You should use java.util.Date if you want to use Data information.
  • Calendar class can convert Date instance by year, month, day, hour, minute, second.
  • SimpleDateFormat class can convert Date instance to arranged String.

πŸ€” Exception

🏈 type

  • Grammer Error
  • Runtime Error
  • Ligic Error

Fatal Error

  • Fatal Error. It is impossible to recover from the error
  • e.g. OutOfMemoryError, ClassFormatError

Exception(except RuntimeError)

  • Child of java.lang.exception(except RuntimeError)
  • You should plan how to recovery by the error in your code.
  • e.g. IOException, ConnectException

RuntimeException

  • You don’t have to consider a recovery plan by the error.
  • e.g. NullPointerException, ArrayIndexOutOfBoundsException

Automatically run close() in try-catch

If a type has java.lang.AutoClosable interface, the API class automatically runs close() in try-catch sentence.


🐞 Special Thanks

πŸ–₯ Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!