Java main class structure: variables and constants

Source: Internet
Author: User
Tags finally block throw exception volatile java keywords

1. Identifiers

Identifier: Used to represent the class name, variable name, method name, array name, valid character sequence of file name
Identifiers by any order of letters, underscores such as: Name, User_age, $page identifier characters cannot be numeric identifiers cannot be reserved keywords in Java

2. Keywords

How many Java keywords (keyword) are there?
51+2 a reserved word = 53 Keywords (java keywords are lowercase!!) )
What is the reserved word for (reserve word)? Question: What are the differences?
2 reserved words
Reserved words in the Java language refer to reserved keywords
1). Const Youdao Interpretation: N. Constants, constants
The declaration used to modify a field or local variable. It specifies that the value of a field or local variable is constant and cannot be modified
2). Goto Youdao Interpretation: VI. Go to
Specifies to jump to the label, and when the label is found, the program processes the command starting from the next line.

What are the keywords, and what are the functions?
(1) Keywords for access modifiers (3 total)
Public: Publicly available across packages, (default selection)
Protected: Protected, available in the current package
Private: The current class is available
(2). Define classes, interfaces, abstract classes and implement interfaces, inherit class keywords, instantiate objects (total 6)
Class: Classes, public Class A () {} curly braces have the implemented method body, the class name needs to be the same as the file name
Interface: interface, public interface B () {} curly braces have a method body, but no implementation, the method body sentence is followed by the English semicolon ":" End
Abstract: Declarative abstraction, public abstract class C () {} is between class and interface, can or does not have an implemented method body
Implements: Implementation for class or interface implementation interface public class A interface B () {}
Extends: Inheritance for class-inheriting classes public class A extends D () {}
NEW: Create a a=new a (); A denotes a class
3). Package keywords (total 2)
Import: Introduction of the Package keyword, when using some classes of a package, only the class name and then use Ctrl+shift+o or select the class name (class or property or method)
Press CTRL + Click to automatically insert the package that contains the class. such as: JFrame shortcut key after automatically join, import javax.swing.JFrame;
Package: Define the keywords for the packages, put all the relevant classes in a package class to find modifications, and so on. such as: Package javake.flycat.draw002;
4). Data type of keywords (12 total)
BYTE: Byte type, 8bit
Char: Character type, 16bit
Boolean: Boolean, can only judge Ture and False (can only be used to determine the true and false)
Short: Shorter integer, 16bit short type integral type
int: integral type, 32bit
float: float type, 32bit
Long: Length integer, 64bit
Double: dual precision, 64bit
void: no return, public void A () {} Other required returns are often used with return
Null: null value
True: True
False: False
5). Condition cycle (flow control) (Total 12)
if: if if () {} What about curly braces in parentheses?
else: Otherwise, or, often with if, use the same
While: when what, while how to do what while () {}
For: When three conditions are met, for (;;) {}
Switches: Switch: switch (expression)
case constant Expression 1: statement 1;
Case constant Expression 2: statement 2;
Default: statement;
Default is to execute a case without a match, and default is not required.
The statement after the case can be used without curly braces.
The decision condition of the switch statement can accept Int,byte,char,short and cannot accept other types.
Case: Returns the result in the switch
Default: Defaults
Do: Run, long and while
Break: Jump out of the loop
Continue: Continue, interrupt this cycle, and start the next
Return: Returns a return value type
Instanceof: Example, a two-dollar operator, and ==,>,< are the same class. Tests whether the object on its left is an instance of the class to the right of it, and returns a Boolean type of data

(6). Modifying methods, classes, properties, and variables (9 total)
Static: Statically, properties and methods can be modified with static, using the class name directly. Property and method name.
Only inner classes can use the static keyword decoration, which calls directly using the class name. The inner class name is called. Static can exist independently. Static block
Final: Ultimately immutable, methods and classes can be modified with final, the final modified class cannot be inherited, the final decoration method cannot be overridden by the quilt class.
Definition of a constant: a property that is inal decorated is a constant.
Super: Call the method of the parent class, common public void paint (Graphics g) {super.paint (g);
This: The object of the parent class of the current class, calling a method in the current class (representing the object that called the method), This.addactionlistener (AL): And so on.
Native: Local
STRICTFP: Strict, precise
Synchronized: threads, synchronizing
Transient: Short
Volatile: Volatile
static Example:
public class test{
Class a{}//Inner class
ActionListener al=new ActionListener (...) {}//anonymous inner class
//       }
Static blocks take precedence over the execution of other methods/classes
7). Error handling (total of 5)
Catch, Handling Exceptions:
The procedure is run into a try block, and if an exception is thrown, go to the catch block to process it. Then execute the statement after the catch block

The procedure is run into a try block, if an exception is thrown, go to the catch block, after the catch block executes, execute the code of the finally block, and then execute the code after the finally block.
If no exception is thrown, execute the try block and execute the code of the finally block. Then execute the statement after the finally block

The procedure is run into the try block, and if an exception is thrown, the program turns to the code that executes the finally block. Will the code behind the finally block be executed? No! Because you did not handle the exception, after you have encountered an exception, the method exits with the exception that was thrown after the finally execution.
It is important to note that because you do not catch an exception, you declare the throw exception after the method
Try: Catch exception
Finally: There are no exceptions to execute
Throw: Throws an Exception object//Some of the factors that can cause problems with the program, such as writing errors, logic errors or API application errors, and so on. In order to prevent the program from crashing, it is necessary to detect these factors beforehand, so Java uses the mechanism of exception.
Exceptions in Java are used by "throw", or "throw" in English, which means "throw" the error message if something is found to be abnormal.
Throws: Declares an exception that could be thrown, handing the exception to his superiors and not handling the exception himself.
Throw is the action you perform. For example, if you think there may be an abnormality, then take it out like this:
String A; if (a = = null),
Throw new Exception ("A is null");
So throw is a throw-away action
Throws is used only at the end of a method, which means that if there is an exception inside the method body, this throws it to its caller. such as: public void Add (int a, int b) throws Exception (); This method indicates that, when executing this method, an exception may be generated, and if an exception is generated, then whoever calls this method will throw it to the person. (from Baidu)
8). I don't know what it is (Total 2)
Enum: enumeration, enumerating types
Assert: Assert
Enum: Represents a set of common constants that can be used to represent a class of constant values of the same type, such as:
public enum Sexenum {male, female;}
public enum Color {
RED, Blue,green,black;
The values inside the enumeration object must be unique.

Declaring variables:

What defines a variable?

For example: Int age;//declares that age is an int type

Char char1= "R";//declare CHAR1 as variable and assign value R

Declaring constants: Final data type constant name [= value]

Final double pi=3.1415926d;//declares a double-type constant PI and assigns a value

public class Part {
Declare pi at this point, such as not assigning a value to Pi
Static final double pi=3.14;//to PI value 3.14, statically
static int age=5;//integer type age is 23, member variable, static
public static void Main (string[] args) {
final int number;//declaration int type constant number
System.out.println ("The value of the constant PI is:" +pi);
SYSTEM.OUT.PRINTLN ("Number value after assignment is:" +number);
System.out.println ("The Age value of the int variant is:" +age);

The result is: the value of the constant pi is: 3.14
The number value after assignment is: 1235
The age value of the int variant is: 3

Java main class structure: variables and constants

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: 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.