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

What are the five naming conventions and eight data types of Java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

This article mainly introduces Java's 5 naming conventions and 8 kinds of data types related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Java's 5 naming conventions and 8 data types what the article will gain, let's take a look.

I. Java naming convention

Any language has a set of naming conventions for the language itself, and java is no exception. Next, I'll enumerate java's naming conventions.

Here's the point. Are you ready?

1. Naming in the code cannot begin with an underscore or a dollar sign, nor can it end with an underscore or a dollar sign.

Counterexample: _ name/$Object/name_/name$/Object$

2. Naming in the code is strictly forbidden to use the mixture of Pinyin and English, let alone directly use Chinese. Explanation: correct English spelling and grammar can make it easy for readers to understand and avoid ambiguity. Note: even pure pinyin naming should be avoided. Positive example: international names such as alibaba / taobao / youku / hangzhou can be regarded as English.

3. The class name uses the UpperCamelCase style and must follow the hump form, except in the following cases: DO / BO / DTO / VO / AO

Positive example: MarcoPolo / UserDO / XmlService / TcpUdpDeal

Counterexample: marcoPolo / UserDo / XMLService / TCPUDPDeal

4. The method name, parameter name, member variable and local variable all use lowerCamelCase style and must follow the hump form.

Positive example: localValue / getHttpMessage () / inputUserId

5. constant names are all capitalized, and words are separated by underscores, so that the semantic expression is complete and clear, and there is no need to disrelish the name length.

Positive example: MAX_STOCK_COUNT

Counterexample: MAX_COUNT

II. Java data type

Data types in Java are divided into reference types and basic data types.

There are eight basic types. Today, we will introduce these eight basic data types:

Integer: byte,short,int,long

Floating point: float,double

Character type: char

Boolean type: boolean

Basic data type

Integer type:

(byte,short,int,long)

1. When in use, as long as it is within the range of values and conforms to the rules, everything else is fine.

What is worth thinking is that the rational use of data types can save memory space.

two。 The representation of numbers in different binary systems.

Base 10: 50, 100, 100, 10

Hexadecimal: 0x15, 0X15 is fine.

Character type (char):

Special meaning symbol table

Char occupies 2 bytes and 16 bits

Be careful when using it, when using single quotes

For example,'A 'represents a character A.

When using double quotation marks

For example, "A" represents a string with only one A

Boolean (boolean):

Boolean occupies 1 bit of memory, that is, 1 prime 8 characters.

Usually used for judgment.

The values are only 0 and 1 respectively.

It can also be expressed as false and true.

It should be noted that

False and null are different.

Floating point type:

(float,double)

1. Floating point is usually used to represent decimals.

Float can be accurate to 7 decimal places

Double can be accurate to 14 decimal places

two。 The constant indicating float is followed by "f".

For example: float axiom 11.2f

Otherwise, something will go wrong.

Note: in mathematics, there are countless floating-point numbers from 0 to 1, but the computer is discrete, so there are errors in representation.

The computer uses precision (a few digits after the decimal point to indicate correctly), and it is not appropriate to compare floating-point numbers with a precision of 0.1.

If an is 0.1, then even if there is an error of amur0.1, because both an and 0.1 are represented as a computer binary with errors.

1. Type conversion

1) automatic conversion: byte-- > short-- > int-- > long-- > float-- > double

Note: small can become big, big to small will lose precision

2) convert String to int or double

Int/Integer num = Integer.parseInt (String str)

Double/Double num = Double.parseDouble (String str)

2. How to convert integer int to string String

Int axi9

1) String s = String.valueOf (a)

This method is static and can be called directly through String. It can be said to be perfect, but it is not used to writing this way.

This implementation avoids the shortcomings and shortcomings of the first two. First, let's take a look at his internal implementation mechanism:

Public static String valueOf (Object obj) {

Return (obj==null)? "null": obj.toString ()

}

A null judgment is made internally, so a null pointer exception is not reported.

From the above source code, you can clearly see the reason why you don't have to worry about null value. However, this also gives us a hidden danger.

We should note that when object is null, the value of String.valueOf (object) is the string "null".

Instead of nullboxes! Keep in mind that you should pay attention to it during use.

2) String s = a.toString ()

In this usage, because there is already a public method .toString () in the java.lang.Object class

So you can call this method on any strictly defined java object. But you should pay attention when using it.

You must ensure that object is not a null value, otherwise a NullPointerException exception will be thrown. When using this method,

Usually derived classes override the toString () method in Object.

3. The precision problem will be lost when double type data is added and manipulated.

/ addition operation /

Public static double add (double M1, double m2) {

BigDecimal p1 = new BigDecimal (Double.toString (M1))

BigDecimal p2 = new BigDecimal (Double.toString (m2))

Return p1.add (p2). DoubleValue ()

}

/ subtraction operation /

Public static double sub (double M1, double m2) {

BigDecimal p1 = new BigDecimal (Double.toString (M1))

BigDecimal p2 = new BigDecimal (Double.toString (m2))

Return p1.subtract (p2). DoubleValue ()

}

/ multiplication /

Public static double mul (double M1, double m2) {

BigDecimal p1 = new BigDecimal (Double.toString (M1))

BigDecimal p2 = new BigDecimal (Double.toString (m2))

Return p1.multiply (p2). DoubleValue ()

}

4. the method of preserving the last two decimal places

Method 1:

DecimalFormat df = new DecimalFormat ("# 0.00")

Double D1 = 3.23456

Df.format (D1)

#. 0000 represents two decimal places #. 00 four decimal places and so on.

Method 2:

Double d = 3.1415926

String result = String.format ("% .2f", d)

% s string type

% c character type

B Boolean type

D integer type (decimal)

% x Integer Type (hexadecimal)

% o Integer type (octal)

% f floating point type

A hexadecimal floating point type

% e Index Type

% g Universal floating point types (shorter of f and e types)

% h hash code

%% Type

% n newline character

% tx date and time type (x represents a different date and time conversion character

% .2f%. Represents any number of digits before the decimal point

2 indicates that the result of the two-place decimal format is f for floating point.

This is the end of the article on "what are the 5 naming conventions and 8 data types of Java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the five naming conventions and eight data types of Java". If you want to learn more, you are welcome to 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

Network Security

Wechat

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

12
Report