Java Basic syntax

Source: Internet
Author: User
Tags binary to decimal ranges

1. Keywords

Keywords Overview: Words that are given a specific meaning by the Java language

Keywords feature: The letters that make up the keywords are all lowercase

Keyword considerations:

A) goto and const exist as reserved words and do not currently apply

b) High-level notepad such as notepad++, which has a special color tag for keywords, very intuitive


2. Identifiers

An overview of identifiers: A sequence of characters that are used to name classes, interfaces, methods, variables, etc.

Composition rules: English uppercase and lowercase letters, numeric characters, $ and _

Note: Cannot start with a number, cannot be a keyword in Java, is case-sensitive


3. Common Naming conventions

Common naming rules requirements: see Names and meanings

Package: is the folder, used to resolve the same name conflict problem, the package name must all lowercase.

Single-Stage: CN

Multilevel: Cn.vo

Class or interface:

One word: The first letter of a word must be capitalized, for example: Hello

Multiple words: The first letter of each word must be capitalized, for example: HelloWorld

method or Variable:

One word: The first letter of a word must be lowercase, for example: Main,age, etc.

Multiple words: Starting with the second word, capitalize the first letter of each word, for example: Showallnames (), and so on.

Constant:

One word: all uppercase, such as Pi.

Multiple words: Each letter is capitalized, separated by _, for example: Student_max_age, etc.


4. Notes

Comment Overview: The text used to interpret the description program.

Comment Classification format in Java:

Single-line Comment://, single-line comments can be nested.

Multiline Comments:/* * *, Multiline comments cannot be nested.

Document Comment:/** */

Precautions:

For single-line and multiline comments, the annotated text is not interpreted by the JVM for execution.

A note to a document is a Java-specific comment that can be parsed by the tool Javadoc provided by the JDK, generating a set of documentation for the program as a Web page file.


5. Comment function

A comment is a good programming habit that a programmer must have.

Explain the program to improve the program of reading stars.

can help us to get the wrong line.


6. Annotated version of HelloWorld

/** *   Requirements: I'm going to write a Java program that outputs the word "HelloWorld" to the console.  *   analysis:  *      1. To write a Java program, you must define a class.  *      2. The data can be output, indicating that our program can run independently, and the program to run independently, you must define the Main method.  *      3. outputting data to the console, you must use the output statement.  *   implementation: The  *      1.java language provides a keyword: class, which is used to define classes, followed by the class name. The format of the  *      2.main method is fixed: *           public static void main (String[] args) { * *           } *      3. The format of the output is fixed:  *           system.out.println ("HelloWorld"); *            "HelloWorld" This content can be changed.  *///This is my HelloWorld case public class helloworld{    /**     *  for the program to run independently, define the Main method      * main method is the entry of the program       *  is automatically called by the JVM      */    public  Static void main (String[] args) {        //in order to display the data in the console, We used the output statement         system.out.println ("HelloWorld");     }}


7. Constants

Constants Overview: Their values cannot be changed during program execution.

Constants in Java categories:

Literal constants

String constants
Enclosed in double quotes, for example: "Hello"
Integer constants
All integers, for example: 12
Decimal constants All decimals, for example: 3.14
Character constants Content enclosed in single quotes, e.g. ' A ', ' d '
Boolean Constants
More special, only true or false
Empty constants Null

Custom Constants


8. Binary

1. Java provides 4 representations of integer constants

Binary, octal, decimal, hex

2. Overview of the system:

Binary: is a carry system, is a method of rounding. For any kind of binary-X-binary, it means that the number operation at a certain position is always X-in. Binary is every two in one, eight into the system is every eight into a, decimal is every ten into a, Hex is 16 into one.

The origin of the binary, any data in the computer is in the form of binary. The early binary evolution of the electrical signal switch. An integer is also binary in memory, but using a large string of 0 or 1 values is cumbersome.

If we express the data using only the two states of the electrical signal switch, then the data that can be expressed is relatively small. And our common data: English letters, numbers, punctuation, which is a lot of. Two states are not enough, in order to be able to represent more data, the International Standards Organization stipulates that: with 8 such signals to represent a data, the unit of this data is called bytes. Later, we passed the numbers 1 and 0, respectively, to express the open and close. Data consisting of such 1 and 0 are binary data.

1byte=8bit,1k=1024byte,1m=1024k,1g=1024k.1t=1024g. However, the form of binary representation of data is a bit long, so we want to simplify it. So how to simplify?

The binary data, starting from the right, each three-bit group, the leftmost not enough three bits of the front complement 0. Then, the corresponding decimal values are calculated, and finally, each decimal data is combined together, is an octal data.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/87/4A/wKiom1fahMnRlWE5AAAMOX0035Y059.png "title=" Qq20160915192348.png "alt=" Wkiom1fahmnrlwe5aaamox0035y059.png "/>

This form of expression is not a simple sin, and we have a simpler one.

The binary data, starting from the right, each four-bit group, the left is not enough time, with 0. Then, respectively, the corresponding decimal value of the computer, and finally, the data of each decimal combination, is a hexadecimal.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/87/47/wKioL1fah7ejuqS0AAAMvZU6Ndc889.png "title=" QQ20160915192918-copy. png "alt=" Wkiol1fah7ejuqs0aaamvzu6ndc889.png "/>

Rule: The larger the binary, the shorter the representation.

3. Different binary data composition

Binary: consists of 0 and 1, starting with 0b.

Octal: Consists of 0,1,2,3,4,5,6,7, starting with 0.

Decimal: Made up of 0,1,2,3,4,5,6,7,8,9,0. Integers are decimal by default.

Hexadecimal: Represented by 0,1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f (a,b,c,d,e,f case). Start with 0x.

/** * Different binary data performance: * Binary: Consists of 0, 1. Start with an OB. * Octal: Made up of 0,1,2,3,4,5,6,7. Start with 0. * Decimal: Made up of 0,1,2,3,4,5,6,7,8,9,0. The default integer is decimal. * Hex: by 0,1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f (uppercase and lowercase). Start with 0x. */public class Radix {public static void main (string[] args) {System.out.println (100);//Decimal SYSTEM.OUT.P Rintln (0b100);//binary System.out.println (0100);//octal System.out.println (0x100);//Hex}}100464256

The conversion of the other binary to decimal.

Decimal
Decimal
12345 = 10000 +2000+300+40+ 5

=1*10 (4) +2*10 (3) +3*10 (2) +4*10 (1) +5*10 (0)

=12345


Coefficient: The data value on each one is itself a factor

Cardinality: X-binary cardinality is X

Right: We numbered the data on each digit, from the right, and from 0 onwards. This number is the weight of the data on that bit.

Formula: The coefficient on each of the * cardinality * weighted power is added.

Binary

Decimal
100=
1*2 (2) = 4
Octal

Decimal
100= 1*8 (2) =
64
Hexadecimal
Decimal
100=
1*16 (2) = 256

Decimal conversion Other Binary

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/87/47/wKioL1faluLg2-VRAACIBazUa88067.png "title=" Qq20160915204104.png "alt=" Wkiol1falulg2-vraacibazua88067.png "/>


9. Fast conversion of decimal and binary binary

8421 yards: 8421 yards is the Chinese mainland, 8421 yards is the most commonly used in BCD code. It means that each bits data corresponds to a fixed value, just add the corresponding data value, you can get the binary corresponding decimal value.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/87/4A/wKiom1fanAaRpATeAAAflFZuw6k036.png "title=" Qq20160915210301.png "alt=" Wkiom1fanaarpateaaaflfzuw6k036.png "/>

Binary conversion to decimal: 1010100=64+16+4=84

Decimal conversion to binary: 100=1100100


10. Symbolic Data representation

Within the computer, there are 3 notation numbers: The original code, the inverse code and the complement, all the data is performed in the complement of the operation.

The original code: is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" means the parent, the remaining bits represent the size of the numeric value.

Anti-code: The inverse code of positive number is the same as its original code, negative number of the inverse code is to the original code bitwise negation, except the sign bit.

Complement: The complement of a positive number is the same as its original code, minus the complement is in its inverse code of the bottom plus 1.

Example: +7 and-7 are represented by the original code, the inverse code, and the complement respectively.

The first binary that gets 7 is: 111

Original code:

Positive number of the original code highest bit is 0, negative numbers of the original code of the highest bit is 1, the other is the value of the bit.


Sign bit
Value bit
+7
0
000,0111
-7
1
000,0111

Anti-code:

The inverse code of positive numbers is the same as the original code, the inverse code of negative numbers and the original code is the symbol bit unchanged, the value bit is opposite, that is 0 becomes 1, and 1 becomes 0.


Sign bit
Value bit
+7
0
000,0111
-7
1
111,1000

Complement:

The complement of positive numbers is the same as the original code, and the complement of negative numbers is added 1 on the basis of the inverse code.


Sign bit
Value bit
+7
0
000,0111
-7
1
111,1001


11. Variables

Variable Overview: In the course of a program's execution, the amount of its value can change within a range. Can be understood as the unknown in mathematics.

The constituent rules of a variable:

1. It must be qualified.

How to qualify? With the data type.

2. When we are in the operation, it is impossible to take this space to operate, when we operate the value of the space, we give the space (memory area) a name, that is, the variable name.

3. Even if there is a data type and variable name, but if there is no value, then this space is a garbage space, meaningless.

The format of the definition variable is: Data type variable name = initialization value;

12. Data type

The Java language is a strongly typed language that defines specific data types for each data and allocates different sizes of memory space in memory.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/87/47/wKioL1faqYCTQ_UUAAAntsTHjG4822.png "title=" Qq20160915220020.png "alt=" Wkiol1faqyctq_uuaaantsthjg4822.png "/>


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/87/4A/wKiom1farljynUlgAAKXAFHUKEk300.png "title=" Qq20160915222107.png "alt=" Wkiom1farljynulgaakxafhukek300.png "/>

Each integer type of Java has a fixed number of table ranges and field lengths that are not affected by the specific operating system to ensure the portability of the AVA program.

The integer constants of the Java language default to int, declaring that a long constant can be added ' l ' or ' l ', such as: int i = 100;//correct, long = 200l;//must be plus l or it will go wrong

Similar to integer types, Java floating-point types are fixed by the number of table ranges and field lengths, and are not affected by the platform.

There are two types of expressions for Java floating-point type constants:

Decimal number form, such as: 3.14

The form of scientific notation, such as: 3.14e2.3

The default for Java floating-point constants is double, and if you want to declare a constant to be a float type, you need to add F or F after the value, such as double d = 12345.6;//correct float f = 12.3f;//must add f or else an error will occur

The char type data is used to denote a "character" in the usual sense. Character constants are single characters enclosed in single quotes, for example: char c1 = ' a ';

Java is Unicode-encoded, with two bytes per character, so it can be represented as a hexadecimal encoded form.

The Boolean type is suitable for logical operations and is generally used for program flow control. Boolean data only allows values of true or false, and it is not possible to replace true and false with integers of 0 or not 0, which differs greatly from the C language.

Public class datatype {   public static void main (String[]  args) {       /*        *  Define the format of the variable:        *    data type    variable name  =  Initialize the value of;         */       byte b  = 10;       system.out.println (b);        short s = 100;       system.out.println (s);        int  i = 1000;        system.out.println (i);       long j =  1000000000000000l;       system.out.println (j);    }} 1010010001000000000000000











This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1852991

Java Basic syntax

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.