"Java from Beginner to Mastery" Chapter III study notes

Source: Internet
Author: User
Tags java keywords

3rd Chapter Java Language Basics

I. Identifiers and Keywords

1. Identifiers in Java are used to identify class names, variable names, method names, array names, and file names.

2. The naming rule for identifiers: consists of letters, numbers, underscores, and dollar signs, but the first character cannot be a number. At the same time, the identifiers cannot use Java keywords and reserved words.

3, the general Convention naming rules: (1) class and interface name: Each word capitalized, such as Myclass,helloworld; (2) method name: First letter lowercase, the other words capitalized, as little as possible underline, such as mymethod,mygetdata; (3) Constant name: the constant name of the basic data type is all uppercase letters, the words are separated by underscores, such as Size_name; (4) Variable name: The first letter is lowercase, the remaining words are capitalized, not underlined, and as little as possible with the dollar sign.

II. Basic data types

1, the integer type: has byte\short\int\long four kinds, the storage space is 1, 2, 4, 8 bytes, in which the long type variable assignment must at the end plus L or L, such as: Long bignumber=1234567l.

2, floating-point type: Float single-precision and double double-precision two, respectively, accounting for 4, 8 bytes. Where the float type variable is assigned a value must be F or f at the end.

3, Character type: Use Char to declare, occupy two bytes.

4. Boolean type: Use a Boolean to declare only two values by true and false.

Iii. Variables and constants

1. Variable valid range (scope) (1) variables defined in the class body are called member variables, and member variables are valid throughout the class. The member variables of a class are divided into static variables and instance variables, static variables are determined by the modifier static, and can be used in other classes in the same way as the class name. Static variable name.

1 classvar{2    intx=3;//This defines an instance variable x for the Var class3    Static inty=30;//this defines a static variable y for the Var class .4 }5 classbar{6    intA=var.y;7 /*8 defines an instance variable a of the bar class,9 and assigns the value of the static variable y in the Var class to aTen */ One}

(2) A variable defined in the method body of a class is a local variable, valid only in the current code block, whose life cycle is allocated memory space only when the method is called, and the memory is freed when the method call finishes.
(3) In a method body of a class, a static variable created by the class body can be directly used, and when the static variable has the same name as a local variable created in the method body, the static variable is hidden, and if a static variable of that class must be called, use the "class name. Static variable Names" method.
Note: After testing, instance variables in the class body cannot be used in methods.

1  Public classNumber {2     Static intmydata=414;3     Static intyourdata=1973; 4          Public Static voidMain (string[] args) {5         intmydata=1314;6System.out.println ("The value of the class body static variable MyData is" +number.mydata);7System.out.println ("The value of the class body static variable Yourdata is" +yourdata);8System.out.println (the value of the local variable MyData inside the method is "+MyData);9     }Ten } One /* A Output---------- - the value of the class body static variable MyData is 414 - the value of the class body static variable Yourdata is 1973 the The value of the local variable MyData inside the method is 1314 - ---------------- - */

Iv. Operators
XOR Application Example: The value interchange of two variables is not implemented using auxiliary variables

1 ImportJava.util.Scanner;2  Public classNexchange {3      Public Static voidMain (string[] args) {4Scanner scan=NewScanner (system.in);5System.out.println ("Please enter the value of variable a");6         LongA=Scan.nextlong ();7System.out.println ("Please enter the value of variable B");8         Longb=Scan.nextlong ();9System.out.println ("a=" +a+ "\tb=" +B);TenSYSTEM.OUT.PRINTLN ("Perform variable interchange ..."); Onea=a^B; Ab=b^A; -a=a^B; -System.out.println ("a=" +a+ "\tb=" +c);  the     } - } - /* - This applies the bitwise operation XOR, for the integer X has: x^x=0; X^0=x, simultaneous XOR or operation satisfies the binding law and the Exchange Law + bring the a=a^b into the following two, respectively: - b=b^a=b^a^b=a^ (b^b) =a^0=a; + a=a^b= (a^b) ^a=...=b; A ---------- at extension: In addition to the Xor method, the method that does not pass the auxiliary variable also has: - (1) a=a+b;b=a-b;a=a-b; - (2) a=a*b;b=a/b;a=a/b; - */

v. Examples
1, judge whether a year is a leap years

1 ImportJava.util.Scanner;2  Public classleapyear{3      Public Static voidMain (string[] args) {4Scanner scan=NewScanner (system.in);5System.out.println ("Please enter year (0~99999):");6         LongYear=Scan.nextlong ();7          while(year<0| | year>99999){8SYSTEM.OUT.PRINTLN ("Please reenter the Year (0~99999):");9Year=Scan.nextlong ();Ten         } One scan.close (); A         if((year/400==0) | | ((year/4==0) && (year/100!=0))){ -System.out.println ("Enter year as" +year+ "is leap years"); -         } the         Else { -System.out.println ("Enter year is" +year+ "is not a leap years"); -         } -     } +}

2, do not use the multiplication sign operator, with the shift operator to calculate the 21*16.

1  Public classMovebit {2      Public Static voidMain (string[] args) {3         Longa=21l;4         intX=1;5          for(inti=1;i<5;i++){6x*=2;7A=a<<1;8System.out.println ("21 left move" +i+ "bit is multiplied by" +x+ "result for" +a);9         }Ten     } One}

"Java from Beginner to Mastery" Chapter III study notes

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.