Java base data type/basic operator

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

Overview
The Java language has been 22 years since the launch of Sun in 1995 and is now one of the most used Languages. Java is also a relatively easy to get started language, my first contact with the language is Java. Java Development. The core of Java is object-oriented programming, where polymorphism, inheritance, encapsulation to embody the concept of Java object-oriented programming, as well as the Java collection, flow, reflection and so On. Today I will start with the most basic data types and basic operators: although the bottom of Java is implemented by c, but Java and C still have a lot of different can also call each other is jni, we will talk about Later.

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

The basic data types are divided into four classes and eight types:

Integer type: byte,short, int, Long
Float Type: float, Double
Character Type: Char
Boolean Type: Boolean

Here's A detailed look at each type of area Edge.
integer type: contains all Integers.
byte:1 byte value range is -128~127 (0 is also one of positive numbers)
short:2 bytes -2^15~2^15-1
int:4 byte -2^31~2^31-1 (common)
Long:8 bytes -2^63~2^63-1
float type: contains all decimals.
float:4 byte -3.403e38~3.403e38 (common)
Double:8 bytes -1.798e308~1.798e308
character Type:
Char 4 byte 0~65535
Boolean type:
the Boolean 1 byte theoretically accounts for one-eighth bytes, because a switch can be determined to be true and false, but the Boolean type in Java does not explicitly specify his large

typically in code we assign values to variables and constants to use, where the Java variables are described in three questions.
A: What is a variable
The amount of the value that can change within a range during the execution of a program
B: The definition format of the variable
data type variable name = variable value;
C: Why to define a variable
used to keep constant of the same type, and can be reused
the concept of constants is presented in the C question, so what is a constant, which is defined in Java as the amount of the value that cannot be changed after the first assignment, which we call a Constant.
the definition Format is: public static final data type constant name = constant value;
note:
1. The variable/constant name must be a valid identifier;
2. variable/constant name cannot use keywords in java;
3. Variable/constant name cannot be duplicated;
4. A meaningful word should be selected as the Variable/constant name;

Conversion of data types: conversion of data types into implicit conversions and casts
implicit conversion: The default translation rules in java, the data type with a small value range and a large range of data types to operate, the small data type is first promoted to large, and then the Operation.

cast: When a data type with a large number of bytes is assigned to a data type that has fewer types, because of the loss of precision, Java prompts the writer to make a forced type Conversion. the conversion format is:
B = (byte) (a + b);
Note that if you exceed the value range of the assigned data type, The result will be different from the result you expect, and the bytes that are out of range will be Lost.

byte B1 = 3;
byte B2 = 4;
byte B3 = b1 + b2;
* B1 and B2 are two variables, the values stored in the variables are variable, so the JVM is unable to determine the specific values in the program run
* Variables of type Byte are automatically promoted to int type when the operation is performed
This involves binary operations. I will write a separate article to explain;

Reference Data types: class (classes), interface (interface), array (array);
I'm not going to be here, but i'll be in the BACK. the reference data type is explained in detail and is said in Object-oriented.

Basic Operators:
The operators in Java are divided into: arithmetic operators, assignment operators, comparison (relational) operators, logical operators, bitwise operators, and ternary operators:

arithmetic operators: + (plus),-(minus), * (multiply),/(except),% (remainder), + + (self-increment),--(self-decrement)
where "+", "-" can also be used as the sign of the data, the rest of the subtraction we should all often use, take-up and self-reduction estimates used relatively little.
%: the literal meaning of the remainder, such as 7%4 is 3,8%4 is 0
+ +: Self-added. +1 of the original data, but it is important to note that the seat of the + + is after the data is represented after the completion of the operation of a self-increment, if it is in front of the data, will be in the self-increment to participate in the operation;
--: Self-reduction. 1 of the original data is the same as the Self-increment.

example:
int x, y, z;
x = 2;
y = x + +;
z = ++x;
System.out.println ("y =" + y + ", z =" + z ");
output result: y = 2, z = 4

Assignment operator: =,+=,-=,*=,/=,%=
=: instead of assigning the left value to the right, as in math, assign the value on the right to the left.
+ =: The data on the left is added to the data on the right and then assigned to the left data;
- =: subtracts the left data from the data on the right and assigns it to the left data;
*=: The data on the left is multiplied with the data on the right and then assigned to the left data;
/=: divides the left data with the data on the right and assigns it to the left data;
%=: the left side of the data and the data on the right to the remainder of the assignment to the left side of the data;

comparison (relational) operator: >,<,==,>=,<=,!=
comparison of the left is greater than the right;
<: Compare to the left is less than the right;
= =: Compare whether the left and right are equal;
>=: Compare to the left is greater than or equal to the right;
<=: Compare to the left is less than equal to the right;
! =: Compare left not equal to right;
note:
In addition to the "= =" and "! =" can compare all the basic data types and reference data types, the rest can only compare integer, floating point type, character type;
comparison operator return value is both Boolean type

logical operator:& (with), &&,| (or), | |,! (non), ^ (xor)
&: False if False.
|: True if True.
^: the same is false, and the difference is True.
!: not false is true, false if not True.
Features: An even number does not change itself
note:
The logical operator operation data must be of type Boolean.

bitwise operators: &,|,^,~,>>,>>>,<<
&: There are 0 0
|: There are 1 1
^: same 0, different 1
~: Bitwise Reverse
<<: left highest bit discarded, right 0
>>: The right shift is 0, the left side is 0, the maximum is 1, the left side is 1
>>>: Unsigned Right shift whether the highest bit is 0 or 1, the left side is 0
note:
bitwise operators operate on the underlying binary so it is also the most efficient way to do this, and I will write an article alone to narrate the use and scenario of binary and bitwise Operators.

ternary operator: (relational expression)? Expression 1: expression 2;
If the relationship expression is true, the expression 1 is returned, instead false returns the expression 2;
note:
simple logic judgments using ternary operators more concise, more complex logic is recommended to use the If...else statement substitution, (a Simple description of the two sides, the ternary operator has been the operator of the assignment action, that is, there are operations, and if the statement is not the result of the operation, only judge.)

about the precedence of operators:
Order of Operations: increment decrement operations > Arithmetic operations > Comparison operations > Logical Operations > Assignment Operations
Order of specific symbol operations:
1 () Brackets
2 +,-plus Sign
3 + +,--,! Unary Operations
4 *,/,% Multiplication
5 +,-plus minus
6 >>,>>>,<< bit arithmetic
7 <,>,<=,>= Comparison size
8 ==,!= comparison is equal
9 & Bits and Operations
10 ^ Bitwise XOR OR Operation
11 | Bit or Operation
&& Logic and Operations
13 | | Logical OR Operation
14?: Ternary Operation
15 = Assignment Operation

At last
The first time to write a blog, the original has always been recorded electronic notes, so do not know whether to write a comprehensive rigorous, to have the wrong or not thoughtful place to welcome correct.
Ps: Although I have done no one will look at the preparation, after all, is the most basic thing. but I will slowly put my notes a little bit of changes are sent Out.

Java base data type/basic operator

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.