Java Study Notes: basic types, strings, and console input and output

Source: Internet
Author: User

Java Study Notes: basic types, strings, and console input and output

1. Basic Java knowledge

Java uses semicolons to indicate the end of a statement;

Java case sensitiveFor example, mynumber and mynumber are considered to be two different names because their case is different.

2. Basic type and identifier

Variable): Variables can be understood as containers that store letters and numbers. The letters, numbers, or other data items areValue). The value can be changed, so it is calledVariable.

Variable DeclarationFormat: Type variable1, variable2 ......

That is, the type name variable name.

For example: int A; Double B;

This indicates that A is an int type variable.

Java has two main types: class type and basic type.Class type (class type, break sentence is class/type) is the type of a class, that is, the type of the object with data and methods. Primitive type is a type such as int, double, and char. Use an image example to illustrate the differences between the two. For example, string is a class type. If a class type string astring is declared, the string object astring can call the method of this string class. The call method is astring. tolowercase (). That is, add a vertex after the class type object, and then follow the class method. The basic type can only be a single number or letter. If you do not understand it, it does not matter. We will continue to introduce it later. You only need to knowJava has two main types: class type and basic type. Class types can call methods of classes, but basic types cannot.

2.1 identifier

The variable can be calledIdentifierIn Java, the first letter of the identifierOnlyYesUnderlineOrLetter, Except for the first letter, other parts of the identifierOnlyYesLetter,Arabic numeralsAndUnderline(_). It cannot contain empty spaces or other symbols. The length is unlimited.Note that Java is case sensitive. Identifiers with different cases are considered different names.Such as rate1 and timeremain. The upper and lower case letters and underscores of identifiers can be allocated at will.Identifier nameCommon sense. To name a large number (
Lager number), you can name it thelagernumber or the_lager_number. That is to say, if a name is composed of several words, you can concatenate these words. The first word is in lower case, and the first letter of the subsequent words is in upper case. Or all lowercase letters, with underscores instead of the original space (because the identifier cannot contain spaces ).

The identifier cannot be the same as the keyword. For example, if, switch, and double cannot be used. They have special and predefined meanings. Then, how can we avoid duplicate names with keywords? There is an empirical method: When you get a name, you need to name the object based on its actual usage and preferences. You do not need to look at the keyword table. For example, if a = 5, B = 7, and you want to write a function to exchange the two values, the two functions can be called switchnumber instead of switch. In this way, the possibility of duplicate names of identifiers and keywords is greatly reduced.

2.2 Basic Types

Java has the following basic types:Four shaping types: two floating point types, one single character type and one boolean type.

IntegerIt is an integer type, such as 1, 23,-54, 566. The four integer types are byte, short, Int, and long. For beginners, if the number you want to process is smaller than 2.1 billion (in most cases, the number is not greater than this value), we recommend that you use the int type for All integers. If an integer is declared as int A, it indicates that a is an integer and can be used to store integers. When declaring a variable, You can initialize the variable. That is, assign an initial value to the variable. For example, int B = 0; this statement declares a variable B used to store integers, and assigns 0 to B while declaring it. In this way, B is equal to 0. The same applies to other types.

Floating Point TypeIndicates the type that contains the decimal point, such as 3.14159 and 5.0. There are float and double types. Don't worry about that much. If there is a fractional part, you can use double for it all.

Single CharacterIt is a character, which can be a symbol.Single quotesFor example, 'A' is a single character, and the type of a single character is Char.

BooleanThere are only two values, true and false; true or false. The Boolean keyword is boolean. Boolean type can be calculated by using a Boolean expression. For Boolean expressions, refer to the first section of this article.

Note:All basic types of keywords are in lowercase.. Keywords of the basic type are used for variable declaration.

2.3 value assignment statement

If a variable is defined, You need to assign a value to the variable and modify the value of the variable at a certain time point. The value assignment statement is required. Format:

Variable = expression;

ANumber = 26; note that this is different from the general mathematical expression direction. In this formula, the left side of the equal sign isA Variable, The right side of the equal sign isOne Value. This statement sets the equal signThe value on the right is assigned to the variable on the left..

The right side of the value assignment statement can be a computing formula, such as cents = 2 + 5 + 6 + 4. In this way, the right side is calculated first. When the calculation is complete, assign the final value to the variable on the left. If you want to perform numerical operations on the variable itself, such as adding variable A to 5, it is clear that the statement used is a = a + 5. Here is a simple method. A + = 5; the effect is the same as a = a + 5. Similarly, a = A * 5; equivalent to a * = 5; A = A-5; equivalent to a-= 5;

2.4 simple screen output

After talking so much, it may be a bit boring. Then, let's write something that must be learned in the first lesson of programming and play Hello World on the screen. In eclipse, create a new project. In this project, create a class named helloworld. Add system. Out. println ("Hello world! "), Click Run, a line of Hello world will appear in the console area below !. For detailed steps, see this article. Therefore, you will understand that if you want to display the content in the console (that is, equivalent to a command line, also known as a DOS window), you can use system. out. println ();, add the content you want to add in the brackets of this statement. When the program executes this statement, the content in the brackets will be output to the console.

2.5 simple input

The simple output is clear, so we will introduce the input. It should be pointed out that the program is used as a reference object to generate output from the program. When the program is running, we press the keyboard to input a value to the program. There are three steps to implement the input function. First, add import Java in the first line of the Code. util. *; then add a line of keyboard = new keyboard (system. in); this is a set statement. Then, like the value assignment statement, you can use anumber = keyboard. nextint (); to input an integer to the program on the keyboard. And assign this integer to anumber. The complete implementation program is

Import Java. util. extends; public class helloworld2 {publicstatic void main (string [] ARGs) {intanumber; system. out. println ("enter a number") scannerkeyboard = new partition (system. in); anumber = keyboard. nextint (); system. out. println ("the value you entered is:" + anumber );}}

Import java. util. Handler; tells the Java compiler to find the definition of the handler class used for input. This is equivalent to finding the word "category" in the dictionary. This is the dictionary directory that tells you where the category is.

Export keyboard = newkeyboard (system. In); this line of code must be before obtaining the code entered by the keyboard. The detailed principle will be explained later.

ANumber = keyboard. nextint (); when this sentence is run, the program will "Suspend" and wait for you to enter an integer in the console and press Enter. Then the integer you entered is assigned to the anumber variable.

Note:: Use System. Out. println () to output the content. If it is int, double, Char, It outputs all the stored values. If it outputs the boolean type, It outputs false or true, which is different from C ++.

2.6 numeric Constants

This section describes how to write a constant number in a program. Compared with the variable, the constant value does not change. In addition to numeric constants, there are other types such as character constants. 2, 3, 0, 95, etc., are numerical constants. In Java, you can add a plus or minus sign before a number, for example, + 12,-72. But there cannot be commas, such as 1,000, which is incorrect. For decimals, scientific notation can be used. For example, 8.65e8 in Java indicates 865000000.0.The decimal point can be floating at any position according to the size of the number after E, which is the origin of the floating point name.For example, 8.65e8 is the same as 86.5e7, but the decimal point is floating. Note: E is case-insensitive, but the digits after e must be integers, positive integers, floating to the right of the decimal point, and floating to the left of the decimal point.

2.7 type conversion

Floating Point Numbers contain decimal places, while integers do not. Therefore, the two types cannot be equal to each other. Even 7 and 7.0 are different. Assigning an integer value to the floating-point type can convert the implicit type, but it is generally not recommended because it is prone to Imperceptible errors. To perform type conversion, we recommend that you use forced type conversion. For example, if the number is double points = 2.0, and an integer is int distance, you must assign the value of points to distance. The statement is as follows: distance = (INT) points; that is, add (INT) before the value to the right of the equal sign to indicate the type to be converted. Note that no fewer parentheses are allowed.

This article is of the key type and can be used for reference. The best way to learn programming is to debug the examples in the book in front of textbooks. This gives us a deep understanding. Wish you a pleasant journey!

If there is any error, please point out that I will accept it and correct it modestly! To be continue...

Sunny

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.