Primitive Data Types

Começar do início
                                        

8 + 5 = 13;

1000 + 0101 = 1101 convert this to decimal (13)

In addition they follow this rule,

0 + 0 = 0

1 + 0 = 1

0 + 1 = 1

1 + 1 = 0 carry 1

Next we go to subtraction:

8 - 5 = 3

1000 + 1011 = 1 0011 ignore the far left 1, this is just an indicator that it is a positive number. In one's complement, 1 in far left most means positive and 0 means negative.

In subtraction, they follow this rule:

1. convert the subtrahend to its complement or its opposite.

   5 ==> 0101 converting complement ==> 1010 we change all the 0 to 1 and the 1 to 0;

2. add one to the subtrahend

    1010 + 0001 = 1011

3. You add it on the minuend;

  1000 + 1011 = 1 0011 and ignore the far left 1. convert 0011 to decimal is equals to 3.

But the problem with this is they do have a double zero. With one's complement they do have positive zero (+0) and a negative zero (-0). So, the solution is to make a computation using two's complement. Your addition is the same like the one's complement but the subtraction, you are doing it like this,

8 - 5 =3

8 ==> 1000

5 ==> 0101

In subtraction they follow this rule:

1. Subtract subtrahend to ideal signed bit

     if 5 uses four bits 0101, then the ideal signed bit is this 1 0000

     1 1 1 1

     1 1 1 1

   1 0000 subtracting

      0101

--------------------

                1

 0  1 1 1 1

     0 1 0 1

-----------------

in subtracting:

              0 - 0 = 0

             0 - 1 = borrow 1 but it would be a double 1

            1 - 0 = 1

            1 - 1 = 0

The answer would 1011 .

2. Then, you add it to the minuend;

   1000 + 1011 = 1 0011 ==>  you ignore the far left most 1, because it is allotted to a positive sign

Now, to go back, that is the two's complement for the signed bit.

2. short - is a 16 bit signed two's complement integer. It  has a range of - 32 768 to + 32 767.

3. integer -  is a 32 bit signed two's complement integer. It has a range (-2 exp 31) to (+2 exp 31) -1

4. long - is a 64 bit unsigned two's complement integer. It has a range of 0 to (2 exp 64 )-1 but for a signed long, it has a range of (-2 exp 63) to (2 exp 63) -1

5. short - is a single precision 32 bit IEEE 754 floating. You can used float for a number with decimal places.

6. double - is a double precision 64 bit IEEE 754 floating point. You can used double for a number with larger decimal places.

7. boolean - it has only two values, the true and false.

8. char - is a single 16 bit unicode character. Unicode means universal character encoding standard. In tech terms, ASCII uses one byte to represent each character while unicode uses four bytes to represent each character. Java uses String too but this is not part of the primitive data type. String is an object.

These are the default values for each type.

1. byte - 0;

2. short - 0;

3. integer - 0;

4. long - 0L (don't forget the subscript L at the end to differentiate it to integer)

5. float - 0.0f (don't forget the subscript f to differentiate it to double);

6. double - 0.0d (you can omit the d if you want)

7. char - '\u0000' (this means that this is character zero "0")

8. String - null (null is the default value for any object)

The usage of private and static is for accessibility of the variable. There are four modifiers:

1. public - it can be accessible to all classes.

2. protected - it can be access within the package of the class but if there is a subclass on other package, it can access it too.

3. default - meaning if you haven't indicated a modifer. It has a default value. It is the same with protected except it does not allow the subclass from the other package.

4. private - accessible only within the class. You used a public method to set and get the variable.

Note: A package is a collection of classes.









Java codes i learned onlineOnde histórias criam vida. Descubra agora