Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define and use Java data types

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to define and use Java data types", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to define and use Java data types" article.

Identifier and keyword identifier

Pronunciation biao zhi fu

What is an identifier?

Packages, classes, variables, methods... Wait, as long as it's named, that name is the identifier.

Rules for defining identifiers

Four can: can be numbers, letters, underscores (_), dollar sign ($), we generally choose to use English letters as much as possible.

Two no: you can't start with a number, and you can't use keywords in java.

See the name: so that the reader can understand what it is by name, such as Bubble sorting (bubble_Sort), we can clearly know that this method is Bubble sorting.

Hump naming:

Class name: the first letter is capitalized and the rest is named after the hump.

Method name / variable name: the first letter is lowercase, the rest follow the hump name

Package name: all lowercase

Length limit: there is no limit to length, but too long is not recommended

Keyword

A word that is given a special meaning by the Java language and used for a special purpose

Features: all keywords in Java are lowercase

Official website: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

Constant and variable constant

There are two kinds of constants:

A constant usually refers to a fixed value, such as 1, 2, 3,'a','b', true, false, "helloWorld", and so on.

In the Java language, the keyword final is mainly used to define a constant. The value of a constant cannot be changed once it has been initialized.

In order to better distinguish and express, 1, 2, 3,'a','b', true, false, "helloWorld" and so on are generally called literal constants, while PI modified by final is called symbolic constants (character constants).

Type of literal constant:

Integer constant: 123 23

Real constant: 3.1415926

Character constant:'a''b'

Logical constant: true false

String constant: "helloworld"

Note: logical constants have only two values, one is true and the other is false

Variable

A variable essentially represents an operable storage space, and the location of the space is determined, but what value is placed in it is uncertain. We can manipulate the value stored in this storage space by accessing the corresponding storage space through the variable name.

Java is a strongly typed language, and each variable must declare its data type. The data type of the variable determines the amount of storage space occupied by the variable. For example, int axiom 3; means that the space size of the a variable is 4 bytes. Variable is the most basic storage unit in the program, and its elements include variable name, variable type and scope. Variables must be declared before they are used, and only after the variables are declared can the corresponding length of storage space be allocated to them.

Declaration format of variables

Data type variable name = initial value, variable name = initial value.

For example:

Public class TestCode01 {public static void main (String [] args) {int axiom 3 recordbcentury 4 record5; / / you can also define a declaration of a variable that does not assign a value to int aperior bjournal c;}}.

If you define only one variable and do not assign a value to it, then the variable is actually undefined.

If the variable is not assigned, there will be an error when using it, telling you that the variable has not been initialized

Public class TestCode01 {public static void main (String [] args) {int a; System.out.println (a); / / will report an error, uninitialized variable a}}

Assignment of variables

Int axiom 10 alter / directly define and assign int bincedure / first define in the assignment bpm 20

Variables cannot be defined repeatedly

Basic data type integer type

Integer data type:

Here is an example of the code:

Public class TestCode01 {public static void main (String [] args) {/ / defines a variable of integer type: / / when assigning a value to a variable, the value can be in different bases: int num1 = 12; / / by default, the assignment is decimal System.out.println (num1); int num2 = 012 / / preceded by 0, the value is octal System.out.println (num2); int num3 = 0x12 suicide bank / preceded by 0x or 0X, this value is hexadecimal System.out.println (num3); int num4 = 0b10 / / preceded by 0x or 0B, this value is the binary System.out.println (num4); / / defines a variable of type byte: byte b = 126. / defines a variable of type byte, named b, assigned to 12 System.out.println (b). / / Note: out-of-range assignments will report an error. Short s = 30000; System.out.println (s); int I = 1234; System.out.println (I) / / the integer type is int by default, so 12345678910 is a number of type int. For type int, it is out of range / / if you want to give a number to a variable of type long, add L (recommended) or l to it and long num5 = 12345678910L; System.out.println (num5) / / Note: you need to add L only after this number is out of the range of int type, otherwise you can assign a value to long type without adding L: long num6 = 12; System.out.println (num6);}} floating point type floating point type constant

(1) Decimal number form, for example: 3.14 314.0 0.314

(2) the form of scientific notation, such as:

/ / 314e2 314E2 (E is not case sensitive) 314E-2double f = 314e2; / / 31410 ^ 2-> 31400.0double f2 = 314eMur2; / / 31410 ^ (- 2)-> 3.14 floating point type variables

Float type is also known as single-precision type, Mantissa can be accurate to 7 significant digits, in many cases, the accuracy of float type is difficult to meet the requirements.

Double indicates that this type is about twice as accurate as the float type, also known as the double precision type, and the vast majority of applications use the double type.

Values of type float have a suffix F or f, and floating-point values without the suffix f f default to type double.

You can also add a suffix D or d after a floating-point value to make it clear that it is of type double.

PS: a significant number refers to the first non-zero number from the left to the last number

Public class TestCode02 {public static void main (String [] args) {/ / constant of floating point type has two forms: / / decimal form: double num1 = 3.14; System.out.println (num1); / / Scientific numeration form: double num2 = 314E-2 System.out.println (num2); / / variable of floating point type: / / Note: floating point type is double by default. To assign a number of double type to float type, you must add F or f float F1 = 3.14234567898623F; System.out.println (F1) / Note: double type can be followed by D or d, but generally we omit double D1 = 3.14234567898623D; System.out.println (D1); / / Note: we'd better not compare floating point types: float f2 = 0.3F Double D2 = 0.3; System.out.println (f2==d2) / * difference: = assignment operation: assign the value on the right side of the equal sign to the left side of the equal sign = to determine whether the values on the left and right sides are equal: the result is either equal or unequal the result of the = = operator is either true Either false * /}} character type character type

Java characters are represented by 16-bit Unicode coding, while computer languages usually use ASCII codes, representing a character with 8 bits.

A character type is a character enclosed in two single quotation marks, such as' axiomagery'. Where'a 'and' A 'represent the characters an and An in the ASCII code, respectively, and' 1' represents character type 1, not the integer value 1. In addition to general characters, Java defines some special characters, as shown in the figure:

In addition to the fact that the representation of constant values is different from that of integers, character types can almost be treated as general integers.

String type

A string is a sequence of 0 or more characters enclosed between two double quotes. An empty string if there are no characters between the two double quotes. Here are some examples of strings:

"

"hello world!"

"hello java"

The Java language treats strings as an object of type String.

Boolean type

The boolean type has two constant values, true and false, which occupy one bit (not a byte) in memory, and you cannot replace true and false with 0 or non-0 integers, unlike the C language. The boolean type is used to judge logical conditions and is generally used for program flow control.

Public class TestCode03 {public static void main (String [] args) {/ / create a Boolean variable: boolean flag1 = true; System.out.println (flag1); boolean flag2 = false; System.out.println (flag2); boolean flag3 = 5 percent 9; System.out.println (flag3) Boolean flag4 = 5 low

When performing an operation:

Left = right: direct assignment

Left and right: direct automatic conversion

The above is about the content of this article on "how to define and use Java data types". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report