JAVA entry must know, a good programmer.

All kinds of knowledge java summary:

 

1. Java annotations way there? How to write?

       Interpretation: compiled and interpreted.

          Block Note:
              / * ** annotation content /
          multi-line comments:
              / * * annotation content /
          single-line comment:
              // annotation content

java common data types

type of data

Keyword

size

Ranges

Byte

byte

1 byte (8 bits)

-27~27-1 (-128~127)

Short integer

short

2 bytes (16 bits)

-215~215-1(-327698~32767)

Integer

int

4 bytes (32-bit)

-231~231-1

Long integer

long

8 bytes (64 bits)

-263~263-1

Single-precision

float

4 bytes (32-bit)

±1.40239846E-45~±3.40282347E+8

Double

double

8 bytes (64 bits)

±4.94065645841246544E-324~±79769313486231570E+308

Character

char

2 bytes (16 bits)

\ U0000 ~ \ uFFFF

Boolean

boolean

Uncertain, the decision by the JVM

true、false

         

               It must be noted that, unlike the C language is different, Java each data type (except boolean) occupies minimal memory space is strictly defined. For example, a signed integer 32-bit data types. Since Java programs are running on the JVM, so Java can define the exact size of the underlying data type, to platform-independent program runs. Thus, the underlying operating system platform will not affect the Java basic data type and size ranges, thus ensuring the portability of Java programs.

               Programming includes processing data. Data stored in computer memory, a program to create and manipulate the data. In Java, we have to deal with the type of data must be specifically stated. For example, if we want to store an integer value, we must refer to integer need much storage space. If we want to store complex data, such as a boss to know all the information of an employee, the data also needs to be defined clearly. To store an integer, we must use one of eight kinds of basic types. And to store information of an employee, we need to write a class to describe the type of data consisting of a staff. To describe the staff by writing a class Employee, we can create a new data type, this new data type is not the Java language built-in basic data types. However, this is definitely the Employee class built-in data types and combinations of other types of components. These other classes are either our own written either in Java SE provides. For example, in Java SE, Sring class is used to represent a string, then we can use the String class can be stored in the employee's name in the Employee class. In Java SE, Date class is used to represent a calendar date, then we can be used to store the Date class entry time employees in the Employee class.

              By combining built-in data types and classes (either Java SE class or user-defined class), we can create new data types. The new data types can be used like with existing data types.

java keywords and Interpretation

Roughly meaning

Keyword meaning
abstract Members of the class or method show abstract properties
assert Assertions, used for debugging
boolean One of the basic data types, Boolean type
break Advance out of a block
byte One of the basic data types, Type Byte
case Represents a branch which is used in the switch statement,
catch In exception processing for capturing an abnormal
char One of the basic data types, character types
class Declare a class
const Reserved keywords, there is no specific meaning
continue Back to the beginning of a block
default By default, for example, used in a switch statement, indicating that a default branch
do Used in the do-while loop structure
double One of the basic data types, double precision floating point type
else Used in a conditional statement, indicating that the time when the branch condition is not satisfied
enum enumerate
extends Indicates that a type is a subtype of another type, there are common types of classes and interfaces
final Used to describe the final attribute that identifies a class can not derive subclasses or members of the method can not be covered, or the value of the member of the domain can not be changed, the constants used to define
finally Statement block for processing exceptions, to declare a substantially certain to be performed to
float One of the basic data types, single precision floating point type
for An endless guide word structure
goto Reserved keywords, there is no specific meaning
if Guide word conditional statement
implements It showed that a class implements the given interface
import To access the show specified class or package
instanceof It used to test whether an object instance of the object type is designated
int One of the basic data types, integer type
interface interface
long One of the basic data types, long integer type
native It is used to declare a method implemented by the language associated with the computer (e.g., C / C ++ / FORTRAN language)
new To create a new instance of an object
package package
private An access control mode: the private mode
protected An access control method: Protected Mode
public An access control method: a common mode
return Return data from member methods
short One of the basic data types, short integer type
static Show a static properties
strictfp Is used to declare FP_strict (single or double precision floating point) expression following the IEEE 754 arithmetic specification [1] 
super Indicates that the type of the parent object of the parent or reference to this type of construction method
switch Guide word branch statement structure
synchronized Show a piece of code need to synchronize execution
this Point to refer to the current instance of the object
throw Throw an exception
throws Member method statement currently defined exceptions thrown in all required
transient Statement not serialized member of a domain
try One possible exception is thrown in the try block
void Statement currently members of the method does not return value
volatile It indicates that two or more variables must be changed in synchronism
while Loop structure used in the

      在Java中,有三个保留字:true、false和null。从技术上说,这三个保留字是字面量值,而不是关键字。但是,它们不能被用作标识符,并且对于Java编译器来说有特定的含义。

     在Java中,我们需要标识代码中的很多元素,包括类名、方法、字段、变量、包名等。我们选择的名称就称为标识符,并且必须遵循如下规则:

  • 标识符不能是关键字或true、false、null。
  • 标识符可以包含字母、数字0-9、下划线(_)或美元符号($)。
  • 标识符的第一个字符必须是一个字母、下划线(_)或美元符号($)。
  • 标识符是区别大小写的,并且没有规定最大长度。

下面是一些合法的标识符:

 

identifier   userName1    User_name   _sys_varl    $change      Public

下面是一些非法的标识符:

 

1_Name           *system             public

 变量释义:

变量用于存储数据。在Java中,变量必须声明。我们在C语言的学习中已经知道,从本质上讲,变量就是内存中的一小块区域,我们在程序中通过使用变量名来访问该区域。因此,每个变量使用前必须先声明(也就是向JVM申请内存区域),然后必须进行赋值(也就是填充这块申请了的内存空间的内容),最后才能使用该变量。

变量声明包括两个步骤:给变量一个名字,指出变量中存储的数据的类型。例如,下面的语句都是变量声明:

 

short x;

int age;

float salary;

 

      1. 基本类型

Java语言的基本类型根据存储的数据的不同,又分为整型、浮点型、字符型、布尔型四大类。下面我们分别详细讲述。

1)整型  

在八种基本类型中,有四种是整数类型,根据其占用内存空间大小和取值范围分为:byte、short、int和long。这四种类型都是有符号的,这意味着它们既可以存储正数,又可以存储负数。

代码清单2.1所示的IntegerDemo程序演示了整型的使用。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/* 代码清单2.1 IntegerDemo.java

  整型数据类型的使用演示。

*/

public class IntegerDemo {

public static void main(String [] args) {

int x = 250;

System.out.println("x的值为:" + x);

short a, b, c;

c = 21;

b = 9;

a = (short) (b + c);                //为什么要强制转换为short?

System.out.println("a的值为:" + a);

long y = 12345678987654321L;         //注意数字结尾的"L"     

System.out.println("y的值为:" + y);

y = x;

byte s;

s = (byte) c;

System.out.println("y的值现在为:" + y + "  s的值为:" + s);

}

}

运行IntegerDemo程序,输出结果如图 2.2所示。


图2.2 程序IntegerDemo的输出

下面,我们来仔细研究一下IntegerDemo程序。我们可以看到,将x赋值为250并且显示出来是很直观的。由于在Java中可以一次声明多个变量,所以我们用一条语句声明了a、b 、c三个短整型变量。但是,在后面的求b和c的和的代码中,我们用了强制转换运算符。这是为什么呢?

在Java中执行数学表达式运算时,有如下自动类型提升规则:

  • 所有的byte型、short型和char型将被提升到int型;
  • 如果其中有一个操作数是long型,那么计算结果就是long型;
  • 如果其中有一个操作数是float型,那么计算结果就是float型;
  • 如果其中有一个操作数是double型,那么计算结果就是double型。

根据以上规则,在执行表达式b+c运算时,b和c被自动提升为int类型,所以b+c返回的也是int类型。现在我们要把b和c的和赋值给short型的a,所以必须强制转换为short型。

变量y被声明为long型,并且被赋值为一个大于32位的整型字面量。这个字面量后面加了一个L以表示它是long型的。如果忽略掉L,那么这行代码就不能通过编译。

现在我们特别关注y=x这条语句,这里y是long型的,而x是int型的。这两个变量是不同的数据类型,但是没有用到强制转换运算符。这是因为int型的数据取值范围小于long型,所以y=x赋值运算是不会丢失数据的。当y被赋值为x的时候,x的值被自动提升为long型,并存储到y中。

但是,给byte类型的s赋值为short类型的c需要强制转换运算符。short类型的c是16位,而s是8位的,所以这种赋值操作有可能会丢失数据。如果没有强制转换运算符,赋值语句将不能通过编译。

2)浮点型

基本类型中的float和double可以用于存储浮点数字。二者之间的不同之处在于所占内存空间的大小:float类型占32位,而double类型是float的两倍大小。浮点值使用IEEE 754标准格式存储。

在前面,我们知道整型字面量默认被看作是int型值,如果在字面量后面加上一个L后缀,则被看作是long型值。同样,浮点字面量默认被看作是double型值。如果我们要让浮点字面量成为float型值,就必须在字面量后加一个F后缀。

代码清单2.2所示的FloatDemo程序演示如何使用float和double数据类型。研究代码,判断程序的输出结果。

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  

/* 代码清单2.2 FloatDemo.java

  浮点型数据类型的使用演示。

*/

public class FloatDemo {

public static void main(String [] args) {

double pi = 3.14159;

float f = 2.7F;

System.out.println("pi = " + pi);

System.out.println("f = " + f);

int n = 15, d = 4;

f = n/d;

System.out.println("15/4 = " + f);

int radius = 10;

double area = pi * radius * radius;

System.out.println("area = " + area);

}

}

图2.3是该程序的实际输出。


图2.3 FloatDemo程序的输出

我们也许会对FloatDemo程序中15/4的结果感到吃惊。因为15和4都是int类型的值,所以二者的商也是int类型的3(剩余部分被截去)。将int类型的值3赋值给float类型的f,所以f的值最后是3.0。

在表达式pi * radius * radius执行计算时,double类型的pi乘以两个int类型的radius,在乘法执行之前,int值被自动提升为double类型,因此,结果就是double类型的值。

3)布尔型  

Java中boolean数据类型用来表示布尔值,它适用于逻辑运算,一般用于程序流程控制。boolean类型的变量可以是true或false。true和false都是Java中的特殊字面量。

代码清单2.3所示的BooleanDemo程序演示了boolean数据类型的使用。仔细研究该程序,并判断输出结果。

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  

/* 代码清单2.3 BooleanDemo.java

  布尔型数据类型的使用演示。

*/

 

public class BooleanDemo {

public static void main(String [] args) {

boolean t = true;

System.out.println("t的值为:" + t);

int x = 10;

boolean y = (x > 15);

System.out.println("y的值为:" + y);

// y = x;     // 不能通过编译!

}

}

在BooleanDemo程序中,t被声明为boolean类型的变量,并赋值为true。当t被作为字符串打印出来时,就显示true。boolean类型的变量y被赋值为一个表达式,该表达式计算结果为false,因为x小于15。最后,y打印出来后显示为字符串false。程序运行结果如图2.4所示。


图2.4程序BooleanDemo 的输出

在Java中,布尔类型不能是整型值,只能是true或false这两个特别的Java字面量,这点与C语言中有所不同。必须注意,在BooleanDemo程序中,布尔类型的y不能赋值给int型的x,即使用强制转换也不行。

4)字符型

char数据类型表示Java中的字符。char类型的数据所占内存空间大小为16位。char类型数据可以被看作是整数值,也就是说我们可以使用char类型数据执行算术运算和大小比较运算。

在Java中,可以用单引号来表示字符字面量。例如,代码中的字面量'A'将被视作为一个字符。如果一个字面量出现在双引号中,例如"A",那么它就不是一个字符,而是一个字符串。

有些不可打印的字符可以用转义符描述。表2-3列出了常用的转移字符。

表2-3 Java的转义字符

字 符

含  义

Unicode表示符

\b

退格

\u0008

\t

制表符

\u0009

\n

换行

\u000a

\r

回车

\u000d

\"

双引号

\u0022

\'

单引号

\u0027

\\

反斜杠

\u005c

如果我们需要用一个字符的Unicode值来描述该字符,可以用转义符\u后跟字符的十六进制格式的Unicode值。

代码清单2.4所示的CharDemo程序演示了char类型的使用以及字符常量。仔细研究该段代码,并判断其输出。

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  

/* 代码清单2.4 CharDemo.java

  字符型数据类型的使用演示。

*/

public class CharDemo {

public static void main(String [] args) {

char a = 'A';

char b = (char) (a + 1);

System.out.println(a + b);

System.out.println("a + b的值为:" + a + b);

int x = 75;

char y = (char) x;

char half = '\u00AB';

System.out.println("y的值为:" + y + ", half的值为:" + half);

}

}

上述程序的运行结果如图 2.5所示。


图2.5 CharDemo程序的输出

在CharDemo程序中,a和b被声明为char类型变量。当b被赋值为(a+1)时,需要用强制转换运算符,因为a加上1的结果是int类型的值。在IntegerDemo程序中,当两个short类型的和为int类型时,我们已经看到了这种自动类型提升。在执行算术运算前,Java会自动将较小的整数类型提升为int类型的值。

(a+b)的结果为两个int类型值的和,即65+66,结果为131。第二个println()语句不是对'A'和'B'求和,而是连接两个字符,连接的结果是一个字符串"AB"。

注意,在CharDemo程序中,int型的x被强制转换为char类型。数值75对应的字符是'K',所以y的值为'K'。变量half演示了转义字符'\u',字符'\u00AB'是字符1/2。

5)字符串

字符串是一序列的字符。在Java中,字符串不是基本类型,因而需要用类来表示。Java有一个名为String的类来表示字符串对象。比起C语言中需要用字符数组来表示字符串相比,Java中字符串的处理更简单。

对于Java代码中的字符串字面量,JVM会自动为我们创建一个String对象。例如,假如我们有如下pringln()语句:

 

System.out.println("Hello, World");

在运行时,字符串字面量"Hello, World"被转换成一个String对象,然后被传递给println()方法。考虑如下的语句:

 

int x = 10;

System.out.println("x = " + x);

这里,字符串字面量"x="被转换为String对象。于是运算符“+”就成为字符串连接运算符,所以变量x需要转换为String对象,然后连接到“x=”以创建第三个String对象“x=10”。最后,是这第三个String对象传给println()方法。

在Java中,每个要与String对象连接的基本类型将被自动转换为一个新的String对象。这简化了基本类型的处理过程以及显示和输出。实际上,因为Java中的每个对象都有toString()方法,所以Java中的任何对象都可以转换为String对象。我们将在《继承》一章中详细讨论toString()方法。

代码清单2.5所示的StringDemo程序演示了字符串字面量和String对象的用法。

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  

/* 代码清单2.5 StringDemo.java

  字符串常量与String对象的使用演示。

*/

public class StringDemo {

public static void main(String [] args) {

String first = "Rich", last = "Raposa";

String name = first + " " + last;

System.out.println("Name = " + name);

double pi = 3.14159;

String s = "Hello, " + first;

System.out.println(s + pi + 7);

System.out.println(pi + 7 + s);

}

}

          图2.6是StringDemo程序执行后的输出结果。


          图2.6 StringDemo程序的输出

          在StringDemo程序中,总共有五个字符串字面量:"Rich"、"Raposa"、" "、"Name = "和 "Hello, "。每个字面量都被转换为一个String对象。所以,first + " " + last是三个String对象进行连接。同样,"Name = " + name是两个String对象连接。

          在StringDemo程序中,我们特地添加了最后两个println()语句,来演示操作顺序的重要性。当表达式s + pi +7被计算时,首先计算s+pi(这是字符串连接,而不是加法运算),随后新的字符串与7连接,创建字符串"Hello, Rich3.141597"。

           在最后一个println()语句中,顺序改变了,首先计算的是pi+7。因为7是整型字面量,从而被当作是一个int类型数据。所以,pi=7是一个double类型数据加上一个int类型数据,7被提升为double型的,执行加法运算,计算结果是double类型数据10.14159。然后,这个double类型数据与s执行字符串连接,创建字符串"10.14159Hello, Rich"。

           在Java中,一个String对象是不可变的,这意味着用一个String对象表示的字符串是不能被改变的。例如,StringDemo程序声明了一个称为name的String对象,并给其赋值为字面量"Rich"。此后,字符串"Rich"就不能被修改。例如,如果我们想让name的值为"RICH",就必须给name赋值为一个新的字符串对象"RICH",而不能改变name的单个字符。

 

 

Guess you like

Origin blog.csdn.net/qq_45097560/article/details/90644993