Java basic 02 data types, operators, branching structures

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators true true

Content:
(1) Data type
(2) operator
(3) Branching structure

1. Data type

  Basic data types in the Java language: Byte, short, int, long, float, double, Boolean, Char.

1.1 Boolean type

  The types of false and true information described in the Java language are: Boolean.

There are only two values for this type of variable: true (True), False (false).

The size of the type in memory space is not clearly defined and is generally considered to be 1 bytes.

1.2 Character types

  The data types used in the Java language to describe a single character are: char such as: ' A ', ' B '.

Where the char type occupies 2 bytes in memory space and has no symbols, so the range is 65535.

This type is rarely used in future development, and uses more strings consisting of multiple characters, described in the Java language using the sting type.

A pattern such as character ' a ' cannot be stored on a computer because the bottom of the computer can only recognize binary sequences of 0 and 1,

If you need to store ' a ', you need to assign a number to the pattern, record the number to the computer, and this number is called ASCII.

The commonly used ASCII are:

' A '-97 ' 0 '-48 spaces-32 line break-10

Escape characters:

\ "represents" \ ' represents ' \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ = Tab

1.3 Conversions between basic data types

  There are two ways to convert between basic data types in the Java language:

Automatic type conversions: conversions from small types to large types

Coercion type conversions: conversions from large types to small types

Syntax format: target type variable name = (target type) source type variable name (not recommended for forced type conversion, possible loss)

2. Operators
2.1 Arithmetic Operators

  + Represents the addition operator-represents the subtraction operator * for the multiplication operator

  /represents the division operator% for modulo/fetch operator

Attention:

(1) The result of dividing two integers in the Java language takes only the integer part, discarding the fractional part.

(2) If you want the calculation result to retain a fractional part, you need to do the following:

(a) to cast one of the operands to a double type.

(b) Multiply one of the operands by 1.0 to the double type and then the operation. (This method is recommended)

(3) In Java 0 cannot do divisor, but 0.0 can do divisor, the result is infinite.

(4) + can be used as the arithmetic operator, or as a string connector, the specific way to distinguish between the following:

As long as one of the operands in the + side is a string type, it is treated as a string connector.

2.2 Relationship/comparison operators

  > indicates whether greater than operator > = indicates whether greater than equals operator < Indicates whether the operator is less than

<= Indicates whether the less than equals operator = = Indicates whether the equals operator = = Indicates whether the operator is not equal

The result of the expression of all relational operators as the final operation must be a Boolean type.


2.3 Self increment operator

  + Represents the addition operator + + to indicate that the increment operator allows the value of the variable itself to be increased by 1, overriding the original value.

-Represents the subtraction operator--Indicates that the decrement operator allows the value of the variable itself to be reduced by 1, overriding the original value.

Attention:

(a) for a single variable, the result of + + placed before or after the variable name is the effect of making the variable itself +1.

(b) For an expression, ++a indicates that the value of a itself is +1 before being the result of an expression.

a++ means let a as the result of the expression, and then let ah own value +1.

-- a means that the value of a itself is 1 and then the result of the expression.

A--means let a be the result of the expression first, and then let the value of its own-1.

2.4 Logical operators

&& represents the logical AND operator, which is equal to and true for true, False for false.

|| Represents a logical OR operator, equivalent to or, a true true, false as false.

! Represents a logical non-operator, which is equivalent to reverse, true false, False true.

The difference between 2.4.1 & and &&

  There are two ways to use the & operator: (1) bitwise and; (2) Logic and.

The && operator is short-circuiting and arithmetic. The difference between the logic and the short-circuit is very large, although both require that the Boolean value on both sides of the operator is true the value of the entire expression is true.

&& is called a short-circuit operation because if the value of the expression on the left side of && is false, the expression on the right will be shorted out directly and will not be evaluated.

Most of the time we may need to use && instead of &, for example, to verify that the user's name is not NULL and is not an empty string when validating the login, it should be written as username! = null &&!username.equals (""), The order of the two cannot be exchanged,

You cannot use the & operator, because if the first condition is not true, the equals of the string cannot be compared, otherwise a nullpointerexception exception will be generated.

Note: Logical OR operator (|) and short-circuit or operator (| | The difference is also true.

Short Circuit Characteristics:

In the case of logic and, if the result of the first operand is false, the result of the entire expression must be false, and the second operand's judgment is skipped.

For logic or, if the result of the first operand is true, the result of the entire expression must be true, and the second operand's judgment is skipped.


2.5 Three mesh/conditional operator

  Conditional expression? Expression 1: Expression 2

(Determine if the conditional expression is true, if the execution expression 1 is executed, otherwise the expression 2)

Java basic 02 data types, operators, branching structures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.