Type conversions and examples in Java

Source: Internet
Author: User

1. Automatic conversion type: Small size data type with large capacity of the data type to do the operation, small capacity will be automatically converted to a large data type.

2. Automatic conversion: char,byte,short=>int=>long=>float=>double. The char,byte,short is the int type that is the default between operations.

3. Coercion type conversion; Large capacity conversion to small capacity, to use the coercion type conversion character: (), may result in a loss of precision.

Example: Long l1=121345;

int m1= (int) L1;

SYSTEM.OUT.PRINTLN (ML)

4. The operation between a string (string, plus "") and the base data type can only be a join operation (such as +), and the resulting result is still a string.

Question 1:string str1= "Hello";

Int myint1=12;

Char chr1= ' a ';

System.out.println (STRL+MYINT1+CHR1);//hello12a

System.out.println (MYINT1+CHR1+STR1);//109hello

System.out.println (chrl+strl+myint1);//ahello12

Problem 2:string str1=4;//wrong

String str2=3.5f+ "";//Yes

System.out.println (STR2);//Output 3.5

System.out.println (3+4+ "hello!"); /Output 7hello!

System.out.println ("hello!" +3+4);//Output hello!34

System.out.println (' A ' +1+ "hello!"); /Output 98hello!

System.out.println ("Hello" + ' a ' + 1);//Output HELLOA1

5. Binary: 0 or 1, start with 0b or 0 B, start sign bit, 0 is positive, 1 is negative;

Decimal: 0-9;

Octal: 0-7, beginning with 0;

Hex: 0-9 and A-f, beginning with 0x or 0X, do not differentiate between a-f and casing.

6. Original code, anti-code and complement; ① positive, the original code, anti-code and complement three yards in one; ② negative, the inverse code for the original code on the basis of the symbol bit fixed the rest of the reverse; the complement is +1 on the inverse code.

Question 3:

The value of the problem 4:byte (bottom of the computer-15)

7. Decimal = binary: In addition to 2 the inverse of the remainder

Cases:

8. Binary = octal

9. Binary = Hex

10. Arithmetic operators, + 、-、 *,/,%, + + 、--

11. Division sign;

Title 5:int i=12;

Int J=I/5;

Double D=I/5;

Double d2= (double) i/5;

Double d1=i/5.0;

System.out.println (j);//2

System.out.println (d);//2.0

System.out.println (D2);//2.4

System.out.println (D1); 2.4

12. Modulus,%, remainder, the result of the symbol depends on the modulus

Title 6: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

13. Gaga, minus and minus; + +,--

① before + +: Increment 1 First, then do the operation;

After + +: First do the operation, after the increase of 1;

Title 7:int myint1=10;

Int myint2=myint1++;//after + +

System.out.println (myInt1);//11

System.out.println (MyInt2);//10

Int myint3=10;

Int myint4=++myint;//Pre + +

System.out.println (MYINT3);//11

System.out.println (MYINT4);//11

② before--: first, minus 1, after doing the operation;

After--: first to do the operation, after the reduction of 1;

14. Command-line mode; dir,md,rd,cd,cd..,cd/,del,exit

15.JDK, JRE, and JVM relationships, java program: write = = compile and run.

16. Single-line comments, multiline comments, and document comments, document comments/** */.

Parse Document Comment: javadoc-d file directory name-autor-version source file name. java;

17. Assignment operation; +, + =,-=, *=,/=,%=.

①+=: The operation can be implemented without changing the data type of s;

Title 8:i+=3 equivalent to i=i+3;

Title 9:short s=10;

s=s+3;//compilation does not pass

S= (short) (s+3);//compile through, do not recommend this compilation

s+=3;//can either implement the operation without changing the data type of S

②=: Assignment is not an equal sign;

Title 10:boolean B1=false;

If (b1=true) {System.out.println ("result is true")}

ELSE{SYSTEM.OUT.PRINTLN ("result is false")}

The output is true

Title 11:boolean B1=false;

If (b1==true) {System.out.println ("result is true")}

ELSE{SYSTEM.OUT.PRINTLN ("result is false")}

The output is False

Title 12:int I=1;

i*=0.1;

System.out.println (i);//0, type is still int,0.1 0

18. Comparison operators; ①==, equal to; ②!=,;③<, >, <=, >=;

④instanceof, check whether the object is a class;

Example: "Hello" instanceof string//is True

Type conversions and examples in Java

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.