Java Learning Chapter 3. Basic types of conversions and direct quantities

Source: Internet
Author: User
Tags arithmetic

Basic Type Conversions

      Automatic Conversion : If the system supports assigning a variable of a primitive type variable directly to a variant of another primitive type, this is a direct conversion.

            Java all numeric type variables can be converted to each other.

public class Autoconversion{public static void Main (String [] args) {byte A = 6;//byte type can be automatically converted to type int. int b = A; System.out.println (b); float C = b; System.out.println (c);//byte types cannot be converted to char types. Char d = (char) A; System.out.println (d);//byte type can be automatically converted to double type double e = A; System.out.println (e);}}
The operation with the string

The value of the base type is converted to a string type when the value of any base type is calculated and the string is linked.

The string is a reference type.

If you want to convert the value of the base type to the corresponding string, you can link the base type value to an empty string.

public class String{public static void Main (string []args) {//cannot be assigned to a reference variable//string a = 9;//a basic variable value plus a string. The value of the base type is automatically converted to a string variable, b = 3.14f + "";//Will output 3.14system.out.println (b);//The following will output 7hellosystem.out.println (3+4+ "Hello" )///The following will output hello34System.out.println ("Hello" +3+4);}}

  

Forcing type conversions

Forcing type conversions is similar to putting water from a large bottle in a small bottle. The syntax format is (targettype) value. The symbol that forces the type conversion is parentheses ().

public class Nconversion{public static void Main (String [] args) {int a = 200;//forces a type of int to be converted to a byte type; byte B = (byte) a;//will lose A negative number System.out.println (b);d ouble C = 3.14;//forces the conversion of a double type to an int type int d = (int) c; System.out.println (d);}}

  Expression type auto-promotion

          When an arithmetic expression contains more than one primitive type value, the data type of the entire arithmetic expression will be automatically promoted.

1. All byte types, short and char types will be automatically promoted to the int type.

2. The data structure of the entire arithmetic expression is automatically lifted to the same type as the highest-level operand in the expression.

/***<br> website: <a href = "https://i.cnblogs.com/EditPosts.aspx?postid=5868502&update=1" > Java Learning Notes </ A>*copyright (C), 2016-2017,dean zhang*this program was protected by the Copyright Laws*program name:promote*date:2016- 9-14 7:22 am
* @author Zhang
* @version 1.8*/public class autopromote{//compile will start here, main function, call method; public static void Main (String [] args) {//define a byte type value first A = 12;//The value of a on the right side of the equation will be automatically promoted to the int type,//Then an error//a = A + 11 will occur; System.out.println (a);//The following is the correct automatic type promotion calculation process; byte b = 12;char c = ' c '; int d = 500;double E = 99.9;//The result should be a double type of value E = b+c+d+ e;//quietly expected results System.out.println (e);//java is a strongly typed language that strictly specifies the format of various variables, the following will only get int type, experience ... System.out.println (28/9);//This will output 3 instead of floating-point. }}

  Direct volume

Types of direct quantities: there are usually only three types that can be directly quantified: primitive type, string type, and null type.

Java has 8 Direct volume: 1.int Type: The integer type value is given directly in the program, divided into binary, decimal, hexadecimal, and octal.

Binary starts with 0b or 0B, octal starts with 0, Hex starts with 0x or 0X

2.Long Type value: Adds 1 or L after an integer type value to become a long type of direct quantity. such as 3l,0x12l

3.float type value: Adding F or F after a floating-point number is a direct amount of type float, which can be either a standard decimal or a scientific count of 5.34f 3.14e5f

4.double Type: Remove the trailing f of float with the double type.

5.boolean Type Direct Volume: Only True and False

6.char Type Direct quantity: The char type has a direct amount of three forms, each of which is enclosed in single quotation marks. The escape character. and Unicode-represented characters. such as ' a ' \ R ' \u0099 '

7.String Type Direct quantity: A sequence of characters enclosed in double quotation marks is a direct amount of type string.

8.null Direct Volume: The direct amount of this type is only one, that is, null

Description: A null type can be assigned to any reference type variable to indicate that the save address in this reference variable is empty, which is not yet pointing to any valid direction.

public class Direct{public static void Main (String [] args) {byte A = 2;char b = ' \ r '; int c = 89999;float D = 3.14f;double e = 3.14; string f = "Yui Hatano";//java ensures that each string constant has only one and does not produce multiple copies s1=s2=s3string s1 = "Hello"; String s2 = "Hello"; String s3 = "he" + "Llo"; System.out.println (S1==S2); System.out.println (S2==S3);}}

  

Java Learning Chapter 3. Basic types of conversions and direct quantities

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.