The basic syntax of Java Java eight basic data types

Java basic syntax:

Identifier

Java identifier is the name of a method, variable, or other user-defined entries defined. Identifier may have one or more characters. In the Java language, constitute a rule identifier is as follows.

  • Identified by numbers (0 to 9) and letters (A ~ Z and a ~ z), the dollar sign ($), underscore (_) and all symbol combinations Unicode character set larger than symbol constituting 0xC0 no space between (the symbols ).
  • The first symbolic identifiers of letters, underscores, and dollar sign, can be followed by any letter, number, dollar sign or underscore.


Further, Java is case sensitive, so myvar and MyVar are two different identifiers.

Tip: When naming identifiers, remember not to start with a number, can not use any Java keywords as identifiers, and can not confer any standard identifier method name.

Identifier divided into two categories, namely user-defined keywords and identifiers.

  1. Keywords are identifiers have special meaning, such as true, false presentation logic is true.
  2. User-defined rule identifier is constituted by an identifier generated by the user, the non-reserved word identifier is an identifier such as abc.


Tip: Be sure to pay attention to when using the identifier, or using keywords, or use a custom non-keyword identifier. In addition, the identifier can contain keywords, but must be unique keywords.

For example, legal and illegal identifier.

  • Legal identifier: date, $ 2011, _date, D_ $ date and so on.
  • Invalid identifier: 123.com, 2com, for, if, fruit and so on.


Identifier is used to name the constants, variables, classes and objects and the like. Therefore, a good programming practice requires naming identifiers, you should give it a meaningful name or use.

Keyword

Keywords (or reserved words) are of special significance to the compiler fixed word, can not use for other purposes in the program. Keyword has a special meaning and purpose, and custom identifiers are different, can not be used as a general identifier. For example, in the " use Notepad to write a Java program to run " HelloJava.java an example of the class is a keyword, which is used to declare a class whose class name HelloJava. public is key, which is used to denote public class. Moreover, keywords are static and void, their use will be described in detail later in this tutorial sections.

Java keywords have special meaning to the Java compiler, which used to represent a data type, or shows the structure of the program and so on. Reserved words are reserved for the Java keywords, although they do not have as a keyword, but it is possible as a keyword in a future upgrade versions.

Java language currently defines 51 keywords that can not be used as variable names, class and method names to use. The following are classified for these keywords.

  1. Data type: boolean, int, long, short, byte, float, double, char, class, interface.
  2. 流程控制:if、else、do、while、for、switch、case、default、break、continue、return、try、catch、finally。
  3. 修饰符:public、protected、private、final、void、static、strict、abstract、transient、synchronized、volatile、native。
  4. 动作:package、import、throw、throws、extends、implements、this、supper、instanceof、new。
  5. Reserved words: true, false, null, goto, const.


Tip: Because Java is case sensitive, so the public is key, but Public is not a keyword. But for clarity and readability of the program, to try to avoid the use of other forms of keyword named.

character set

  Character Set (character set) is a collection of all the abstract characters supported by the system. Character (character) is a variety of words and symbols, including national characters, punctuation marks, graphic symbols, numbers, and so on.

Common coded character set are:

Unicode: also known as unified character set, which contains all been discovered and require the use of characters (such as Chinese, Japanese, English, German, etc.) almost in the world.
  1: ASCII: Early computer systems can handle only English, so the ASCII character set has become the default computer, includes all the characters needed for English.
  2: GB2312: Chinese characters, contains ASCII character set. ASCII denoted by a single byte, the remaining portion indicated by double-byte.
  3: GBK: GB2312 expansion, complete contains all the content of GB2312.
  4: GB18030: a superset of the GBK character set, often called the large Chinese character set, also known as CJK (Chinese, Japanese, Korea) character set, including Chinese, Japanese, Korean and language of all the characters.
Character code (character encoding), a character code conversion between the character set and the actual value is stored. Common coding methods are: UTF-8 (encoding the Unicode character set), UTF-16 (encoding the Unicode character set), UTF-32 (encoding the Unicode character set), ASCII (ASCII character set encoding )Wait.

Modify the character set used in a way to try:

   Decoding relatively simple and requires only: new String (decryData, "UTF-8"). // agreed to UTF-8 character set

byte [] getBytes (String charsetName)
using the specified character set coding this String is byte sequence, and storing the result into a new byte array.

 

Java's eight primitive data types

     About eight kinds of basic data types Java, its name, the number of bits, the default value, and the example in the range shown in the following table:

No.

type of data

Digit

Defaults

Ranges

for example

1 byte (position) 8 0  -2^7 - 2^7-1 byte b = 10;
2 short (short integer) 16 0 -2^15 - 2^15-1 short s = 10;
3 int (integer) 32 0 -2^31 - 2^31-1 int i = 10;
4 long (long) 64 0 -2^63 - 2^63-1 long l = 10l;
5 float (single-precision) 32 0.0 -2^31 - 2^31-1 float f = 10.0f;
6 double (double precision) 64 0.0 -2^63 - 2^63-1 double d = 10.0d;
7 char (character) 16 air 0 - 2^16-1 char c = 'c';
8 boolean (Boolean value) 8 false true、false boolean b = true;

 

      

 

 

 

 

 

 

 

      In order to verify the contents of the table, the idea was to run verification

 

package com.ce.test;

class Test {
    static byte b;  
    static short s;  
    static int i;  
    static long l;  
    static float f;  
    static double d;  
    static char c;  
    static boolean bo;   
  
    public static void main(String[] args) {  
        
      System.out.println("byte的大小:"+Byte.SIZE
              +";默认值:"+b
              +";数据范围:"+Byte.MIN_VALUE+" - "+Byte.MAX_VALUE, whichever); 
      
      System.out.println ( "short in size:" + Short.SIZE
               + "; Default:" + S
               + "; Data range:" Short.MIN_VALUE + + "-" + Short.MAX_VALUE);   
      
      System.out.println ( "int size:" + Integer.SIZE
               + "; default:" + I
               + "; data range:" of Integer.MIN_VALUE + + "-" + Integer.MAX_VALUE); 
      
      System.out.println ( "Long size:" + Long.SIZE
               + "; default:" + L
               + "; data range:" of Long.MIN_VALUE + + "-" + of Long.MAX_VALUE); 
      
      System.out.println ( "float size: "+ Float.SIZE
               +"; default: "+F
               + "; Data range:" Float.MIN_VALUE + + "-" + Float.MAX_VALUE); 
      
      System.out.println ( "double the size:" + Double.SIZE
               + "; Default:" + D
               + "; Data range: "Double.MIN_VALUE + +" - "+ Double.MAX_VALUE);      
      
      System.out.println ( " char size: "+ Character.SIZE
               +"; default: "+ C
               +"; data range: "+ Character + .MIN_VALUE "-" + Character.MAX_VALUE);   
      
      System.out.println ( "Boolean size:" + Byte.SIZE
               + "; default:" + BO
               + "; data range:" + Byte.MIN_VALUE+" - "+Byte.MAX_VALUE);     
  
    } 
}

 

 In the console output results are as follows:

 

byte size: 8; default: 0; Data range: -128--127 
size of short: 16; default: 0; Range: -32768--32767 
size of the int: 32; default: 0; Data range: -2147483648-- 2147483647 
size of long: 64; default: 0; data range: -9223372036854775808 - 9223372036854775807 
size of the float: 32; default: 0.0; data range: 1.4E-45 - 3.4028235E38 
size of double: 64; default value: 0.0; data range: 4.9E-324 - 1.7976931348623157E308 
size of the char: 16; default:; data range: - ??? 
Boolean size: 8; default: to false; data range: -128--127

  

 Why char output data range here is not 0--65535 it?

       The Java char type is represented by two bytes or 16 bits, since the number is unsigned, it is 2 to the power 16, it is the numerical ranges: 0 - 2 ^ 16-1;

Interview questions: 

  java char type can be used, given its characters do?

  The answer is: Yes, is a kind of Chinese characters in Unicode encoding, encoding is Unicode representation of char type set, it is possible

 

Operator:

Java can be divided by function operator: arithmetic operators, relational operators, logic operators, bitwise operators, assignment operators, and the conditional operator.

Reprinted with  https://baijiahao.baidu.com/s?id=1659125201403697638&wfr=spider&for=pc

Arithmetic operators

Ordinary arithmetic operator comprises a plus (+), subtract (-), multiply (*), divide (/), modulo (%), to complete the integer data and floating point arithmetic operations.

Relational Operators

Relational operator used to compare two values, including greater than (>), less than (<), greater than or equal to (> =), less than or equal to (<=), equality (==) and not equal (! =) Six kinds. Relational operators are binary operators, i.e. Each operator has two operands, the result of calculation is a logical value. Java allows "==" and "! =" Operator is used two kinds of data of any type. For example, the value may be determined whether the two numbers are equal, it may be determined whether the instance of the object is equal to or array. Analyzing the comparison example is when two objects are equal a reference address in memory.

Logical Operators

Including logic and logical operators (&&), logical OR (||) and logic NOT (!). The first two are binary operators, it is after a unary operator. Java logic or with logic and provide a "short circuit" function, that is, during operation, the operator first calculated value of the expression on the left side, if the value can be obtained using the entire expression is skipped Operator Right side calculating expressions, calculation operators or expression on the right, and to give the entire expression.

Bitwise Operators

Bit operators for operating bits of the binary, including bitwise (~), bitwise AND (&), bitwise OR (|), exclusive OR (^), right (>>), left ( <<) and unsigned right shift (>>>). Bitwise operators only for integer operate and character data.

Assignment Operators

Role assignment operator is the value of the constant, variable or expression is assigned to a certain variable.

Conditional operator

Conditional operator (:?) Is also referred to as "ternary operator" or "ternary operator."

Syntax: boolean expression? Expression 1: Expression 2.

Operation: If the Boolean expression is  true, the return  value of Expression 1, otherwise it returns  the value of expression 2.

Priorities of operators

When a priority order of the expression is evaluated, if the expression contains a variety of operators, operators will have the primary safety from high to low. Operator priorities are as follows:

 

 

Bit computing is a profound one operation, he is the root of all operations, source, his highest efficiency, all a good algorithm might use a clever bit operation, 

 

Guess you like

Origin www.cnblogs.com/naimao/p/12605849.html