Java Basic program Design structure (i)

Source: Internet
Author: User
Tags float number uppercase letter

Java Core Technology Volume Ι Basics (Nineth edition) Learn notes

Chapter Three Java BASIC program design structure


1 First Application

public class firsttest{public
    static void Main (string[] args) {
        System.out.println ("hello,world!");
    }

This is the basic mechanism for Java applications. Java is sensitive to case, and if a capitalization error occurs (such as: Main is written as main), the program may not run.

In the preceding code, the first keyword public is called the Access modifier, which is used by the other part of the control to access the level of the Code, access rights.

The keyword class indicates that all content in the Java program is contained in the class.

The class name follows the keyword class. Rule of class name: Must start with a letter, followed by any combination of letters and numbers, the length is unlimited, you cannot use keywords and reserved words as the class name.

From the class name Firsttest, the standard class name specification is: The class name starts with an uppercase letter, if it consists of multiple words, the first letter of each word is capitalized, and the other is lowercase.

The code has the same file name as the public class class name, and it has the. java extension, which means the file name for the above code corresponds to Firsttest.java.

The Java compiler compiles the code (Javac Firsttest.java), generates a byte-code file (Firsttest.class) with different extensions of the same name in the same directory, and then enters the Java firsttest program after the command is executed. The console will output Hello,world.

The main () method is the entry that the program executes, and must be public static.

In the main method, the System.out object is used and its println method is invoked. Note that the (.) point number is used to invoke the method member variable. Call method: Object.Method ();

Method must precede the method name with parentheses (), regardless of parameters.

System.out.println ("Hello"), output hello and wrap.

System.out.print ("Hello"), no wrapping after the output hello. The back output is immediately following the O letter.


2 Notes

Three kinds of annotations

① single-line Comment//comment content from//to the end of the bank

② all characters between/*///////////* * are ignored by the compiler

③ Document Annotation/** * * All characters between/** * * are ignored by the compiler, Java Special (used to generate documents);

Multiple-line comments and document annotations cannot be nested.


3 Data Types

Java data types are divided into basic data types and reference data types. There are 8 basic types of data, 4 integers, 2 floating-point types, 1 Boolean types, and one character type.


3.1 Integral type

The integral type includes four kinds, from small to large is byte, short, int, long. They represent values that do not have a decimal part, and are allowed to be negative.

1, Byte:byte type (byte type) data occupies 1 bytes in memory, the storage data range of the expression is: -128~127.

2. The Short:short type (short integer) data occupies 2 bytes in memory. Rarely used, it restricts the storage of data to first-high bytes, followed by low bytes, so that errors can occur in some machines.

3. The Int:int type (integer) data occupies 4 bytes in memory. The most commonly used integral type. It represents a large enough range of data and is suitable for 32-bit, 64-bit processors. For large computations, however, a large integer is often encountered that exceeds the range represented by the int type, and a long type is used.

4. The Long:long type (Long integer) data occupies 8 bytes in memory. The Long data is appended with L or L.

decimal integers, such as 123,-123,11.

An octal integer that begins with 0, such as 023 for the decimal number 19,-0111 for decimal-81.

A hexadecimal integer that begins with a 0x or 0X, such as a decimal number 291,-0x12 0x123 decimal-18.

Starting with Java7, you can use binary numbers, plus prefix 0b, such as 0b101 is 5.

At the same time, JAVA7 can be used to underline numbers without affecting the values just to make them easier to read. If 1_000_000 says 1 million.

3.2 Floating-point type

A floating-point type is used to indicate a number without a decimal part. Two floating-point types: float (single-precision floating-point type) and double (double-precision floating-point type).

Float:4 Byte, 32 bits. The float number needs to be added with the suffix F or f, which runs faster than the double, takes up less memory, but becomes imprecise when the value is very large or very small. Float type can be used when the precision requirement is not high.

Double:8 byte, 64 bits. A floating-point number with no suffix f or f is the double type by default, and the double can also be appended with a suffix D or D. Its precision is twice times the float type, and most applications use the double type.

3.3 Char Type
Char type: 2 bytes, used to represent a single character. It is usually used to denote the constants quantity of characters. For example: ' A ' is the number of characters constants encoded as 65, and "A" is a string with content of a.

Java uses the Unicode character set to represent characters, and Unicode is a fully internationalized character set that can represent all characters in the human language. Unicode requires 16-bit width, so the char type in Java is also represented by the bit.    The assignment is this: char ch1 = 23; Char CH2 = ' A ';

3.4 Boolean Type

A Boolean (Boolean) type has two values: false and True to determine the logical condition. cannot be converted to an integer value.

Note: in C and C + +, values can be substituted for Boolean values. 0 is the equivalent of a Boolean value of false, and a non 0 is equivalent to true. This is not possible in Java.

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.