Basic Java Knowledge

Source: Internet
Author: User

I. identifiers, Keywords

the definition format for identifiers in Java consists of letters, numbers,_(underscores), $, which cannot be duplicated, cannot start with a number, and cannot be a keyword in java. And each identifier must have its own meaning, such as Studentname, that is, the name of the student.

Note : Identifiers are case-sensitive.

Tip: Identifiers should always start with a letter, and try not to include other symbols.

J key words in Ava

Abstract

Assert

Boolean

Break

Byte

Case

Catch

Char

Class

Continue

Const

Default

Do

Double

Else

Extends

Enum

Final

Finally

Float

For

Goto

If

Implements

Import

instanceof

Int

Interface

Long

Native

New

Package

Private

Protected

Public

Return

Short

Static

Synchronized

Super

Strictfp

This

Throw

Throws

Transient

Try

void

Volatile

While

Second, the variable

1, definition: variable is the memory of the small box loading data, you can only use it to save data and fetch data.

The Smallest unit of information in a computer's storage device is called a bit, whichwe call "bit", usually in lowercase letter b . representation.

and The smallest storage unit of a computer is called"byte", usually denoted by the capital letter B , which is composed of 8 bits in a row .

1B (bytes) = 8bit

1KB = 1024B

2. Basic types of variables (4 classes 8 kinds)

Variables must have a definite type, what type of variable is loaded and what type of data.

There are 4 basic types in Java , the 4 classes are expanded after a total of 8 Basic types.

The default integer type in Java is type int

in the integer Constants in Java if you do not have to add an "L" suffix between -2147483648~2147483648 (lowercase can also, but is recommended to use uppercase), in -2147483648~2147483648 can also be added between the " L "suffix.

Integer constants with the "L" suffix added are of type long , for example:100L,12345678901L are constants of type long.

The default floating-point type in Java is double type

the float type constant must be added with an "F" suffix (lowercase also, but uppercase is recommended)

In addition to the 8 basic types (Byte,short,char,int,long,float,double,boolean), other types are reference types.

Example: string, defining a string

3. Defining variables

/*variable definition Format: Data type variable name = variable value;*/ Publicclass Variable { Publicstatic void Main (String[]args) {        intA= Ten;//define integral Type A, assign a value of tenDoubleB= 3.14;//Defines a floating-point type, variable name B, and a variable value of 3. -        CharC= 'Z';//Define character type, variable name c, variable value'Z'String S="I love Java";//define reference type, variable name s, variable value "I love Java" a=  -;    System.out.println (a); }}

4. Precautions for use of variables

1, the variable can not be assigned after the value, the use of re-assignment. No assignment can be used.

 public static void main (String[]  args) {int=+//  assigns a value of 20SYSTEM.OUT.PRINTLN (x) to X; // read the value in the X variable, and then print}

2. Scope restrictions when using variables

 public static void main (String[]  args) {int=+; {     int =  - ;} SYSTEM.OUT.PRINTLN (x); // read the value in the X variable and print System.out.println (y); // failed to read the value in the Y variable, reason for failure, cannot find Y variable, because the y variable is outside the scope, so you cannot use the Y variable}

3, variables can not be defined repeatedly.

 public static void main (String[]  args) {      int=ten;       Double = 5.5; // compilation failed, variable duplicate definition}

5. Data type Conversion

Can different types of variables work together? The answer is yes, but first the type conversion and then the operation.

Follow the principles:

A small range of data type values (such as byte) that can be converted directly to a large range of data type values (such as int);

A range of data type values (such as int) that cannot be converted directly to a data type value of a small range (such as byte)

List the various data types from small to large in the data range:

double, float, long , short, int

One, automatic type conversion

A data type that represents a small range is converted to a large range of data types, which is called automatic type conversion

Automatic type conversion format:

data types with large range variable = data type value with a small range;

Double =  + ; or int =  - ; Double = i;

Ii. Coercion Type conversion

A data type that represents a large range is converted to a small range of data types, which is called coercion type conversion

Forced type conversion Format:

data types with a small range variable = ( the data type with a small range ) has a large range of data type values ;

int  = (int) 6.718;   // The value of I is 6 or Double  = 3.14 ; int  = (int) D;     The value of //I2 is 3

Basic Java Knowledge

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.