Basics part 1

A job search path can be pretty exciting. Another exciting interview for a great company! For the technical part, they send over some materials to go over and prepare for the technical interview with JAVA. I can’t say enough how convenient situations like that are for us — new in tech! I received everything I needed, to learn a new language! materials and resources, and the most important — deadline! I feel like this is the most important for the success of any project. Let me tell you what I have learned:

## Java datatypes:

Java specifies its own datatypes. Each variable specifies its own type. There are two categories of variables: primitives and reference variables. **Primitives** :

A primitive variable uses a predefined amount of space in memory There are 8 primitive datatypes:

1. boolean: true or false

2. byte: 8-bit two’s complement integer

3. short: 16-bit two’s complement integer

4. char: 16-bit Unicode character

5. int: 32-bit integer

6. float: 32-bit floating-point number

7. long: 64-bit integer

8. double: 64-bit floating-point number **Reference Variables :**

A reference variable uses a class name as its type. Ex: Dog d = new Dog(); **Arrays:**

An array is a data structure that acts as a collection of either primitive or a reference-type variable.

## Operators

Operators are used to create statements in Java. The assignment operator uses the equals (=) sign. It allows you to assign a value to a variable. **Mathematical Operators :**There are several mathematical operators and many resemble their common symbols and functionality +, =, -, %, *.

**Parenthesis and Operator Precedence**:

You can group expressions using parenthesis to change operator precedence just like you can in conventional mathematics Ex: int a = 9 * (5 + 2); //a is 63 and not 47

**Shift Operators**

There are 3 types of shift operators and they perform bitwise shifting. Meaning they’ll take the bit representation of a value and shift the numbers a specified amount of decimal places.

**>> Signed right-shift Operator**:

The >> operator will shift the bits to the right a specified number. Ex: int a = 8 >> 1; The above result will be 4. We know that 8 is represented in binary as 1000 or 0000 1000. If we shift the bits to the right by 1 then the binary value is 100 or 0000 0100 and this value equates to 4.

**>> Unsigned right shift Operator:**

The >>> operator will shift a zero into the leftmost position of a number. Ex: int a = -8 >>> 1; The above result will be 2147483644. We know that -8 is represented in binary as 1111 1111 1111 1111 1111 1111 1111 1000 using two’s complement. If we shift a zero into the leftmost position then we get 0111 1111 1111 1111 1111 1111 1111 1100 and this value equates to 2147483644. Note an int is the default value system and thus we have 32 bits or 4 bytes.

**Relational operators**are used to compare values.

**Greater than Operator:**

The > operator will compare a value on the left and return true if it is greater than the value on the right. It returns a boolean type. Ex: boolean b = 6 > 5; The above result will be true.

**= Greater than Operator:**

The >= operator will compare a value on the left and return true if it is greater than or equal to the value on the right. It returns a boolean type. Ex: Week 2 Page 2 Ex: boolean b = 6 >= 6; The above result will be true. instanceof Operator The instanceof operator will compare an object reference variable to the type of a class. Ex: boolean b = dog instanceof Dog; The above result will be true if the variable dog refers to an instance of the class Dog. Equality Operators equality operators are really a type of relational operator that equates the value of one variable to another.

**== Equals Operator:**

The == operator returns true if both values are the same. Ex: boolean b = 6 == 6; The above result will be true.

NOTE: Be careful when comparing objects. The == operator will test whether the point to the same instance and not if they have equivalent values.

**!= Not Equals Operator:**

The != operator returns true if both values are not the same. Ex: boolean b = 6 != 6; The above result will be false.

NOTE: Be careful when comparing objects. The != operator will test whether the point to the same instance and not if they do not have equivalent values.

**Bitwise Operators:**

Bitwise operators will manipulate the individual bits of a value.

**~ Unary Bitwise Complement Operator:**

The ~ operator will invert a bit pattern. Any “1”s will become “0”s and vice versa. Ex: byte b = ~6; The above result will be -7. We know that 6 is 0000 0110 in binary (8 bits is one byte). Inverting this value will equate to 1111 1001 Week 2 Page 3 We know that 6 is 0000 0110 in binary (8 bits is one byte). Inverting this value will equate to 1111 1001 which is -7 in decimal.

**& Bitwise AND Operator:**

The & operator will perform a bitwise AND operation. The AND operation will compare each bit of the values in order and return a 1 only if both values have a “1” in that position. Ex: byte six = 6; //0110 byte two = 2; //0010 //0010 performing a bitwise AND operation int result = six & two; //result is 2 (0010)

**| Bitwise OR Operator:**

The | operator will perform a bitwise OR operation. The OR operation will compare each bit of the values in order and return a 1 if either of the values have a “1” in that position. Ex: byte six = 6; //0110 byte one = 1; //0001 //0111 performing a bitwise OR operation int result = six & two; //result is 7 (0111)

**^ Bitwise Exclusive OR Operator (XOR):**

The ^ operator will perform a bitwise exclusive XOR operation. The Exclusive OR operation will compare each bit of the values in order and return a 1 if either of the values have a “1” in that position, but not both. Ex: byte six = 6; //0110 byte one = 2; //0010 //0100 performing a bitwise xOR operation int result = six & two; //result is 4 (0100)

**Logical Operators:**

A logical operator will compare two boolean values and return a result.

**&& Logical AND Operator**:

The && operator will compare two boolean values and return true if and only if both values are true. Ex: boolean b = true && false; The above will value will be false because both operands are not true.

**|| Logical AND Operator:**

The || operator will compare two boolean values and return true if either operand is true. Ex: boolean b = true || false; Week 2 Page 4 boolean b = true || false; The above will value will be true because at least one operand is true.

**Ternary Operator**:

The ternary operator is named so because it has three operands. It takes in a comparison and will return a specified value if the comparison evaluates to true or another value if it evaluates to false. If is equivalent to the phrase, “If this, return this, otherwise return that”. It takes the format: comparison_statement ? value to return if true : value to return if false; Ex: int x = 5 > 7 ? 18 : 24; The above will value of x will be 24 because 5 is not greater than 7 and so we’ll return the false value (24). Ex boolean value = (6 | 2 ) > 2 ? false: true; The above will value will be false because at the bitwise OR of 6 (0110) and 2 (0010) is 6 and that is greater than 2, so we’ll use the value to return if true and that is false.

**Assignment Operators:**

In addition to the assignment operator (=), there are several shortcuts that combine other operators and assign the value to a variable all in one statement. These operators use the current value of the variable and perform some operation with it and another value and assign it to that same variable.

That’s a lot for one post, stay tuned for other parts!