亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
1. Java’s eight basic data types
2. Constants in Java
3. 數(shù)據(jù)類型之間的轉(zhuǎn)換
4.Java引用類型
Home Java Javagetting Started What are the eight major data types in Java

What are the eight major data types in Java

Feb 02, 2023 am 10:17 AM
java

Eight major data types: 1. byte (bit), the maximum data storage capacity is 255; 2. short (short integer), the maximum data storage capacity is 65536; 3. int (integer), the maximum data storage capacity It is 2 to the 32nd power minus 1; 4. long (long integer), the maximum data storage capacity is 2 to the 64th power minus 1; 5. float (single-precision floating number), f must be added after the number when assigning a value directly. or F; 6. double (double precision); 7. boolean (Boolean type); 8. char (character).

What are the eight major data types in Java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

1. Java’s eight basic data types

There are eight basic Java types. Basic types can be divided into three categories: character type char, boolean type boolean and numerical value Types byte, short, int, long, float, double. Numeric types can be divided into integer types byte, short, int, long and floating point types float and double. There are no unsigned numerical types in JAVA. Their value range is fixed and will not change with changes in the machine hardware environment or operating system. In fact, there is another basic type void in JAVA, which also has a corresponding packaging class java.lang.Void, but we cannot directly operate on them. 8 Medium type representation range is as follows:

byte: 8 bits, the maximum amount of stored data is 255, and the stored data range is between -128~127.

short: 16 bits, the maximum storage capacity is 65536, and the data range is -32768~32767.

#int: 32 bits, the maximum data storage capacity is 2 to the 32nd power minus 1, the data range is negative 2 to the 31st power to positive 2 to the 31st power minus 1 .

long: 64 bits, the maximum data storage capacity is 2 to the power of 64 minus 1, the data range is negative 2 to the power of 63 to positive 2 to the power of 63 minus 1 .

float: 32 bits, data range is 3.4e-45~1.4e38, f or F must be added after the number when assigning directly.

double: 64 bits, data range is 4.9e-324~1.8e308, d or D can be added or not added when assigning.

boolean: There are only two values: true and false.

char: 16 bits, stores Unicode code, assign value with single quotes.

Java determines the size of each simple type. These sizes do not change with changes in machine architecture. This immutability of size is one of the reasons why Java programs are highly portable. The following table lists the simple types defined in Java, the number of binary digits they occupy, and the corresponding wrapper classes.

##Binary digits18161632643264##DoubleVoid

For the value range of the basic types of numerical types, we do not need to be forced to remember, because their values ??have been defined in the corresponding packaging class in the form of constants. For example:

Basic type byte Binary digits: Byte.SIZE Minimum value: Byte.MIN_VALUE Maximum value: Byte.MAX_VALUE

Basic type short Binary digits: Short.SIZE Minimum value: Short .MIN_VALUE Maximum value: Short.MAX_VALUE

Basic type char Binary digits: Character.SIZE Minimum value: Character.MIN_VALUE Maximum value: Character.MAX_VALUE

Basic type double Binary digits: Double .SIZE minimum value: Double.MIN_VALUE maximum value: Double.MAX_VALUE

Note: The minimum values ??of float and double types are not the same as the values ??of Float.MIN_VALUE and Double.MIN_VALUE. In fact, Float.MIN_VALUE and Double.MIN_VALUE refer to the smallest positive number that can be represented by float and double types respectively. That is to say, there is a situation where the float type cannot represent the value between 0 and ±Float.MIN_VALUE, and the double type cannot represent the value between 0 and ±Double.MIN_VALUE. This is not surprising since values ??in these ranges are outside their range of precision.

The minimum and maximum values ??of Float and Double are output in scientific notation. The "E number" at the end indicates how many times the number before E should be multiplied by 10. For example, 3.14E3 is 3.14×1000=3140, and 3.14E-3 is 3.14/1000=0.00314.

Java basic types are stored on the stack, so their access speeds are faster than instance objects of corresponding wrapper classes stored in the heap. Starting from Java5.0 (1.5), the JAVA virtual machine (JavaVirtual Machine) can complete automatic conversion between basic types and their corresponding wrapper classes. Therefore, we use their wrapper classes just like basic types when doing assignments, parameter transfers, and mathematical operations, but this does not mean that you can call methods that are only available in their wrapper classes through basic types. In addition, the wrapper classes of all basic types (including void) use final modification, so we cannot inherit them to extend new classes, nor can we override any of their methods.

Advantages of basic types: data storage is relatively simple and operation efficiency is relatively high

Advantages of packaging classes: some are easy, for example, the elements of a collection must be object types, which satisfies everything in Java The idea of ??objects

2. Constants in Java

Hexadecimal integer constants: When expressed in hexadecimal, they need to start with 0x or 0X. Such as 0xff,0X9A.

Octal integer constant: Octal must start with 0, such as 0123, 034.

Long integer type: Long integer type must end with L, such as 9L, 342L.

Floating point constants: Since the default type of decimal constants is double, f (F) must be added after the float type. Variables with decimals also default to double type.

For example:

float f;
f=1.3f;//必須聲明f。

Character constants: Character constants need to be enclosed in two single quotes (note that string constants are enclosed in two double quotes). Characters in Java take up two bytes. Some commonly used escape characters:

①\r means accepting keyboard input, which is equivalent to pressing the Enter key;

②\n表示換行;

③\t表示制表符,相當(dāng)于Table鍵;

④\b表示退格鍵,相當(dāng)于Back Space鍵;

⑤\'表示單引號(hào);

⑥\''表示雙引號(hào);

⑦\(yùn)\表示一個(gè)斜杠\。

3. 數(shù)據(jù)類型之間的轉(zhuǎn)換

1).簡(jiǎn)單類型數(shù)據(jù)間的轉(zhuǎn)換,有兩種方式:自動(dòng)轉(zhuǎn)換和強(qiáng)制轉(zhuǎn)換,通常發(fā)生在表達(dá)式中或方法的參數(shù)傳遞時(shí)。

自動(dòng)轉(zhuǎn)換

具體地講,當(dāng)一個(gè)較"小"數(shù)據(jù)與一個(gè)較"大"的數(shù)據(jù)一起運(yùn)算時(shí),系統(tǒng)將自動(dòng)將"小"數(shù)據(jù)轉(zhuǎn)換成"大"數(shù)據(jù),再進(jìn)行運(yùn)算。而在方法調(diào)用時(shí),實(shí)際參數(shù)較"小",而被調(diào)用的方法的形式參數(shù)數(shù)據(jù)又較"大"時(shí)(若有匹配的,當(dāng)然會(huì)直接調(diào)用匹配的方法),系統(tǒng)也將自動(dòng)將"小"數(shù)據(jù)轉(zhuǎn)換成"大"數(shù)據(jù),再進(jìn)行方法的調(diào)用,自然,對(duì)于多個(gè)同名的重載方法,會(huì)轉(zhuǎn)換成最"接近"的"大"數(shù)據(jù)并進(jìn)行調(diào)用。這些類型由"小"到"大"分別為 (byte,short,char)--int--long--float—double。這里我們所說(shuō)的"大"與"小",并不是指占用字節(jié)的多少,而是指表示值的范圍的大小。

①下面的語(yǔ)句可以在Java中直接通過(guò):

byte b;
int i=b; 
long l=b; 
float f=b; 
double d=b;

②如果低級(jí)類型為char型,向高級(jí)類型(整型)轉(zhuǎn)換時(shí),會(huì)轉(zhuǎn)換為對(duì)應(yīng)ASCII碼值,例如

char c='c'; int i=c;
System.out.println("output:"+i);

輸出:output:99;

③對(duì)于byte,short,char三種類型而言,他們是平級(jí)的,因此不能相互自動(dòng)轉(zhuǎn)換,可以使用下述的強(qiáng)制類型轉(zhuǎn)換。

short i=99 ; 
char c=(char)i; 
System.out.println("output:"+c);

輸出:output:c;

強(qiáng)制轉(zhuǎn)換

將"大"數(shù)據(jù)轉(zhuǎn)換為"小"數(shù)據(jù)時(shí),你可以使用強(qiáng)制類型轉(zhuǎn)換。即你必須采用下面這種語(yǔ)句格式: int n=(int)3.14159/2;可以想象,這種轉(zhuǎn)換肯定可能會(huì)導(dǎo)致溢出或精度的下降。

2)表達(dá)式的數(shù)據(jù)類型自動(dòng)提升, 關(guān)于類型的自動(dòng)提升,注意下面的規(guī)則。

①所有的byte,short,char型的值將被提升為int型;

②如果有一個(gè)操作數(shù)是long型,計(jì)算結(jié)果是long型;

③如果有一個(gè)操作數(shù)是float型,計(jì)算結(jié)果是float型;

④如果有一個(gè)操作數(shù)是double型,計(jì)算結(jié)果是double型;

例, byte b; b=3; b=(byte)(b*3);//必須聲明byte。

3)包裝類過(guò)渡類型轉(zhuǎn)換

一般情況下,我們首先聲明一個(gè)變量,然后生成一個(gè)對(duì)應(yīng)的包裝類,就可以利用包裝類的各種方法進(jìn)行類型轉(zhuǎn)換了。例如:

①當(dāng)希望把float型轉(zhuǎn)換為double型時(shí):

float f1=100.00f;
Float F1=new Float(f1);
double d1=F1.doubleValue();//F1.doubleValue()為Float類的返回double值型的方法

②當(dāng)希望把double型轉(zhuǎn)換為int型時(shí):

double d1=100.00;
Double D1=new Double(d1);
int i1=D1.intValue();

簡(jiǎn)單類型的變量轉(zhuǎn)換為相應(yīng)的包裝類,可以利用包裝類的構(gòu)造函數(shù)。即:Boolean(boolean value)、Character(char value)、Integer(int value)、Long(long value)、Float(float value)、Double(double value)

而在各個(gè)包裝類中,總有形為××Value()的方法,來(lái)得到其對(duì)應(yīng)的簡(jiǎn)單類型數(shù)據(jù)。利用這種方法,也可以實(shí)現(xiàn)不同數(shù)值型變量間的轉(zhuǎn)換,例如,對(duì)于一個(gè)雙精度實(shí)型類,intValue()可以得到其對(duì)應(yīng)的整型變量,而doubleValue()可以得到其對(duì)應(yīng)的雙精度實(shí)型變量。

4)字符串與其它類型間的轉(zhuǎn)換

其它類型向字符串的轉(zhuǎn)換

①調(diào)用類的串轉(zhuǎn)換方法:X.toString();

②自動(dòng)轉(zhuǎn)換:X+"";

③使用String的方法:String.volueOf(X);

字符串作為值,向其它類型的轉(zhuǎn)換

①先轉(zhuǎn)換成相應(yīng)的封裝器實(shí)例,再調(diào)用對(duì)應(yīng)的方法轉(zhuǎn)換成其它類型

例如,字符中"32.1"轉(zhuǎn)換double型的值的格式為:new Float("32.1").doubleValue()。也可以用:Double.valueOf("32.1").doubleValue()

②靜態(tài)parseXXX方法

String s = "1";
byte b = Byte.parseByte( s );
short t = Short.parseShort( s );
int i = Integer.parseInt( s );
long l = Long.parseLong( s );
Float f = Float.parseFloat( s );
Double d = Double.parseDouble( s );

③Character的getNumericValue(char ch)方法

5)Date類與其它數(shù)據(jù)類型的相互轉(zhuǎn)換

整型和Date類之間并不存在直接的對(duì)應(yīng)關(guān)系,只是你可以使用int型為分別表示年、月、日、時(shí)、分、秒,這樣就在兩者之間建立了一個(gè)對(duì)應(yīng)關(guān)系,在作這種轉(zhuǎn)換時(shí),你可以使用Date類構(gòu)造函數(shù)的三種形式:

①Date(int year, int month, int date):以int型表示年、月、日

②Date(int year, int month, int date, int hrs, int min):以int型表示年、月、日、時(shí)、分

③Date(int year, int month, int date, int hrs, int min, int sec):以int型表示年、月、日、時(shí)、分、秒

在長(zhǎng)整型和Date類之間有一個(gè)很有趣的對(duì)應(yīng)關(guān)系,就是將一個(gè)時(shí)間表示為距離格林尼治標(biāo)準(zhǔn)時(shí)間1970年1月1日0時(shí)0分0秒的毫秒數(shù)。對(duì)于這種對(duì)應(yīng)關(guān)系,Date類也有其相應(yīng)的構(gòu)造函數(shù):Date(long date)。

獲取Date類中的年、月、日、時(shí)、分、秒以及星期你可以使用Date類的getYear()、getMonth()、getDate()、getHours()、getMinutes()、getSeconds()、getDay()方法,你也可以將其理解為將Date類轉(zhuǎn)換成int。

而Date類的getTime()方法可以得到我們前面所說(shuō)的一個(gè)時(shí)間對(duì)應(yīng)的長(zhǎng)整型數(shù),與包裝類一樣,Date類也有一個(gè)toString()方法可以將其轉(zhuǎn)換為String類。

有時(shí)我們希望得到Date的特定格式,例如20020324,我們可以使用以下方法,首先在文件開(kāi)始引入,

import java.text.SimpleDateFormat;
import java.util.*;
java.util.Date date = new java.util.Date();
 
//如果希望得到Y(jié)YYYMMDD的格式
SimpleDateFormat sy1=new SimpleDateFormat("yyyyMMDD");
String dateFormat=sy1.format(date);
 
//如果希望分開(kāi)得到年,月,日
SimpleDateFormat sy=new SimpleDateFormat("yyyy");
SimpleDateFormat sm=new SimpleDateFormat("MM");
SimpleDateFormat sd=new SimpleDateFormat("dd");
String syear=sy.format(date);
String smon=sm.format(date);
String sday=sd.format(date);

總結(jié):只有boolean不參與數(shù)據(jù)類型的轉(zhuǎn)換

(1).自動(dòng)類型的轉(zhuǎn)換:a.常數(shù)在表數(shù)范圍內(nèi)是能夠自動(dòng)類型轉(zhuǎn)換的

b.數(shù)據(jù)范圍小的能夠自動(dòng)數(shù)據(jù)類型大的轉(zhuǎn)換(注意特例)

int到float,long到float,long到double 是不會(huì)自動(dòng)轉(zhuǎn)換的,不然將會(huì)丟失精度

c.引用類型能夠自動(dòng)轉(zhuǎn)換為父類的

d.基本類型和它們包裝類型是能夠互相轉(zhuǎn)換的

(2).強(qiáng)制類型轉(zhuǎn)換:用圓括號(hào)括起來(lái)目標(biāo)類型,置于變量前

4.Java引用類型

Java有 5種引用類型(對(duì)象類型):類 接口 數(shù)組 枚舉 標(biāo)注

引用類型:底層結(jié)構(gòu)和基本類型差別較大

JVM的內(nèi)存空間:(1). Heap 堆空間:分配對(duì)象 new Student()

(2). Stack ??臻g:臨時(shí)變量 Student stu

(3).Code 代碼區(qū) :類的定義,靜態(tài)資源 Student.class

eg:Student stu = new Student(); //new 在內(nèi)存的堆空間創(chuàng)建對(duì)象

stu.study(); //把對(duì)象的地址賦給stu引用變量

上例實(shí)現(xiàn)步驟:a.JVM加載Student.class 到Code區(qū)

???? b.new Student()在堆空間分配空間并創(chuàng)建一個(gè)Student實(shí)例;

???? c.將此實(shí)例的地址賦值給引用stu, ??臻g;

更多編程相關(guān)知識(shí),請(qǐng)?jiān)L問(wèn):編程教學(xué)!!

#Simple type

boolean

byte

char

short

Int

long

float

double

void

--

Wrapper class

Boolean

Byte

Character

Short

Integer

Long

##Float

The above is the detailed content of What are the eight major data types in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

python itertools combinations example python itertools combinations example Jul 31, 2025 am 09:53 AM

itertools.combinations is used to generate all non-repetitive combinations (order irrelevant) that selects a specified number of elements from the iterable object. Its usage includes: 1. Select 2 element combinations from the list, such as ('A','B'), ('A','C'), etc., to avoid repeated order; 2. Take 3 character combinations of strings, such as "abc" and "abd", which are suitable for subsequence generation; 3. Find the combinations where the sum of two numbers is equal to the target value, such as 1 5=6, simplify the double loop logic; the difference between combinations and arrangement lies in whether the order is important, combinations regard AB and BA as the same, while permutations are regarded as different;

Mastering Dependency Injection in Java with Spring and Guice Mastering Dependency Injection in Java with Spring and Guice Aug 01, 2025 am 05:53 AM

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

python pytest fixture example python pytest fixture example Jul 31, 2025 am 09:35 AM

fixture is a function used to provide preset environment or data for tests. 1. Use the @pytest.fixture decorator to define fixture; 2. Inject fixture in parameter form in the test function; 3. Execute setup before yield, and then teardown; 4. Control scope through scope parameters, such as function, module, etc.; 5. Place the shared fixture in conftest.py to achieve cross-file sharing, thereby improving the maintainability and reusability of tests.

A Guide to Java Flight Recorder (JFR) and Mission Control A Guide to Java Flight Recorder (JFR) and Mission Control Jul 31, 2025 am 04:42 AM

JavaFlightRecorder(JFR)andJavaMissionControl(JMC)providedeep,low-overheadinsightsintoJavaapplicationperformance.1.JFRcollectsruntimedatalikeGCbehavior,threadactivity,CPUusage,andcustomeventswithlessthan2%overhead,writingittoa.jfrfile.2.EnableJFRatsta

Best Practices for Writing Maintainable Java Code Best Practices for Writing Maintainable Java Code Jul 31, 2025 am 06:21 AM

Follow naming specifications to make the code as easy to read as prose; 2. The method should be small and focused, and a single responsibility is easy to test and reuse; 3. Write meaningful comments to explain "why", rather than obvious operations; 4. Prioritize immutability and packaging to prevent external accidental modifications; 5. Exceptions should be properly handled without ignoring and providing clear information; 6. Unit tests should be clearly named and cover key paths; 7. Reasonable use of modern Java features such as var and Stream to improve readability; 8. Organization of package structures layered by functions to improve project navigation efficiency - these practices jointly ensure that Java code is maintained for a long time.

Laravel error and exception handling Laravel error and exception handling Jul 31, 2025 am 11:57 AM

Laravel's error and exception handling mechanism is based on the PHP exception system and Symfony component, and is managed uniformly by the App\Exceptions\Handler class. 1. Record exceptions through the report() method, such as integrating Sentry and other monitoring services; 2. Convert exceptions into HTTP responses through the render() method, supporting custom JSON or page jumps; 3. You can create custom exception classes such as PaymentFailedException and define their response format; 4. Automatically handle verification exception ValidationException, and manually adjust the error response structure; 5. Decide whether to display details based on the APP_DEBUG configuration.

See all articles