Java Language Fundamentals

Java variables -

type of data Defaults storage format data range
short 0 2 bytes -32,768 to 32,767
int 0 4 bytes -2,147,483,648 to 2,147,483,647
byte 0 1 byte -128 to 127
char null 2 bytes Unicode character range: '\u0000' (ie 0) to '\uffff' (ie 65,535)
long 0L or 0l 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036, 854,775,807
float 0.0F or 0.0f 4 bytes 32-bit IEEEE 754 single precision range
double 0.0 or 0.0D(d) 8 bytes 64-bit IEEE 754 double precision range
boolean false First place true(1) or false(0)

    In Java, variables need to be declared before they can be used.

Java Constants -

    Constants represent values ​​that cannot be changed while the program is running. In the Java coding standard, constant names must be capitalized.

The syntax of constants is as follows:

  • final data type constant name = value;

  • final data type constant name 1 = value 1, constant name 2 = value 2, ... constant name n = value n;

E.g:

final double PI = 3.14final char MALE=‘M’,FEMALE=‘F’;

 

Java annotations -

    There are generally three types of annotations in Java:

  • line comment //: comment only one line

  • Segment Comments /*...*/: Comment out several lines

  • Documentation Comments /**...*/: Comment several lines and write them into the javadoc documentation

 

Java automatic type conversion and coercion -

    In Java programs, different data types sometimes need to be converted to each other. Data type conversion is divided into automatic type conversion 自动类型转换and 强制类型转换:automatic type conversion. During the execution of the program, we do not need to make special declarations or operations. Variables are automatically converted into appropriate data types due to needs.

    Automatic type conversion needs to meet the following two conditions:

  • The target type is compatible with the original type

  • The number of bytes of the target type is greater than or equal to the number of bytes of the original type

Among the basic data types, the boolean type boolean occupies one byte. Due to its special meaning, the boolean type cannot be type-converted with other basic types (neither automatic type promotion nor forced type conversion), otherwise , will compile with an error.

 

Java bitwise operators - [a=60; b=13]

 

bitwise operators name describe Example
& bitwise AND The result is 1 if the corresponding bits are all 1, otherwise 0 (a & b), get 12, which is 0000 1100
bitwise OR If the corresponding bits are all 0, the result is 0, otherwise it is 1 (a|b) get 61, which is 0011 1101
^ Bitwise XOR If the corresponding bits have the same value, the result is 0, otherwise 1 (a^b) gets 49, which is 0011 0001
~ bitwise complement Flip each bit of the operand, i.e. 0 becomes 1 and 1 becomes 0 (~a) gets -61 which is 1100 0011
<< Shift left bitwise Shift the left operand to the left by the number of bits specified by the right operand a<<2 gets 240, which is 1111 0000
>> bitwise shift right Shift the left operand to the right by the number of bits specified by the right operand a>>2 gets 15 which is 1111
>>> Bitwise shift right with zero padding The value of the left operand is shifted to the right by the number of bits specified by the right operand, and the resulting vacancies are filled with zeros a>>>2 gets 15 which is 0000 1111


Java logical operators - [Boolean variable a is true, variable b is false]

Logical Operators name describe Types of Example
&& and The condition is true if and only if both operands are true Binary operator (a && b) is false
|| or Either of the two operands is true and the condition is true Binary operator (a||b) is true
No Used to invert the logical state of the operand. Logical NOT operator will get false if the condition is true unary operator (!a) is false
^ XOR 如果两个操作数逻辑相同,则结果为假,否则为真 双目运算符 (a ^ b)为真

 

Java引用——

    引用:Java 中一切都是对象,但操作的标识符实际是对象的一个引用。

        例:

String s;

    在这里,创建的是引用,不是对象。创建引用后,一种安全的做法是对其进行初始化。

String s = "abc";
String s =  new String("abc");

    通常采用new操作符生成一个新对象,然后将引用与该对象相关联。

    想要比较对象的内容是否相同时,Java 提供了一个特殊的方法equals(),它不适用于基本类型,基本类型使用==和!=进行比较。

 

Java条件运算符——

    条件运算符?:便是唯一的一个三目运算符了。

    语法形式:布尔表达式?表达式1 : 表达式2

    运算过程:如果布尔表达式的值为true ,则返回 表达式1 的值,否则返回 表达式2 的值。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127707&siteId=291194637