## 5. The Bitwise Operators:

Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows:

a = 0011 1100

b = 0000 1101

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

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

The following table lists the bitwise operators:

Assume integer variable A holds 60 and variable B holds 13 then:

 & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operan A << 2 will give 240 which is 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 1111 >>> Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. A >>>2 will give 15 which is 0000 1111
~ is different from ! which is a logical operator like !, &&, ||

## Conditional Operator ( ? : ):

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:

variable x = (expression) ? value if true : value if false

## instanceof Operator:

This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). instanceof operator is wriiten as:

( Object reference variable ) instanceof  (class/interface type)

If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is the example:

public class Test {

public static void main(String args[]){
String name = "James";
// following will return true since name is type of String
boolean result = name instanceof String;
System.out.println( result );
}
}

This would produce the following result:

true

This operator will still return true if the object being compared is the assignment compatible with the type on the right. Following is one more example:

class Vehicle {}

public class Car extends Vehicle {
public static void main(String args[]){
Vehicle a = new Car();
boolean result =  a instanceof Car;
System.out.println( result );
}
}

This would produce the following result:

true

## 6. Enhanced for loop in Java:

As of Java 5, the enhanced for loop was introduced. This is mainly used for Arrays.

## Syntax:

The syntax of enhanced for loop is:

for(declaration : expression)
{
//Statements
}
• Declaration: The newly declared block variable, which is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element.

• Expression: This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.

example:

7. Numbers class

Normally, when we work with Numbers, we use primitive data types such as byte, int, long, double, etc.

## Example:

int i = 5000;
float gpa = 13.65;
byte mask = 0xaf;

However, in development, we come across situations where we need to use objects instead of primitive data types. In-order to achieve this Java provides wrapper classes for each primitive data type.

All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

This wrapping is taken care of by the compiler, the process is called boxing. So when a primitive is used when an object is required, the compiler boxes the primitive type in its wrapper class. Similarly, the compiler unboxes the object to a primitive as well. TheNumber is part of the java.lang package.

Autoboxing和unboxing又名拆箱和装箱，简单一点讲，就是从primitive转换到wrapper class，例如int类型到Integer类型就是装箱，而Integer类型到int类型则是拆箱。当然，这里的装箱和拆箱都是auto的，是JVM在工作的内容，事实上不用我们手写，然而也有手写的对应方式，如下所示：

1 int i=10;2 Integer a=new Integer(i);//装箱的操作3 int j=a.intValue();//拆箱的操作

1 int i=10;2 Integer b=i;//自动的装箱3 int k=b;//自动的拆箱

1 Integer d=new Integer(10);2 d++;//这条语句使得d先拆箱，然后进行++操作，而后对结果再装箱

Here is an example of boxing and unboxing:

## Number Methods:

Here is the list of the instance methods that all the subclasses of the Number class implement:

When x is assigned integer values, the compiler boxes the integer because x is integer objects. Later, x is unboxed so that they can be added as integers.

8. Characters Class
Character ch = new Character('a);

## Escape Sequences:

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.

The newline character (\n) has been used frequently in this tutorial in System.out.println() statements to advance to the next line after the string is printed.

Following table shows the Java escape sequences:

Escape Sequence Description
\t Inserts a tab in the text at this point.
\b Inserts a backspace in the text at this point.
\n Inserts a newline in the text at this point.
\r Inserts a carriage return in the text at this point.
\f Inserts a form feed in the text at this point.
\' Inserts a single quote character in the text at this point.
\" Inserts a double quote character in the text at this point.
\\ Inserts a backslash character in the text at this point.

9. String Class
As with any other object, you can create String objects by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters.

10. Array Class

## The foreach Loops:

JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.

## Example:

The following code displays all the elements in the array myList:

public class TestArray {

public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};

// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}

#### 【Java Tutorial中文版 开篇综述】 Oracle（甲骨文）公司Java最新官方教程【译文】

2016-02-04 17:28:54

#### javaSE Tutorial中文版

2009年12月28日 49KB 下载

#### java官方书籍推荐

2012-10-27 17:48:57

#### The Java EE 7 Tutorial

2014年08月01日 14.14MB 下载

#### The Java EE 6 Tutorial 中文版 (翻译) 第32章 JPA简介 (一) 实体

2013-06-20 11:35:16

#### The Java EE 6 Tutorial

2011年05月07日 11.71MB 下载

#### The Java EE 7 tutorial

2013年09月03日 14.82MB 下载

#### graph slam tutorial ：从推导到应用２

2015-10-12 11:04:22

#### Java Tutorial

2009年04月14日 6.49MB 下载

#### [初学Unity]Space Shooter Tutorial Extension

2016-08-07 17:47:58

Java tutorial 2