Fundamentals of Java 20160806

Source: Internet
Author: User

Note that the Java Foundation introduced here refers to the C language you are already more skilled or have a certain basis, and then learn the following this knowledge will be faster.

The 1.JAVA is also executed from main, but the class must be defined first, the file name is identical to the class name and the first letter of the class name is capitalized , and Java does not have an include.

It does not need to be included, when it is necessary to use an additional class Library, import is required, and for imported objects that are not static, you need to create a re-object instantiation before you can use it. Of course, if you do not use an additional class library, you do not need to import.

However, for example, some system-provided libraries (which do not need to be imported), such as System.out.println, are static objects, so this can be used to print directly

The development environment is recommended for use under Ubuntu, such as compiling direct input: Javac Hello.java

Execute direct input: Java hello (which is performed using a Java Virtual machine)

2. Data type comparison:

The basic type of Java is boolean,byte, where bool and byte occupy the same number of bytes, but the value range is not the same, where the bool value range is only True/false,

Char uses 2 bytes because Java uses Unicode (a byte of C)

references include: arrays, classes, interfaces, null

3. Comments are the same way

4.souceinsight is Ascii,java compiled by the use of UTF8 parsing , and Si in the written Chinese character is ASCII, so UTF cannot parse ASCII, compile error, solve the method without Chinese characters, Either use your UE to convert the file encoding format

5. There is no malloc function in Java, there is no pointer , if you want to use malloc like the way it is similar to the use of arrays , so the definition of malloc in Java and define the array is the same:

The use of Char s[]=new char[100] In Java is to assign the address in the heap and then give the address to the variable in the stack .

The array in C defines space and points to the same address as normal variables in the task stack.

All arrays in Java are reference data types, no pointers, called references, and values inside variables are addresses of a heap of addresses .

Java other ways to define an array int p2[]={1,2,3} This is a static assignment. In c the data is placed in the stack,

Java has a stack on the left and a heap on the right, so the arrays in Java are in the heap , and the stack is a variable that stores the address to the heap space.

The array in Java is always the reference data type, and the space of the array is always inside the heap.

5. Define a string in which a string in Java is a class that defines a string that cannot be used in the C language

C Way: Char sh2[]= "AVVC", or in the stack, generally except malloc are in the stack

Java definition method String shr= "AVB"

(The so-called reference data type (similar to a pointer) is the space that the variable points to refers to the space in the heap , the variable (storage address) in the stack )

c The program (main) exits free, but always executes the main word will be free, otherwise it will always leak, or always allocated.

Java is automatically freed , and when the pointer to a variable equals null , Nothing is referenced in the heap space , and the garbage collection mechanism in Java frees the space. So you don't have to let p=null.

When there are two variables pointing, i.e. p2=p1= ..., both are equal to NULL to be released.

6. Data type conversions:

In Java, 3.14 is a double type, which is converted in the following way:

float f= (float) 3.14;//cast

Float f2=3.14f;//automatic conversion occurs when a small range of data types is converted to Big data types

Short S=1;

S=s+1 error, the number is generally int type, at the same time participate in the operation, for Byte,short, in order to ensure the accuracy, will be automatically converted to int, and then assign the value of int to short of course error.

Short s2=1;

So s=s+s2 is also wrong, to strengthen the system conversion.

7. Operators, expressions, statements and C are basically exactly the same,

Functions in 8.c, Java is called a method,

Java method:

Format: public static int Add (int x,inty) {

} More public static

The addition of the public keyword means that this method can be called externally, and the method preceded by static means that it can be called when no object is created (called by: Class name. method),

Also, if the field ( data member) is preceded by static , it means that the field has only one copy of the space for each object created by the same class, and without static there is a storage space for each object.

9.java overload, the function name (return value type) is consistent, the parameters can be overloaded, the call is called the corresponding parameters of the method (call is the actual participation parameter consistent),

When modifying the return type is not overloaded because you do not know what kind of results you want.

10.java function parameter pass, the value cannot be changed can only address, C in the use of pointers, Java does not use a pointer like an array name implementation, such as:

int p[]=new int[1];

p[0]=123;

FUN2 (P);// Pass in the address

System.out.println ("fun2:" +p[0]);//format printing preceded by spaces, followed by a + sign

public static void Fun2 (int[] p) {

p[0]=200;//the content of the address to be modified is 200;

}

Fundamentals of Java 20160806

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.