Java中的数据类型

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.

Java语言具有丰富的数据类型实现。 数据类型指定大小和可以存储在标识符中的值的类型。

In java, data types are classified into two catagories :

在Java中,数据类型分为两类:

  1. Primitive Data type

    原始数据类型

  2. Non-Primitive Data type

    非原始数据类型

1)原始数据类型 (1) Primitive Data type)

A primitive data type can be of eight types :

基本数据类型可以有八种类型:

Primitive Data types
charbooleanbyte shortintlongfloat double
原始数据类型
char boolean byte short int long float double

Once a primitive data type has been declared its type can never change, although in most cases its value can change. These eight primitive type can be put into four groups

声明原始数据类型后,其类型就永远无法更改,尽管在大多数情况下其值可以更改。 这八个原始类型可以分为四组

整数 (Integer)

This group includes byte, short, int, long

该组包括byteshortintlong

byte : It is 1 byte(8-bits) integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;

byte:这是1个字节(8位)的整数数据类型。 值范围从-128到127。默认值为零。 示例: byte b=10;

short : It is 2 bytes(16-bits) integer data type. Value range from -32768 to 32767. Default value zero. example: short s=11;

short:是2字节(16位)的整数数据类型。 值范围从-32768到32767。默认值为零。 例如: short s=11;

int : It is 4 bytes(32-bits) integer data type. Value range from -2147483648 to 2147483647. Default value zero. example: int i=10;

int: 4字节(32位)整数数据类型。 值范围为-2147483648至2147483647。默认值为零。 例如: int i=10;

long : It is 8 bytes(64-bits) integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;

long:是8个字节(64位)的整数数据类型。 值范围从-9,223,372,036,854,775,808到9,223,372,036,854,775,807。 默认值为零。 例如: long l=100012;

Example:

例:

Lets create an example in which we work with integer type data and we can get idea how to use datatype in the java program.

让我们创建一个示例,在其中使用整数类型数据,我们可以了解如何在Java程序中使用数据类型。

package corejava;

public class Demo{      
    
    public static void main(String[] args) {
    	// byte type
    	byte b = 20;
    	System.out.println("b= "+b);
    	
    	// short type
    	short s = 20;
    	System.out.println("s= "+s);
    	
    	// int type
    	int i = 20;
    	System.out.println("i= "+i);
    	
    	// long type
    	long l = 20;
    	System.out.println("l= "+l);
    	
   
    }  
}

b= 20 s= 20 i= 20 l= 20

b = 20 s = 20 i = 20 l = 20

浮点数 (Floating-Point Number)

This group includes float, double

该组包括floatdouble

float : It is 4 bytes(32-bits) float data type. Default value 0.0f. example: float ff=10.3f;

float:这是4个字节(32位)的float数据类型。 默认值为0.0f。 例如: float ff=10.3f;

double : It is 8 bytes(64-bits) float data type. Default value 0.0d. example: double db=11.123;

double:它是8个字节(64位)的float数据类型。 默认值为0.0d。 例如: double db=11.123;

Example:

例:

In this example, we used floating point type and declared variables to hold floating values. Floating type is useful to store decimal point values.

在此示例中,我们使用浮点类型和声明的变量来保存浮点值。 浮动类型对于存储小数点值很有用。

public class Demo{      
    
    public static void main(String[] args) {
    	// float type
    	float f = 20.25f;
    	System.out.println("f= "+f);
    	
    	// double type
    	double d = 20.25;
    	System.out.println("d= "+d);
   
    }  
}

f= 20.25 d= 20.25

f = 20.25 d = 20.25

性格 (Characters)

This group represent char, which represent symbols in a character set, like letters and numbers.

该组代表char ,代表字符集中的符号,例如字母和数字。

char : It is 2 bytes(16-bits) unsigned unicode character. Range 0 to 65,535. example: char c='a';

char:这是2个字节(16位)的无符号unicode字符。 范围从0到65,535。 例如: char c='a';

字符类型示例 ( Char Type Example )

Char type in Java uses 2 bytes to unicode characters. Since it works with unicode then we can store alphabet character, currency character or other characters that are comes under the unicode set.

Java中的Char类型使用2个字节对​​字符进行unicode。 由于它可与unicode一起使用,因此我们可以存储字母字符,货币字符或unicode集下的其他字符。

public class Demo {

    public static void main(String[] args) {


        char ch = 'S';
        System.out.println(ch);
        
        char ch2 = '&';
        System.out.println(ch2);
        
        char ch3 = '$';
        System.out.println(ch3);

    }

}

S & $

S&$

布尔型 (Boolean)

This group represent boolean, which is a special type for representing true/false values. They are defined constant of the language. example: boolean b=true;

该组表示boolean ,这是表示true / false值的特殊类型。 它们是语言的常量。 例如: boolean b=true;

布尔类型示例 (Boolean Type Example)

Boolean type in Java works with two values only either true or false. It is mostly use in conditional expressions to perform conditional based programming.

Java中的布尔类型只能使用两个值true或false。 它主要用于条件表达式中以执行基于条件的编程。

public class Demo {

    public static void main(String[] args) {

        boolean t = true;
        System.out.println(t);
        
        boolean f = false;
        System.out.println(f);

    }

}

true false

真假

2)非原始(参考)数据类型 (2) Non-Primitive(Reference) Data type)

A reference data type is used to refer to an object. A reference variable is declare to be of specific and that type can never be change.

引用数据类型用于引用对象。 引用变量声明为特定变量,并且该类型永远不能更改。

For example: String str, here str is a reference variable of type String. String is a class in Java. We will talk a lot more about reference data type later in Classes and Object lesson.

例如: String str ,这里str是String类型的引用变量。 字符串是Java中的类。 稍后在“类和对象”课程中,我们将更多地讨论参考数据类型。

Reference type are used to hold reference of an object. Object can be instance of any class or entity.

引用类型用于保存对象的引用。 对象可以是任何类或实体的实例。

In object oriented system, we deal with object that stores properties. To refer those objects we use reference types.

在面向对象的系统中,我们处理存储属性的对象。 为了引用那些对象,我们使用引用类型。

We will talk a lot more about reference data type later in Classes and Object lesson.

稍后在“类和对象”课程中,我们将更多地讨论参考数据类型。

Java中的标识符 (Identifiers in Java)

All Java components require names. Name used for classes, methods, interfaces and variables are called Identifier. Identifier must follow some rules. Here are the rules:

所有Java组件都需要名称。 用于类,方法,接口和变量的名称称为Identifier 。 标识符必须遵循一些规则。 规则如下:

  • All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore.

    所有标识符必须以字母(a到z或A到Z)或货币字符($)或下划线开头。

  • After the first character, an identifier can have any combination of characters.

    在第一个字符之后,标识符可以具有任何字符组合。

  • Java keywords cannot be used as an identifier.

    Java 关键字不能用作标识符。

  • Identifiers in Java are case sensitive, foo and Foo are two different identifiers.

    Java中的标识符区分大小写,foo和Foo是两个不同的标识符。

Some valid identifiers are: int a, class Car, float amount etc.

一些有效的标识符是: int a,汽车类,浮动量等。

翻译自: https://www.studytonight.com/java/datatypes-and-identifier.php

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值