Java Basic syntax

Source: Internet
Author: User

Introduction to Java Basic grammar experiment

In this chapter we will learn about the key words, identifiers, annotations, variables, and constants in the basic Java syntax.

First, the development of a Java program

Do you remember the Hello world of the last lesson? In this lesson, let's take a look at this piece of code first.

    public class HelloWorld        {            public static void main(String[] args) { System.out.println("Hello World!"); } }

Some of the basic features of Java are included in the above program:

    • Class: The above program defines a class HelloWorld whose name is the same as the name of the. java file.

    • Method: A method of the class is defined inside the class, where main is called the main function and is the entry point of the Java program.

    • Statement (statement): the "print" feature is implemented by a statement that:System.out.println("Hello World!");

The following two points about how Java is written:

    • The statements in Java are to ; end with.

    • Use curly braces {} to combine statements to form blocks of programs. Through the block, we can know the scope of different parts of the program, such as where the class starts and where it ends.

Second, the Java keyword

Java's keywords have special meanings for Java compilers, they are used to represent a data type, or to represent the structure of a program, and the keyword cannot be used as a variable name, method name, class name, package name.

The Java keyword has the following table listed, the students first have an impression, the specific meaning we will in the following content in detail:

Third, Java identifiers

In the Java language, for variables, constants, functions, and statement blocks there are also names, which we all call Java identifiers.

Identifiers are used to name classes, objects, methods, variables, interfaces, and custom data types.

Java identifiers consist of numbers, letters and underscores _ , and dollar signs $ . is case-sensitive in Java, and also requires that the first digit cannot be a number. Most importantly, the Java keyword cannot be used as a Java identifier. The following identifiers are valid:

$money, _Java_learning, OK, _3th, _3_

The following identifiers are illegal:

#shiyan,25years,class,&name,if

In Java, there are also some rules of the rule of naming, I hope that students in writing code can follow these rules:

    1. Class and interface name. The first letter of each word is capitalized and is case-sensitive. For example, Myclass,helloworld,time and so on.

    2. The method name. The first character is lowercase, the remainder is capitalized, and is case-sensitive. Use as little underline as possible. For example, Myname,settime and so on. This naming method is called Camel-named.

    3. The constant name. The constant name of the base data type uses all uppercase letters, and the words are separated by underscores. Object constants can be mixed in size. For example, Size_name.

    4. The variable name. Can be mixed in uppercase and lowercase, the first character is lower case, and the word delimiter is capitalized with the first letter of the word. Don't underline, use the dollar sign less. The name of the variable is as far as possible to see the name of understanding.

    5. Naming process as far as possible to see the name of the understanding, convenient later review and modify the code, but also facilitate the reading of other people.

Iv. variables

Remember the Java keyword we saw above? Part of the keyword is the type of data we'll use later. Usually when we define a variable, we precede the variable name with the data type that corresponds to the variable.

Computer languages often need to store data in memory, such as variables in C, and Java has similar variables. Both the Java and C languages are static types of languages. Declare the type of the variable before you use it.

Variable (variable) occupies a certain amount of memory space. Different types of variables occupy different sizes. The types of variables in Java are as follows:

Td>double
data type default storage format data range
Short 0 2 bytes -32,768 to 32767
int 0 4 bytes -2,147,483,648 to 2,147,483,647
byte 0 1 bytes -128 to 127
Char /u0000 2 bytes Unicode character range
long 0L or 0l 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036, 854,775,807
float 0.0F or 0.0f 4 bytes 32 bit ieeee 754 single precision range
0.0 or 0.0D (d) 8 bytes 64-bit IEEE 754 double precision Range
boolean false 1-bit true (1) or False (0)

In Java, a variable needs to be declared (declare) before it can be used. In the declaration, describe the type of the variable, giving the variable a special name so that it is called in a later program. You can declare variables anywhere in the program. Like what:

    public class Test    {        public static void main(String[] args) { System.out.println("Define a variable a is "); int a; //声明变量a a = 5; System.out.println(a); // 打印一个整数a } }

Above A is the variable name. You can assign a value to a variable while declaring it, such asint a = 5;

  变量Concept is actually derived from the process-oriented programming language. In Java, the so-called variables are actually 基本类型 (premitive type) . We will be more in-depth in the class explanation. The above program can also be seen in Java, which is used to // lead annotations.

Speaking of annotations, when we write code, in order to facilitate understanding and reading, often in the relevant code is added near the explanatory text, which is our comments. The comment is to prevent the compiler from compiling our comments at compile time, which causes the program to go wrong, so the compiler will automatically skip the comments we wrote at compile time.

There are generally three types of annotations in Java:

    • Line Comment // : Only one line is commented

    • Paragraph notes /*...*/ : comment on several lines

    • Document comments /**...*/ : comment on several lines and write to Javadoc document

V. Automatic type conversion and forced type conversion

Little friends, let's run the following program segments, what will the results be in the console output?

public class HelloWorld{    public static void main(String[] args) { double avg1=79.5; int rise=10; double avg2=avg1+rise; System.out.println("调整前的平均分:"+avg1); System.out.println("调整后的平均分:"+avg2); }}

In the above program, we have defined three variables: AVG1, Rise, AVG2, where AVG1 is a variable of type double, and rise is a variable of type int, what type of variable of two different types is added together? Yes, the rise is automatically converted into a variable of type double during the operation.

In Java programs, different data types sometimes need to be converted to each other. The conversion of data types is divided into 自动类型转换强制类型转换

Automatic type conversion is in the process of program execution, do not need to go to special declaration or operation, the variable is automatically converted to the appropriate data type as needed.

The following two conditions are required for an automatic type conversion:

    1. Target type compatible with original type

    2. The number of bytes in the destination type is greater than or equal to the original type of bytes

VI, constants

Sometimes when we write a program and don't want to change some of the values because of some operations, we're going to use the concept of constants. So-called constants, we can also interpret them as special variables, except that they are not allowed to change in the course of a program's operation.

There are two meanings of general constants:

    1. The first meaning is that the value itself, such as an integer 9, is said to be "a constant of type int 9". Of course there are real constants 3.14, character constants ' A ' and so on.

    2. The other constant represents the immutable variable, which is when we add the final keyword before the variable declaration. For example, the final int i=0 value of this I cannot be modified.

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.