双语版Java程序设计
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

3.1 Data Types Overview

Java is known as a strongly typed language. It means that Java is a language that will only accept specific values within specific variables or parameters. Some languages, such as JavaScript, are weakly typed languages. This means that you can readily store whatever you want into a variable. Here is an example of the difference between strongly typed and weakly typed languages:

JavaScript (weakly typed)(Java脚本)

1: var x; // Declare a variable

2: x = 1; // Legal

3: x = "Test"; // Legal

4: x = true; // Legal

Java (strongly typed)

1: int x; // Declare a variable of type int

2: x = 1; // Legal

3: x = "Test" // Compiler Error

4: x = true; // Compiler Error

In a weakly typed language, such as JavaScript, you simply declare a variable without assigning it a type. In a strongly typed language, such as Java, you must give a variable a type when you declare it. Once you’ve declared a variable to be that type, it will remain of that type definitely and will only accept values that are within that types range. You should note that this is one of the many differences between Java and JavaScript. Despite their names, they have very little to do with one another.

Java是一种强类型编程语言。当声明一个变量的时候,必须给出该变量的类型。一旦给定了类型,所赋予的值必须在相应的精度内。

Now that we know that Java is a strongly typed language, you can probably see how important it is to know what data types there are in Java. There are 9 data types in Java, 8 primitive types and a reference type. First, let’s look at primitive types and then we’ll move along to reference types.

Java是强制类型检查的语言。Java中有9种数据类型,其中8种是基本类型,一种是引用类型。