Review java - Variable

Basic types

Every type have a default value:

TypeRepresentationInitial valueStorageMax. value
bytesinged integer08 bits127
shortsinged integer016 bits32767
intsinged integer032 bits2147483647
longsinged integer064 bitsover 10^18
floatfloating point0.032 bitsover 10^38
doublefloating point0.064 bitsover 10^308
booleantrue or falsefalse1 bit
charUNICODE (not ASCII)u000016 bitsuFFFF

Difference between i++ and ++i

1
2
b = 1;
a = b++; // a = 1; b = 2
1
2
b = 1;
a = ++b; // a = 2; b = 2
1
2
3
int a = 1;
int res = a++ + a;
// res = 3 ; a = 2.
1
2
3
int a = 1;
int res = a + a++;
// res = 2; a = 2.
1
2
3
int a = 1;
int res = ++a + a;
// res = 4; a = 2.
1
2
3
int a = 1;
int res = a + ++a;
// res = 3; a = 2.

For loop

for(int i=0; i < n ; i++)

Break or continue the outer loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class TestContinueLabel{
    public static void main (String args[]){
        outer:
        for(int i = 1; i < 5; i++){
            System.out.println("Begin outer for i="+i);inner:
            for (int j = 1; j < 5; j++){
                if (j == i) continue outer;
                System.out.println("inner: i=" + i + " j="+j );
            }
            System.out.println("End outer for i="+i);
        }
            System.out.println("Finished.");
    }
}

Result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Begin outer for i=1
Begin outer for i=2
inner: i=2 j=1
Begin outer for i=3
inner: i=3 j=1
inner: i=3 j=2
Begin outer for i=4
inner: i=4 j=1
inner: i=4 j=2
inner: i=4 j=3
Finished.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class TestBreakLabel{
    public static void main (String args[]){
        outer:
        for(int i = 1; i < 5; i++){
            System.out.println("Begin outer for i="+i);
            inner:
            for (int j = 1; j < 5; j++){
                if (j == i) break outer;
                System.out.println("inner: i=" + i + " j="+j );
            }
            System.out.println("End outer for i="+i);
        }
        System.out.println("Finished.");
    }
}

Result:

1
2
Begin outer for i=1
Finished.

C++ only use goto to achieve this feature

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
void TestContinueLabel(){
    for (int i = 1; i < 5; ++i)
    {
        cout << "Begin outer for i=" << i << endl;
        for (int j = 1; j < 5; ++j)
        {
            if(i == j)
                goto ContinueTry;
            cout << "inner: i=" << i << " j=" << j << endl;
        }
        cout << "End outer for i=" << i << endl;
        ContinueTry:;
    }
    cout << "Finished." << endl;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
void TestBreakLabel(){
    for (int i = 1; i < 5; ++i)
    {
        cout << "Begin outer for i=" << i << endl;
        for (int j = 1; j < 5; ++j)
        {
            if(i == j)
                goto BreakTry;
            cout << "inner: i=" << i << " j=" << j << endl;
        }
        cout << "End outer for i=" << i << endl;
    }
    BreakTry:
    cout << "Finished." << endl;
}