Java Fundamentals 1

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators logical operators java reference

I. Features of the Java programming language (a purely object-oriented language):
1, Object-oriented
A, Package
B, inheritance
C, polymorphic
2, security
3, cross-platform

Two. See below a simple Java program that will print the string Hello world!

 Public class Texthelloworld {             publicstaticvoid  main (string[] args) {                    System.out.println ("Hello world!") ) ;           } }

Places to be aware of:
The above code Texthelloworld is the class name, the class name to be consistent with the file name (exactly the same), that is, the above Java code file name is Texthelloworld.java

Three. Basic syntax

The following points should be noted when writing Java programs:

1. Case Sensitive : Java is case-sensitive, which means that the identifier Hello is different from hello.

2. Class name : For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Texthelloworld.

3. Method Name : All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.

4. Source file name : The source file name must be the same as the class name. When saving the file, you should use the class name to save the filename (remember that Java is case-sensitive), and the suffix of the file name is. java. (a compilation error is caused if the file name and the class name are not the same).

5. Main method Entry : All Java programs are executed by the public static void main (String []args) method.

Four. Java identifiers (called identifiers, wherever you need to name them)

For the Java identifiers, there are a few things to note:

1. All identifiers should start with a letter (A-Z or a-Z), a dollar symbol ($), or an underscore (_) and cannot start with a number

2. The first character can be a letter (A-Z or a-Z), a dollar symbol ($), an underscore (_), or any character combination of a number

3. Keywords cannot be used as identifiers

4. Identifiers are case-sensitive

5. Examples of legal identifiers: Age, $salary, _value, __1_value

6. Examples of illegal identifiers: 123abc,-salary

7. Code specification: Hump naming method String EmpName; getElementById

Five. Java basic data type: cannot =null;

Four types of eight types:
1, Integer type
         byte 2 of 8 square (value range--<-128~127>)--2 bytes
          Short 2 of 16 square--4 bytes
         int 2 of 32 square--6 bytes
         Long 2 of 64 square--8 bytes
2, floating-point type
         float 7 bits after decimal--4 bytes
         Double 11 bits after decimal point--8 bytes
3, type bool
         Boolean (Only two values, true, False)--1 bytes
4, Character type
         Char--2 bytes

char a = ' Q '; Note: In Java, characters are enclosed in single quotation marks (' ')
String
        String
String s = "Hanqi"; Note: In Java, strings are enclosed in double quotation marks ("")

Six. Java reference type (object):--can be =null;
All the classes
All the interfaces
All the arrays

Seven. Type conversion:
Double float long int char short byte
When a char short byte is evaluated, the value defaults to int
Implicit conversion (low-to-high), explicit conversion (high-low)

Eight. java keyword

The following is a list of Java reserved words. These reserved words cannot be used for constants, variables, and names of any identifiers.

Nine. Operators:

1. Arithmetic operator: +-*/% + +--
2. Relational operators:> < >= <= = = =!
3. Logical operator:!     & (and, and) && (short-circuit operator) | || (short-circuit operator) ^ (XOR)
4. Xor operator: Convert to binary form to compare each digit, not the same as 1, the same as 0
5. Bitwise operators:>> << >>> (unsigned Right shift)
6. Assignment operator: = + = = *=/=%=
7. String Join Operator: +
8. Note: In the output, as long as one parameter is a string, the entire output is a string
9. Ternary operator (expression) (Trinocular operator): Boolean value 1: Value 2

10. Self-increment arithmetic operator

1. The self- increment (+ +) decrement (-) operator is a special arithmetic operator that requires two operands in an arithmetic operator, while the self-increment decrement operator is an operand.

 public  class   text{ public  static  void   main (string[] ages) { int  a = 3;//  define a variable;  int  b = ++a; //         self-increment operation         int  c = 3;  int  d =--c; //                System.out.println ("The value since increment is" + b);    System.out.println (the value after the  "is" + D); }}

Output:

Analytical:

int b = ++a; the split operation process is: a=a+1=4; B=a, the final result is b=4,a=4

int d =--c; The splitting operation process is: c=c-1=2; D=c, the final result is d=2,c=2

2, prefix self-increment (++a,--a): the first self-increment or self-subtraction operation, and then the expression operation.

3, suffix self-subtraction (a++,a--): The first expression operation, then self-increment or self-subtraction operation.

 Public class text{    publicstaticvoid  Main (string[] ages) {        int a = 5 ; // define a variable;        int b = 5;         int x = 2*++A;         int y = 2*b++;        System.out.println ("increment operator prefix operation a=" +a+ ", x=" +x);        System.out.println ("b=" +b+ "after the increment operator suffix operation, y=" +y);}    }

11. Relational operators

The following table is a Java-supported relational operator

The value of the instance integer variable A in the table is 10, and the value of variable B is 20:

 Public classtext{ Public Static voidMain (string[] ages) {intA = 10; intb = 20; System.out.println ("A = = b =" + (A = =b)); System.out.println ("A! = b =" + (A! =b)); System.out.println ("a > b =" + (A >b)); System.out.println ("A < b =" + (A <b)); System.out.println ("B >= A =" + (b >=a)); System.out.println ("B <= A =" + (b <=a)); }}

12. Bitwise operators

Java defines a bitwise operator, which is applied to types such as Integer type (int), long Integer (length), short integer (shorter), character type (char), and byte type (byte).

The following table lists the basic operations of the bitwise operators, assuming that the value of integer variable A is 60 and the value of variable B is 13:

 Public classtext{ Public Static voidMain (string[] ages) {intA = 60;/*= 0011 1100*/      intb = 13;/*= 0000 1101*/     intc = 0; C= A & B;/*0000 1100*/System.out.println ("A & B =" +c); C= A | b/*1101 = 0011*/System.out.println ("A | b = "+c); C= a ^ b;/*0001 = 0011*/System.out.println ("a ^ b =" +c); C= ~a;/*-61 = 1100 0011*/System.out.println ("~a =" +c); C= a << 2;/*0000 = 1111*/System.out.println ("A << 2 =" +c); C= a >> 2;/** = 1111*/System.out.println ("A >> 2 =" +c); C= a >>> 2;/*= 0000 1111*/System.out.println ("A >>> 2 =" +c); }}

13. Logical operators

The following table lists the basic operations of the logical operators, assuming that the Boolean variable A is true and that the variable B is false

 Public class text{    publicstaticvoid  Main (string[] ages) {         Boolean  true;          Boolean false ;         System.out.println ("a && B =" + (a&&b));         System.out.println ("a | | b = "+ (a| | b));         SYSTEM.OUT.PRINTLN ("! (a && B) = "+! (A && b));}    }

14. Assignment operators

The following are the assignment operators supported by the Java language:

 Public classtext{ Public Static voidMain (string[] ages) {intA = 10; intb = 20; intc = 0; C= A +b; System.out.println ("C = a + b =" +c); C+=A; System.out.println ("C + = a =" +c); C-=A; System.out.println ("c-= a =" +c); C*=A; System.out.println ("C *= A =" +c); A= 10; C= 15; C/=A; System.out.println ("C/= A =" +c); A= 10; C= 15; C%=A; System.out.println ("C%= A =" +c); C<<= 2 ; System.out.println ("C <<= 2 =" +c); C>>= 2 ; System.out.println ("C >>= 2 =" +c); C>>= 2 ; System.out.println ("C >>= A =" +c); C&=A; System.out.println ("C &= 2 =" +c); C^=A; System.out.println ("C ^= A =" +c); C|=A; System.out.println ("C |= A =" +c); }}

15. Conditional operator (?:)

The conditional operator is also known as the ternary operator. The operator has 3 operands and needs to determine the value of a Boolean expression. The main decision of the operator is to decide which value should be assigned to the variable.

format: var x = () ? value if true : value if false< /c18>

 Public classtext{ Public Static voidMain (string[] ages) {intA, B; A= 10; //if a equals 1, set B to 20, otherwiseb = (A = = 1)? 20:30; System.out.println ("Value of B is:" +b); //if a equals 10, set B to 20, otherwiseb = (A = = 10)? 20:30; System.out.println ("Value of B is:" +b); }}

Java Fundamentals 1

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.