Java from entry to the master Chapter 3 Java language foundation

 

table of Contents

Basic data types

Variables and Constants

Operators

Type Conversion

Code Specification

Identifier naming convention


Basic data types

A total of 8 Java data types, integer (byte, Short, int , Long), float (float, Double ), string, Boolean.

  • Hex: Decimal can not start with 0, octal begin with 0, hexadecimal with 0x or 0X beginning.
  • Integer: l L or to add precision to prevent deletion (overflow) at the rear when using long. Java default integer type int .

  • Float: To add F or f or an error occurs later when using the float. Java default decimal type double .

To avoid 4.35 * 100 = 4.34999999 problem, use Math.round () is rounded.

To avoid unequal 2.0-1.9 == 0.1, using the Math.abs (), if the absolute difference between the two numbers is less than 1e-6, 0 is determined, i.e., considered equal .

  • Character types: Java use unicode encoding , character encoding from 0x0000-0xffff. Two bytes.
    • char type
    • Escape

  • Boolean: true, false

Variables and Constants

  • Identifier: letters, underscores, dollar symbols , numbers, and the first character can not be a number.
  • Keywords:

  • variable:
    • It must be a valid identifier; you can not use the keyword; do not repeat; select meaningful variable names.
    • Local variables static variables shield
  • Constant: final variable name Data type [= value].
    • final variables belonging to member variables (class member variables) must be assigned when defining time.
    • Can define the time constant in the method belongs, after the assignment.
  • Variable range:
    • Member variables into static variables (plus static int a; across class, use the class variable name reference.) And instance variables (general int a).
    • Local variables (variables defined method) shield member variable (class variables defined)

Operators

  • Assignment operator: =
  • Arithmetic operators:

  • Increment decrement operators: ++ a, a ++, - a, a--.
    • To change the value or the value of the difference between first use.
    • Unary operator, can not be placed outside the parentheses.
  • Comparison operators:
    • Can not be used in tandem

  • Logical Operators:
    • &&, ||: boolean operand must be the type of short-circuit
    • Bitwise logical operations may be performed: &, |, ^

  • Bitwise Operators:
    • Bitwise: & bitwise and, bitwise or |, ~ bitwise, bitwise exclusive OR ^;
    • The shift operators: left <<, >> right, unsigned shift right >>>
    • Tip: Right shift is divided by the equivalent of 2, left shift is equivalent to dividing by two.
  • Ternary operator: the conditional expression? Value 1: Value 2; conditional expression is true the entire expression evaluates to 1 if the value of 2.
    • There is a return value, must be used.
  • Operator Precedence

Type Conversion

  • Implicit type conversions: byte <short <int <long <float <double. char type can also be converted to an int and more advanced types. Converted to a byte, short possible overflow, because only one byte byte, short two bytes, but more than 32,767 the number will be negative, char is unicode encoded as two bytes.
  • Explicit type conversion:
    • In addition boolean, conversion between other basic types in a method to achieve explicit type conversions. int a = (int) 1.2;
    • Assign a value to byte, short, int, long time variables, variables can not exceed the range, otherwise it will error, you must use a cast.
    • Mandatory conversion will result in inaccurate data, the decimal point is lost, overflow.

Code Specification

  • Code comments: // single-line comments, multiline comments /*...*/, document annotation /**...*/
  • Note: Multi-line comments can not be nested multiline comments

Identifier naming convention

  • Class Name: usually nouns, capitalized the first letter of all words
  • Method name: commonly used verbs, the first word lowercase, uppercase first letter of subsequent words
  • Variable: the first word lowercase, uppercase first letter of subsequent words
  • Constant: in all uppercase letters

 

 

 

 

 

 

Published 46 original articles · won praise 0 · Views 1036

Guess you like

Origin blog.csdn.net/weixin_37680513/article/details/103283658