Java tutorial 2

转载 2015年07月09日 10:37:52

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:


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:


6. Enhanced for loop in Java:

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


The syntax of enhanced for loop is:

for(declaration : expression)
  • 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.


7. Numbers class

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


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.

Number Subclasses

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先拆箱,然后进行++操作,而后对结果再装箱

上面的这条语句,使得Java保证了wrapper class也可以是正常使用通用的操作符,但这绝对不是C++中的运算符重载。

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.
之后,使用intValue(), doubleValue()等,使得x转换为相应类型。
然后parseInt, parseDouble将String转换为int,其中parseInt( , )两个参数的话,第二个为该数的进制,例如444就是15进制数,转换为10进制为964.

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.


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) {


Java Arrays Tutorial (2)

Two-dimenstional arrays have two or more columns of values, for example, the following code assigns ...

Java NIO Tutorial 2- Java NIO Overview

Java NIO Overview Java NIO consist of the following core components: ChannelsBuffersSelectors Ja...

Java 2D tutorial

  • 2009-03-12 20:06
  • 1.24MB
  • 下载

Step-by-Step SqlAlchemy Tutorial(part 1 of 2)

一直觉得orm很高深,今天时间比较充分打算学习下 安装说一点,我的系统是win7 64位家庭普通版 用easy_install安装sqlalchemy出现问题,于是下载最新版0.7.6的zip压缩...

Mule ESB-Content-Based Routing Tutorial(2)

承接 Mule ESB-Content-Based Routing Tutorial(1) 五、运行应用程序  完成创建,配置,并保存你的新的应用程序,您就可以在嵌入Mule的服务器上运...