Seraphcex's Blog.

Java Basic--Variable and Constant

2018/01/22 Share

According to the Java lesson that I took,I’ll divide my notes into 3 parts:basic,medium and advanced.So this is the first section of part 1.

Keyword

These words have special purposes in Java,each one has special meaning to the Java compiler,also we need to notice that they are case sensitive.
For example

1
2
void // this is a keyword
Void // this isn't a keyword

And here are part of keywords that we usually use:

abstract boolean break byte case catch
char class continue default do double
else extends false final finally float
for if implements import instanceof int
interface long native new null package
private protected synchronized this throw throws
transient true try void volatile while

Pratice

1
2
3
4
5
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World");
}
}

Identifier

It’s a symbol that we use to name variable,class,method etc.

When we use identifier,there are some rules need to be followed:

  1. A identifier can consists of letter,number,_,$,and can’t starts with number.
  2. Identifier can’t be Java keyword or reserved word.
  3. Identifier is case sensitive.
  4. Naming a identifier better can inform its meaning or purpose.

Variable

We can see it as a box,we can put anything we want into the box,and replace it with new item at any time.

In Java,we use 3 elements to describe variable:type,name and value.

Also we need to follow rules like:

  1. A variable name can consists of letter,number,_,$,and can’t starts with number.
  2. Variable name can’t be Java keyword or reserved word.
  3. Variable name is case sensitive.
  4. Naming a variable name better can inform its meaning or purpose.

In order to form a better program habit,there are several naming methods:

  1. Hungarian notation
  2. Camel case
  3. Pascal case

Practice

1
2
3
4
5
6
public class HelloWorld{
public static void main(String[] args) {
String hobby = "football"; // we declare a variable
System.out.println("My hobby is" + hobby); // we bind those two together with '+',and print it out
}
}

Data type

Java is a strong typing programming language,it means every data in Java has its own type,and it must be define when Java compiles,want more details click here

Practice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class HelloWorld{
public static void main(String[] args) {
String name="Tom";
char sex='male';
int age=18;
double price=120.5;
boolean isOK=true;
System.out.println(name);
System.out.println(sex);
System.out.println(num);
System.out.println(price);
System.out.println(isOK);
}
}

Rules of using variables

  1. Variables must declare before using it.

    1
    2
    3
    4
    5
    6
    public class HelloWorld{
    public static void main(String[] args) {
    String name="Tom";
    System.out.println(name);
    }
    }
  2. You can declare and initialize variables in the same time.

    1
    String name = "Tom";

Also you can first declareit,and then assign it afterward.

1
2
String name;
name = "Tom";

  1. You can only assign one value to a variable,but you can change multiple time.

    1
    2
    String name = "Tom";
    name = "Jack";
  2. The variables that you have declared in method main must be assigned first,and then you can output it.

    1
    2
    3
    4
    5
    6
    public class HelloWorld{
    public static void main(String[] args) {
    String name="Tom";
    System.out.println(name);
    }
    }
  3. Although you might not have error shows up,but in real life development,I suggest only using english when you naming variables.It’s easier to have safety issues if you use other languages,such as having gibberish when you are working on cross platform.

Auto type conversion

In our program,we usually need to convert between differnet primitive data types of data.For example:

1
2
3
int num1 = 82;
double num2 = num1;
System.out.println(num2);

This kind of conversion is called auto type conversion.Of course,it needs to match particular antecedents:

  1. Target type is compatible with source type,like double is compatible with int,but char isn’t compatible with int.
  2. Target type must larger than source type.

Forced type conversion

Although auto type conversion is pretty convenient,but it can’t match all of our programming needs.So we need something called forced type conversion.

  • syntax: (target type)value
1
2
3
double num1 = 75.8;
int num2 = (int)num1;
System.out.println(num2);

and then you’ll have

1
75

You can see ,it just simply remove everything to the right of the decimal point(including decimal point itself).

Therefore,it’s pretty clear now,using forced type conversion might cause data lost,you need to be careful when you using it.

Constant

We see it as a kind of special variable,when its value has been assigned,it doesn’t allow to change during the operation of your program.

  • syntax: final type name = value;
1
2
final String NAME = "Tom";
final double PI = 3.14;

Using constant in your program can improve maintainability,and notice we normally use capital letter while naming a constant.

Annotation

When we are coding,usually need to add some notes attach to the source code,in order to describe its purpose.It’s a important thing when we talk about coding.

There are three kinds of annotation:

  1. 1
    2
    3
    4
    5
    /**
    *
    *@authur Seraphcex
    *@version v1.0
    */



  1. 1
    2
    3
    4
    /*
    *this can include multiple lines
    *
    *


  1. 1
    //single line

Annotation won’t be executed during the operation,more details click here.

CATALOG
  1. 1. Keyword
  2. 2. Identifier
  3. 3. Variable
  4. 4. Data type
  5. 5. Rules of using variables
  6. 6. Auto type conversion
  7. 7. Forced type conversion
  8. 8. Constant
  9. 9. Annotation