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:

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.

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();//拆箱的操作

上面是手动的,在Java5.0之后已经在JVM中有了自动的装箱和拆箱的转换,如下所示:

 

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.

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】: Getting Started(开始入门)【官方原版教程】

入门: 这个页面内容提供了你需要了解并开始使用Java编程语言的一切。
  • RuingMan
  • RuingMan
  • 2016年02月05日 17:58
  • 1547

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

此篇为我博客【Java Tutorial】系列开篇综述。JavaTutorial是Oracle公司的官方教学教程,内容从认识Java到如何开发Java有着非常系统的体系,而且叙述清楚明了,非常适合Ja...
  • RuingMan
  • RuingMan
  • 2016年02月04日 17:28
  • 4367

这才是Angular2的灵魂!

原文出处:http://www.imooc.com/article/13633 文|章小飞(Aaron Zhang) 谷歌 Angular 技术推广工程师 背景介绍:Angular...
  • chelen_jak
  • chelen_jak
  • 2016年12月19日 17:00
  • 561

GAN系列学习(1)——前生今世

作者:刘威威编辑:李文臣11.GAN的 ‘前世’?大家都知道GAN是Ian Goodfellow 2 014年放出的一篇开山之作,在深度学习界评价很高,可以说GAN的出现,给深度学习界带来了很多的研究...
  • u013709270
  • u013709270
  • 2017年12月09日 00:00
  • 746

JAVA tutorial官方案例:学习笔记

学习java语言基础知识语法规范:变量命名 1 变量名称区分大小写。 变量的名称可以是任何合法标识符:字母,美元符号,或下划线字符’_’头的Unicode字母和数字的无限长序列。 然而在实际中我们总是...
  • qq_25467523
  • qq_25467523
  • 2016年11月28日 15:57
  • 113

PyQt4 tutorial读书笔记(2)

Statusbar、Menus and toolbars in PyQt4英文参考:http://zetcode.com/gui/pyqt4/menusandtoolbars/ 把三者整合到一起的代...
  • oZuiJiaoWeiYang
  • oZuiJiaoWeiYang
  • 2015年03月04日 16:17
  • 640

graph slam tutorial :从推导到应用2

在上一部分中通过一个例子大致了解了graph based slam的优化过程。在本篇博客中将提升一个层次,对图优化的求解过程进行推导。由于博文关注的在图构建好以后,如何调整机器人位姿使误差最下。因此,...
  • heyijia0327
  • heyijia0327
  • 2015年10月12日 11:04
  • 9202

Docker的基本使用

Ubuntu14.04中Docker的基本使用 1、安装 1.1 使用apt-get安装(非最新版本) sudo apt-get install docker.io 1.2 下载安装(...
  • wang_san_shi
  • wang_san_shi
  • 2015年08月26日 15:49
  • 1339

Unity实例.003官方示例Survival Shooter Tutorial核心代码学习

Unity官方教学实例《噩梦射手》(Survival Shooter Tutorial)的一些核心代码的学习,包括如何实现角色的移动,摄像机的跟随,敌人的寻路,玩家的生命控制等等内容。...
  • Constant23
  • Constant23
  • 2017年05月02日 16:15
  • 950

SVD应用的经典例子

Singular Value Decomposition (SVD) Tutorial
  • makenothing
  • makenothing
  • 2015年12月17日 16:32
  • 1805
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java tutorial 2
举报原因:
原因补充:

(最多只允许输入30个字)