Java program basic structure and data type

Source: Internet
Author: User

1 //source program Hello.java2  Public classHello3 {4     StaticString str = "Hello World";5      Public Static voidMain (String args[])6     {7 System.out.println (str);8     }9}

This is a simple way to see some of the basic structure of Java. (These are some of the understanding of predecessors to do the next record)

1, in Java Everything is an object, data and functions must be encapsulated in the ' class '. A source file contains at least one class (Hello) to compile and execute.

2, use the keyword class to declare a class, in this case the class name is Hello, the source program file name must be the same name as the class, that is, Hello.java disk.

3, the Java language is case-sensitive, by convention, the class name begins with capital letters, variables, methods, and object instance names start with lowercase.

4. The syntax within the class declaration statement after {...} becomes a class body, and he can include several data variables and functions.

5. In Java, a data variable is a "member variable" or a member of a class, or a function in Java called a "member method" of a class:

A, in this case, class Hello consists of a member variable STR and a member method main;

b, member Str is a string class object;

6. The main () method must be included in the application Application Master class:

A, the modifier before the main method indicates once that the method is public, static, and no return value (void). The main method must use these three modifiers;

B. You can define multiple classes in a Java program, and each class can define multiple methods, but one. JAVA files, up to only one public class, the main () method can only have one, and as the entry for the program, other member methods can be called.

In the C, main () method definition, String args[in parentheses () is a parameter passed to the main () method, with the parameter named args, which is an object of class string and can be 0 or more arguments.

7, the main () method only with one statement System.out.println (str), the function is the standard output device (screen) output string str.

8, each statement has a semicolon end; class body, method and statement block with curly braces;

(Below is a simple combination of Eclipse's understanding of Java Engineering Management, which is casually written.) Very irregular, follow-up will be done in the detailed mapping supplement)

After creating a new project with Eclipse, in a package of multiple classes, compile the generated. class file in the same folder.

class file Directory C:\Users\qq\Desktop\rr\test\bin\test

Java file directory C:\Users\qq\Desktop\rr\test\src\test

Each package, a single folder is generated. All the classes in each package will be in the directory of the folder where the package resides.

The current understanding of the internal and subordinate relationship is: Engineering, source code files, all kinds of standard library, various packages (package), a variety of classes with the main function of the subdivision of some classes

The file directory is the program folder->bin folder (placed in a folder-by-package-differentiated folders, the class file generated by the classes within each package, the same package does not allow the same class name to appear), the SRC folder (placed in a package-differentiated folder Java files of the class within each package, i.e. source code)

Basic data type:

A total of eight basic data types in Java
BYTE, short, int, long, float, double, char, Boolean
Integral type
Where byte, short, int, long all represent integers, except that their range of values is not the same
Byte has a value range of -128~127 and occupies 1 bytes (-2 of 7 to 2 of 7) (1)
Short has a value range of -32768~32767, which takes 2 bytes (-2 of 15 to 2 15-1)
The value range of int is ( -2147483648~2147483647), which takes 4 bytes (-2 of 31 to 2 31-1)
The Long value range is ( -9223372036854774808~9223372036854774807), which takes up 8 bytes (-2 of the 63-to-2 63-square-1)

Floating point Type

float and double are data types that represent floating-point type, the difference between them is that their accuracy is different
Float 3.402823e+38 ~ 1.401298e-45 (e+38 is multiplied by 10 for 38 times, similarly, e-45 means 10 minus 45 times) consuming 4 bytes
Double 1.797693e+308~ 4.9000000e-324 takes 8 bytes

Boolean (Boolean type)
This type has only two values, True and False (true and non-true)
Boolean t = true;
Boolean f = false;
Char type (text type)
The data type used to hold characters, 2 bytes, Unicode encoded, and ASCII compatible with the first 128 bytes of encoding
Character storage range in \u0000~\uffff, when defining the character type of data should pay attention to ", such as ' 1 ' means the character ' 1 ' instead of the value 1,

Data type conversions:

Small-capacity types can automatically go to large-capacity data types

Byte->short->int->long->float->double

There are two ways to convert a type with a large capacity to a small type that requires casting, and the data accuracy is lost, as far as possible using the second method;

①double a = 1.2; int b = (int) A;

②double a = 1.2; int B = (new Double (a)). Intvalue ();

Java program basic structure and data type

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.