Java basic variables and data types and related operations

Source: Internet
Author: User
Tags arithmetic variable scope

Java Basic Syntax:

1): The Java language is strictly case-sensitive, like main and main are completely different concepts. 2): Multiple Java classes can be defined in a Java source file, but at most one of these classes is defined as the public class. If the public class is included in the source file, the source file must have the same name as the public class. 3): When a source file contains n Java classes, a successful compilation generates an n-bit bytecode file, i.e. each class generates a separate class file with the same byte-code file name as its corresponding class name. 4): If a class must run, you must have the main method, because the main method is the entry for the program.

Programming Recommendations:

A Java source file defines only one class, and different classes use different source file definitions;

Define the individually defined classes in each source file as public;

Keep the main file name of the Java source file consistent with the class name in the source file;

Note Type

Single-line Comment:// // note information, all characters from // start to the end of the bank are ignored by the compiler; Multi-line Comment:/**/*/*/* * ** * * * * * * * * * * * The compiler ignores document comments:/**  */;/*** * As with multiline annotations, you can also specifically generate a Document information API.
Key words:

In programming languages, there are some pre-defined words that have special meanings and uses.

Reserved words:

As with the keyword is the programming language in advance definition, just said that there is no special use, but perhaps someday will suddenly be given meaning and be used, so the words are preserved, goto and const is the Java reserved word.

Attention

Keywords and reserved words are made up of lowercase

Java language delimiter:

Semicolon (;): The Division of a statement, the end of a sentence, as we use the period. Curly braces ({}): Represents a block of code, is a whole, curly braces are to be used in pairs. square brackets ([]): used when defining arrays and accessing array elements. Parentheses (()): Widely used, specifically used in detail. Dot (.): used when a class and an object access its members. Space (): a whole sentence divided into a few paragraphs, the number of spaces is not limited, like a word in English should be written separately. Note: The English symbol must be in the half corner. ; Ab;ab
Identifier:

When writing code, in order to enhance the readability of the code will be a lot of custom names, such as: Class name, method name, variable name and so on. In programming we have a name that is customized to enhance the readability of the program, called an identifier.

Identifier naming rules:

① consist of letters, numbers, underscores, and $, but cannot start with a number (note: The letters here can be Chinese, Japanese, etc.).

② is case sensitive.

③ must not use keywords and reserved words in Java.

④ does not use the built-in class names in Java for its own class name.

In fact, we do not need to remember so much, the use of a non-keyword and reserved words of meaningful English words is OK. Note The problem: the class name first character specification is uppercase.

Constant:

Fixed values that are not changed in the program.

Constant classification:

Literal Constants : For example: integer constant, decimal constant 3.14, Boolean constant false,true, etc... Literal, it represents a value directly given (can be integers, decimals, true,false, etc.),

Direct volume. The final variable defined:

Definition of variable variables:

Represents storage space that can be used to hold a constant of a certain type, has no fixed value, can be reused, and can be used to store unknown data of some kind. Like the name of the hotel room.

Characteristics of variables:

① occupy a piece of storage area in memory;

② the region has its own name (variable name) and type (data type);

③ can be reused;

④ the data of this region can be changed continuously in the same type range;

The definition of a variable:

A. Define variables first, then assign values: data type variable name; Variable name = value

B. Simultaneous assignment at declaration: Data type variable name = initialization value;

Note: The format is fixed, remember the format, status quo.

Not recommended: Define multiple variables at the same time.

Categories of variables:

As variable definitions are positioned differently in the class, the variables are divided into two main categories:

member Variable: a variable directly defined in a class, also known as a global variable or field.

Local variables: variables other than member variables are local variables. There are 3 different forms of representation according to the defined position: Method parameter, method inner variable, code block variable.

Variable scope:

Refers to the scope of the existence of a variable, only within this scope, the program code to access it. When a variable is defined, its scope is determined: the scope is the end of the curly brace where the definition begins to the definition;

Variable usage rules:

1. Variables must be declared and initialized before they can be used;

2. Declaring a variable must have a data type

3. Variable names cannot be defined repeatedly in the same scope

Expression:

is a combination of numbers, operators, numeric groupings of symbols (parentheses), constants, variables, and so on that can be evaluated in a meaningful arrangement. A variable/constant concatenated with an operation symbol can be called an expression. That is, the expression is a statement that consists of constants, variables, operators, and parentheses that make a meaningful result of the value being evaluated.

The end result of an operand in an expression is the result of an expression.

The order of operations for expressions (mostly as in math, refer to the following operator precedence):

Data types and classifications

The data types in Java are divided into two broad categories as a whole:

1): Basic data type/native data type, total 8:

1>: Numeric: Integer type: byte,short,int,long, different integer types in memory the size of memory space is not the same. Decimal type: float,double.

2>: Character type: Char, what is character, letter/symbol.

3>: Boolean: Boolean, which represents the right and wrong. True,false.

2): Reference data Type/Object data type: Class/interface/array

Boolean type:

Typically used for logical operations and Program flow control (condition selection/looping). The value of this type can only be true or FALSE, which indicates true or FALSE. You cannot use an integer of 0 or not 0 instead of false and true, which is distinguished from the C language. (In fact, the processing of the Boolean in the JVM also uses 0 to indicate false, and 0 for true.) )

False and True are Boolean constants.

Integer type (byte-short-int-long):

BYTE, short, int, long type: Four representations of integer constants:

① binary integers: 0B or 0b (new features of Java7), such as: int a = 0b110;

② octal integer: Required to start with 0, such as int a = 012;

③ decimal integer: For example: int a = 17;

④ hexadecimal integer: Requires a 0X or 0x start, such as int a = 0x12;

The Java language's integer constants are type int by default, and a long variable is declared with ' l ' or ' l ', and uppercase L is recommended because the lowercase l is easily confused with the number 1. By default, the literal of an integral type is the int type by default.

byte,short,int, long the only difference is that the capacity size of the storage space is not the same. Depending on the data you want to store, choose the appropriate data type, in general, use Int.

Decimal type (float-double):

float, double type: Represents a decimal type, also known as a floating-point type, where float represents a single-precision type and double represents a dual-precision type, but neither can represent an exact decimal number.

There are two representations of the floating-point type constants in Java:

Decimal form: For example: 3.14, 168.0,. 618

Scientific notation forms: For example: 3.14e2, 3.14E2, 1000E-2 scientific notation The result returned is a double type. By default, the literal of a floating-point type is the double type by default. To declare a constant of type float, you need to add either D or D after the constant after the F or f,double constant to omit it.

Note: Only floating-point variables in Java can accept the results of scientific calculations: because both float and double cannot accurately represent decimals, how do you represent accurate decimals in systems with high precision, such as in a banking system? With the BigDecimal type, it represents data of arbitrary precision.

Character type (char):

characters, letters, and symbols.

Char type : Represents a 16-bit unsigned integer or Unicode character, and Java encodes characters with Unicode characters. Unicode collects symbols from all the languages in the world, and is a cross-platform encoding that takes two bytes of Java characters and can represent a single character.

What is encoding?

A computer can only represent 0 and 12 numbers, so people make a rule to use a number to denote a particular character, such as a using 97. Char is duplicated in the first 256 characters and ASCII (American Information Interchange Standard code) codes.

There are 3 representations of Char constants:

① use a single character directly to specify a character constant, such as ', ' A ', ' 7 ';

② is used directly as a decimal integer value, but the data range is in [0,65535] format such as 97, but the printed value is still the ASCII code table for the symbol, such as 97 printed out is the character a.

③ and 2, just represents a 16 binary value, formatted as ' \ux ', x represents 16 binary integers, for example: 97 of 16 is 61. So the expression for ' \u0061 ' print out is also a. So some people say that char is essentially an integral type, and it makes sense. The characters in the first 256 characters of Char and ASCII (American Information Interchange Standard code) are duplicated and can be looked up in the table.

The most commonly used reference type is-string

In addition to the 8 basic data types, all other types are reference data types, including classes, interfaces, and arrays. The default initial value of the reference data type is null.

String is a class that represents a string, the so-called string, is a string of characters, that is, n characters are concatenated together (n can represent 0 or more), like kebabs.

strings are enclosed in "", and the connection string uses the "+" symbol.

string concatenation: Strings are concatenated with any data type, and the result is a string type.

SYSTEM.OUT.PRINTLN (8 + 7 + "Hello"); // The result is 15hello . System.out.println ("Hello" + 7 + 8); // The result is hello78 . SYSTEM.OUT.PRINTLN (7 + "Hello" + 8); // The result is 7hello8 . String str = + "ABC"; // compose a new string of 17ABC  = (String) + "ABC"; // Compile Error: Non-convertible type

Data too large and overflow

When the data to be represented exceeds the critical range of the data type, it is called overflow.

When the overflow occurs, the program does not do a data range check processing, and a data disturbance occurs. int maximum value int intmax = 2147483647;

Basic Data type Conversions

In 8 basic data types, Boolean is not a numeric type and does not participate in conversions

The conversion rules are actually the spatial size of the respective data types. Look at the graphs to see different data types as containers of different capacities.

A byte is 1 bytes and can be up to 1 kilos of water.

Short is 2 bytes and can only fit up to 2 pounds of water.

An automatic type conversion, also known as an implicit type conversion:

When assigning a numeric value or variable of a small data range type to another large data range type variable, the system can complete the automatic type transformation. This is like putting a catty of water in a container into a container that can fit two kilograms of water.

Again, the Boolean type is not convertible to another data type.

In general, Byte,short,char does not participate in conversion operations. We directly pay the Byte,short,char directly to the int type.

Coercion type conversions, also known as Display type conversions:

When assigning a value or variable of a large range type to another small-scope type variable, the system cannot complete the conversion automatically, and a forced conversion is required, but such an operation may result in a reduction or overflow of data precision, so use caution. This is like putting the water in a two-pound water container in a container that can fit a kilo of water, which can cause water to overflow. To insist on putting 4 pounds of apples into a bag of 3 pounds, what should we do? Use violence.

Forced type conversions, which can cause overflow and precision loss

Automatic elevation of an expression type

When an arithmetic expression contains a value of more than one base data type (except the Boolean), the data type of the entire arithmetic expression will be automatically promoted when the data is evaluated.

The rules are:

All of the byte, short, and char types are automatically promoted to the int type; the final result type of the entire expression is promoted to the type of the highest type in the expression;

double D1 = 123 + 1.1F + 99L + 3.14; System.out.println (' a ' + 1); // 98 byte b = 11= B +; // compile error, at which point the result type should be int

Java basic variables and data types and related operations

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.