Java grammar Basics

Source: Internet
Author: User
Tags decimal to binary
Is Java a compiled or interpreted language? Compiled language: the source code of the advanced language is compiled into a binary machine code that can be executed by the hardware of the platform by the compiler for a specific platform (operating system. Compile the program once and package it into executable programs that the platform can recognize. Interpreted language: Use a dedicated interpreter to interpret the source program line by line as the machine code of a specific platform and execute it immediately. Each execution must be explained. To sum up, Java is a hybrid language. Because the Java program must be compiled first and then explained in two steps. 1. the Java compiler compiles the source program into a class bytecode file unrelated to the platform. These classes are not oriented to any specific platform but only to JVM. 2. The JVM will explain and execute the class file. JVM: Java virtual machine. Is an abstract computer. Like a computer, it has an Instruction Set and uses different storage areas. It executes commands and manages data, memory, and registers. Garbage collection in Java is handled by JVM in another thread, and programmers do not need to participate.

A Java source file can contain multiple classes, but a maximum of one public class is allowed;

If a Java source file contains a public class, the file name of the source file must be the same as the class name of the public class.

 

// Definition class public class demo {// main function, program entry public static void main (string [] ARGs) {// execution statement }}

 

Main function:

Public: this function has the largest public access permission. Static: this function is loaded as the class is loaded. With the extinction of classes. Void: This function does not return a specific value. Main: Not a keyword, but can be recognized by JVM. (String [] ARGs): A function parameter. The parameter type is an array, and the elements in the array are strings. ARGs is a form parameter.

 

 

Note:

Single-line and multi-line annotations are used to mark some special texts in the program, which are not involved in compilation and running.

Single line comment: // comment content

Multi-line comment:/* Comment content */

Document injection is a special release in Java. You can use the tool (javadoc.exe) in JDK to parse and generate a help document.

Document comment:/** comment content */

 

Constant:

A value that cannot be changed.

Classification: Integer constants, decimal constants, Boolean constants, character constants, string constants, and null constants. A more special final modified variable is also called a constant.

 

Variable:

A storage area in the memory.

Format:

Variable type variable name = initialization value;

Int x = 3;

X is called a variable. The value of X can be changed to store constants of the same type and can be reused.

You must declare the variable before using it.

The range is.

 

Hexadecimal:

Features: X-in-one.

Java value representation:

1. Binary: Any data stored in a computer is stored in binary format.

2. octal sequence: 0-7. Each octal sequence starts with 0.

3. Decimal: 0-9, which is frequently used in daily life.

4. hexadecimal: 0-15, each hex-1, the program starts with 0x.

 

Hexadecimal conversion

A. Decimal to binary, octal, and hexadecimal

Division is used to obtain the remainder. Divide the number to be converted by the hexadecimal number. Remember the remainder, and then divide it by the hexadecimal number. Remember the remainder until this number is equal to 0, reversing all the remainder is the corresponding binary representation.

B. Convert binary, octal, and hexadecimal to decimal

Multiplication: Number of the numbers to be converted. The number starts from the low position and starts from 0. multiply the number on each digit by the number to the power of the base number, finally, all the obtained results are added in decimal format.

C. Binary and octal Conversion

Each bits in octal correspond to three digits in binary.

A. Binary and hexadecimal conversion

Each digit in hexadecimal format corresponds to four digits in binary format.

 

Negative expression:

See: http://blog.csdn.net/wyl530274554/article/details/7910642

 

/* Note: The hexadecimal conversion implemented by Java:

Decimal: integer. parseint (string num, int redix); (return INT)

Decimal to binary, octal, and hexadecimal: integer. tobinarystring (int I), integer. tooctalstring (int I), integer. tohexstring (int I ). (Return string)

*/

 

 

Identifier:

Symbol used to name variables, classes, and methods in a program. Rule: 1. It can contain letters, numbers, underscores (_), and dollar signs ($). It cannot start with a number. 2. The identifier cannot be a keyword, a special direct volume, or reserved word. 3. the Java language is case sensitive.

Keywords:

All keywords in Java are lowercase letters. Java has 48 keywords and three special values: True, false, and two reserved words Goto and const in nulljava.

Escape characters:

Use \ to change the meaning of subsequent characters or symbols.

\ N line feed, \ r press enter, \ B Return, \ t tab.

In Windows, the newline is represented by \ r \ n; in UNIX, The newline is represented by \ n.

 

Path separator:

Cross-platform: file. Separator

Use: \ or/in Windows/

Other platforms :/

 

Data Type classification:

Java is a strongly typed language (each variable and expression has a type determined during compilation)

All variables in Java must be declared before use.

Basic data type conversion:

1. automatic conversion: (byte, short, char) --> int --> long --> float --> double --> string.

2. Forced conversion: Strong conversion of the basic type to enhanced conversion character; strong conversion of strings to other basic types, the operation is completed using the basic type of packaging class, such as integer. parseint (string Str ).

OPERATOR:

1. Arithmetic Operator 2, logical operator 3, three object Operator 4, assignment operator 5, comparison operator 6, bitwise Operator

 

Bitwise OPERATOR:

Left shift: <one digit is equivalent to the power of the number of digits that multiply by 2, for example, 3 <2 = 3 * = 12;

Right Shift: ①> one digit is equivalent to the power of the number of digits to be moved divided by 2, and the maximum bit is used to fill the space. ② >>> Unsigned right shift, that is, 0 is used to fill the space.

 

Exclusive or: 0 for the same value and 1 for different values.

If a number is different or the same number is two times, the result is still the original value, for example, 7 ^ 4 ^ 4 = 7.

// No third-party variables are required. Two numbers are exchanged, with an exclusive or. Int M = 3; int n = 8; system. out. println ("m =" + M + ", n =" + n); // M = 3, n = 8 m = m ^ N; n = m ^ N; // equivalent to (m ^ N) ^ n = mm = m ^ N; // equivalent to m ^ (M ^ n) = nsystem. out. println ("m =" + M + ", n =" + n); // M = 8, n = 3

Convert the natural number 0 to 0.

Use 1 ^ (X)

Bitwise AND :&

Int num: num & 15

Int num: num & 255

 

The three-object operator can replace the simple if... else statement.

 

Process control:

1. Ordered Structure: execute one by one

2. Branch Structure: If and switch

3. Loop Structure: For, while, do... while

Control Loop Structure:

1. Break end Loop

2. Continue ends the loop and continues the next iteration.

3. Return end method.

Print the multiplication table on the console:

Public class multiplicationtable {public static void main (string [] ARGs) {for (INT I = 1; I <= 9; I ++) {// control the number of rows for (Int J = 1; j <= I; j ++) {// control the output system of each element. out. print (J + "*" + I + "=" + I * j + "\ t"); // output each row} system. out. println (); print a line and wrap it .}}}

Java labels:

Outer:

It works only before loop statements.

Use Case: Break outer (exclusive loop); can also be used for continue statements.

 

Function:

It is called a method in Java.

Is a piece of program code that defines specific functions in the class.

Format:

Modifier return value type method name (parameter list ){

Execute the statement;

Return Statement;

}

Features:

1. define functions to encapsulate code

2. Improved code reusability

3. The function is executed only when it is called.

4. If no specific return value exists, the return value type can be expressed by void, and return can be omitted in the last row.

 

How to define a function?

1. Specify the result type (that is, the return value type)

2. Determine whether unknown data is involved (that is, the parameter list)

 

Function overloading:

In a class, more than one function with the same name is allowed, as long as the content of their parameter list is different. (Parameter classes, parameter types, and Parameter order)

Array type:

Array is a type;

The array stores the same type of data. You can use the badge to operate the elements in the array.

After the array is initialized, the length is unchangeable.

Array Common exceptions: Both arrayindexoutofboundsexception and nullpointerexception are subclasses of runtimeexception.

 

Format: 1. Int [] I = new int [3]; element type [] array name = new element type [array length]

2. Int [] arr = new int [] {1, 2, 3, 4}; element type [] array name = new element type [] {element, element ,...}

3. Int [] arr = {1, 2, 3, 4}; element type [] array name = {element, element ,...}

 

How to print data in an array:

/*** Array printing methods. * **/Import Java. util. arrays; public class demo {public static void main (string [] ARGs) {int [] arr = {,}; arrays. sort (ARR); // sort // Method 1: Use the arrays tool class system. out. println ("arrays tool:"); system. out. print (arrays. tostring (ARR); // Method 2: For Loop traversal array system. out. println ("\ n \ Nfor loop:"); For (INT I = 0; I <arr. length; I ++) {system. out. print (ARR [I]);} // method 3: Enhance the for loop traversal array (foreach) system. out. println ("\ n enhanced for loop:"); For (int I: ARR) {system. out. print (I);} // Method 4: The WHILE LOOP traverses the array system. out. println ("\ n \ nwhile loop:"); int x = 0; while (x <arr. length) {system. out. print (ARR [x]); X ++;} // Method 5: Do... while loop traversal array system. out. println ("\ n \ Ndo... while loop: "); int n = 0; do {system. out. print (ARR [N]); N ++;} while (n <arr. length );}}

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.