In day2 java, variables and data types and types are converted. In day2java

Source: Internet
Author: User

In day2 java, variables and data types and types are converted. In day2java

Identifier: Any place that can be named by yourself is called an identifier, such as the class name, method name, and interface name...

1. Naming rules for identifiers:
It consists of 26 English letters (uppercase and lowercase), 0-9, _, or $. If you do not comply with the conditions, an error is returned.
It cannot start with a number.
You cannot use keywords or reserved words.
Java is case sensitive and has unlimited length.
The identifier cannot contain spaces.

2. Naming rules in java
Package name: multiple words make up all lowercase letters: xxxyyyzz
Class Name and Interface Name: when multiple words are combined, the first word is in lowercase, and the first letter of each word is in uppercase: xxxYyyZzz
Constant name: All letters are in upper case. Multiple words are connected with underscores (_): XXX_YYY_ZZ.
It is best to know what you mean. do not always use a, B, and c.

3. Variables
The memory space opened up by the java variable name. This space must have its own name and type. For example, int a = 1. Open a space named a and its type is integer. The scope of the variable is valid. There must be an initialization value.

public class variable{    public static void main(String[] args){        int myInt = 233;        double myDouble = 3.14;        System.out.println(myInt);        System.out.println(myInt + myDouble);    }}

The two variables we define here are valid only in this curly brackets, that is, the scope is in this curly brackets.

Eight basic data types in java

Integer: byte, short, int, long. The default value is int type.

Float: float, double (double by default ).

Character Type: char can only represent one character (English, Chinese, punctuation, '\ t',' \ u1234 ', etc.): char a =' AB ', an error is reported, contains two characters. The string str = "AB" must be defined to contain multiple characters ".

Boolean: boolean. It can only be set to true or false. It cannot be set to null or 0 or 1.

Class java_type {public static void main (String [] args) {// byte type, the default integer value is 0 byte myByte = 100; System. out. println (myByte); // short type short myShort = 233; System. out. println (myShort); // int type int myInt = 23333; System. out. println (myInt); // long type, default value: 0L long myLong = 12345678910111213L; System. out. println (myLong); // float type. The default value is 0.0f float myFloat = 234.5f; System. out. println (myFloat); // double type; default value: 0.0d double myDouble = 3.14; System. out. println (myDouble); // boolean type. The default value is false boolean myBoolean = true; System. out. println (myBoolean); // char myChar = 'J'; System. out. println (myChar );}}

Automatic type conversion

Low ----------------------------------> high byte, short, char-> int-> long-> float-> double

Type conversion cannot be performed on the boolean type. conversion may result in overflow or loss of precision.

When performing operations on small data types and large data types, small data types are automatically converted to large data types. For example, if int and short are added, the result is upgraded to the int type, and the result of adding two short data types is also upgraded to the int type. When the char type and integer type are added, the char type is converted to an integer by default (for example, 'A' is converted to the corresponding ascii code value 97 ).

Forced type conversion

If the conversion from a large capacity to a small capacity requires a forced type conversion character (), the precision will be lost.

For example, convert int 12345 to byte:

Int 12345 is converted to binary 110000 00111001, and byte is an 8-bit binary number. The value of 110000 00111001 is truncated and the value of 00111001 is 00111001. The result of conversion from to 10 is 57.

Class exchange {public static void main (String [] args) {int myInt = 12345; byte myByte = (byte) myInt; System. out. println (myByte); char c1 = 'a'; // defines a char type int i1 = c1; // the char type is automatically converted to int System. out. println ("the value after automatic char type conversion to int is equal to" + i1); char c2 = 'a'; // defines A char type int i2 = c2 + 1; // Calculation of char and int types: System. out. println ("the char type and int value are equal to" + i2 );}}

The running result is:

Root @ debian:/home/jeff/java_coding/day002 # java exchange 57char the value after automatic conversion to int is equal to 97char and the value after int calculation is equal to 66

Calculation Between String and basic data types

First, you can only use the Join Operation: +. The result is still a string.

String str = "abc";String str1 = str + myInt;System.out.println(str1);

The result is abc12345.

Arithmetic

class suanshu{    public static void main(String[] args){        int i1 = 12 % 5;        int i2 = -12 % 5;        int i3 = 12 % (-5);        int i4 = -12 % (-5);        System.out.println(i1);2        System.out.println(i2);-2        System.out.println(i3);2        System.out.println(i4);-2    }}

It can be concluded that the modulo result is related to the modulus of the modulus.

Class suanshu {public static void main (String [] args) {// ++ // a ++, calculate and then + 1 int int1 = 10; int int2 = int1 ++; System. out. println (int1); 11 System. out. println (int2); 10 // ++ a, first + 1. Then assign int int3 = 10; int int4 = ++ int3; System. out. println (int3); 11 System. out. println (int4); 11 }}

There is no difference between other arithmetic values and General Arithmetic values.

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.