Java Getting Started learning notes (0)--Understanding variables

Source: Internet
Author: User

HelloWorld
1 // Helloworld.java 2 3  Public class HelloWorld {4      Public Static void Main (string[] args) {5         System.out.println ("hello,world!" ); 6     }7 }
View Code

The foundation is not strong, shake. Learn or start with "HelloWorld"!

First program: 1, write and save source code <HelloWorld.java> 2, compile <javac helloworld.java> 3, run <java helloworld>

  Note : In a ". Java" source file, you can include multiple classes, but only one public class, and the class name of public must be consistent with the file name.

Recognize variables

There are two types of variables in Java: Primitive master data types and references.

Primitive main data types are: Boolean, char, Byte, short, int, long, float, double. For variables of these data types, you should be aware of "value overflow" in assignments or other operations.

Reference references
new book ();

There are 3 steps for declaring, creating, and assigning an object:

1. Declare a reference variable/* Book MyBook */, require the JVM to allocate memory space to the reference variable, and name this variable myBook, type book type;

2. Create Object/* new book () * *, require the JVM to allocate heap space to the newly created book object;

3, connection objects and references/* Book MyBook = new book () */, the new book is assigned to myBook this reference variable.

References should be interpreted as pointers in the Java language. Note The following points:

1. An object variable (reference variable) does not actually contain an object, but simply refers to an object.

2, you can explicitly set the object variable to NULL, indicating that the object variable does not reference any object.

3. The local variable is not automatically initialized to null, and must be initialized by calling new or setting it to null.

4. All Java objects are stored in the heap.

The difference between an instance variable and a local variable

Instance variables are declared within a class and not within a method, and there will always be default values, even if there is no definite assignment or call setter, and the instance variable has a value.

The local variable is declared in the method, and there is no default value, must be initialized before use, if used before the initialization of the variable, the compiler will error.

Comparison of variables (primitive master data type or reference)

Use = = to compare two primitive master data types, or to determine whether two references refer to the same object;

Use Equals () to determine whether two objects are equal in meaning.

For arrays, the array is always an object, whether it is declared to host a primitive master data type or an object reference. The array must not be a primitive master data type, it must be an Object!

  

Java Getting Started learning notes (0)--Understanding variables

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.